JS代码:
// 匹配安全角色 function matchUserRole(entityRoleNameArray) { var isMatch = false; if (entityRoleNameArray.length > 0) { var strPermitRoleNames = entityRoleNameArray.split(","); var userRoleNameArray = new Array(); // 设置筛选条件 var filter = ""; // 获取当前用户安全角色Guid组 var userRolesGuid = Xrm.Page.context.getUserRoles(); for (var index = 0; index < userRolesGuid.length; index++) { filter += "<value>" + userRolesGuid[index] + "</value>"; } var fetchXMLEnableRules = "<fetch mapping='logical'>" + "<entity name='role'>" + "<attribute name='roleid'/>" + "<attribute name='name'/>" + "<order attribute='name' descending='false'/>" + "<filter type='and'>" + "<condition attribute='roleid' operator='in'>" + filter + "'</condition>" + "</filter>" + "</entity>" + "</fetch>"; var clientURL = Xrm.Page.context.getClientUrl(); var uri = clientURL + "/api/data/v8.2/roles?fetchXml=" + encodeURI(fetchXMLEnableRules); var req = new XMLHttpRequest() req.open("GET", encodeURI(uri), false); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.onreadystatechange = function () { if (this.readyState == 4 /* complete */) { req.onreadystatechange = null; if (this.status == 200 || this.status == 204) { var objData = JSON.parse(this.responseText); for (var i = 0; i < objData.value.length; i++) { var objDataItem = objData.value[i]; var roleName = objDataItem["name"]; if (strPermitRoleNames.indexOf(roleName) != -1) { isMatch = true; return true; } } } else { var error = JSON.parse(this.response).error; Xrm.Utility.alertDialog(error.message); } } }; req.send(); } return isMatch; }
C#代码:
待补充