MSCRM2011 Current User has Role 【判定当前用户角色方法】
第一种:普通的RetrieveMultiple方式
//功能说明: 获取当前用户是否具有该角色 //备注: //参数说明: // (string/string[])roleName: 角色名称(列表) //返回值说明(return): // bool: function CurrentUserHasRoles(roleName) { var xml = "" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" + Xrm.Page.context.getAuthenticationHeader() + " <soap:Body>" + " <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" + " <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" + " <q1:EntityName>role</q1:EntityName>" + " <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" + " <q1:Attributes>" + " <q1:Attribute>name</q1:Attribute>" + " </q1:Attributes>" + " </q1:ColumnSet>" + " <q1:Distinct>false</q1:Distinct>" + " <q1:LinkEntities>" + " <q1:LinkEntity>" + " <q1:LinkFromAttributeName>roleid</q1:LinkFromAttributeName>" + " <q1:LinkFromEntityName>role</q1:LinkFromEntityName>" + " <q1:LinkToEntityName>systemuserroles</q1:LinkToEntityName>" + " <q1:LinkToAttributeName>roleid</q1:LinkToAttributeName>" + " <q1:JoinOperator>Inner</q1:JoinOperator>" + " <q1:LinkEntities>" + " <q1:LinkEntity>" + " <q1:LinkFromAttributeName>systemuserid</q1:LinkFromAttributeName>" + " <q1:LinkFromEntityName>systemuserroles</q1:LinkFromEntityName>" + " <q1:LinkToEntityName>systemuser</q1:LinkToEntityName>" + " <q1:LinkToAttributeName>systemuserid</q1:LinkToAttributeName>" + " <q1:JoinOperator>Inner</q1:JoinOperator>" + " <q1:LinkCriteria>" + " <q1:FilterOperator>And</q1:FilterOperator>" + " <q1:Conditions>" + " <q1:Condition>" + " <q1:AttributeName>systemuserid</q1:AttributeName>" + " <q1:Operator>EqualUserId</q1:Operator>" + " </q1:Condition>" + " </q1:Conditions>" + " </q1:LinkCriteria>" + " </q1:LinkEntity>" + " </q1:LinkEntities>" + " </q1:LinkEntity>" + " </q1:LinkEntities>" + " </query>" + " </RetrieveMultiple>" + " </soap:Body>" + "</soap:Envelope>" + ""; var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP"); xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false); xmlHttpRequest.setRequestHeader("SOAPAction", " http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple"); xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); xmlHttpRequest.setRequestHeader("Content-Length", xml.length); xmlHttpRequest.send(xml); var resultXml = xmlHttpRequest.responseXML; var bVisible = false; var roles = resultXml.selectNodes("//BusinessEntity/q1:name"); if (roles != null) { var arr = new Array(); if (roleName.constructor == Array) { arr = roleName; } else { arr[0] = roleName; } for (var i = 0; i < roles.length; i++) { for (var j = 0; j < arr.length; j++) { if (roles[i].text == arr[j]) { bVisible = true; break; } } if (bVisible == true) { break; } } } return bVisible; }
第二种: OData方式
//功能说明: 获取当前用户是否具有该角色 //备注: //参数说明: // roleName: 角色名称 //返回值说明(return): // bool: function CurrentUserHasRole(roleName) { //为了避免OData中文检索不出这种问题,使用encodeURIComponent来encode; var serverUrl = Xrm.Page.context.getServerUrl(); var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/"; oDataEndpointUrl += "RoleSet?$top=1&$filter=Name eq " + encodeURIComponent (roleName); var service = GetRequestObject(); if (service != null) { service.open("GET", oDataEndpointUrl, false); service.setRequestHeader("X-Requested-Width", "XMLHttpRequest"); service.setRequestHeader("Accept", "application/json, text/javascript, */*"); service.send(null); var requestResults = eval('(' + service.responseText + ')').d; if (requestResults != null && requestResults.results.length == 1) { var role = requestResults[0]; var id = role.RoleId; var currentUserRoles = Xrm.Page.context.getUserRoles(); for (var i = 0; i < currentUserRoles.length; i++) { var userRole = currentUserRoles[i]; if (GuidsAreEqual(userRole, id)) { return true; } } } } return false; } //功能说明: 获取请求体 //备注: //参数说明(Parameters): //返回值说明(return): // RequestObject function GetRequestObject() { if (window.XMLHttpRequest) { return new window.XMLHttpRequest; } else { try { return new ActiveXObject("MSXML2.XMLHTTP.3.0"); } catch (ex) { return null; } } } //功能说明: 比较两个Guid //备注: //参数说明(Parameters): // guid1:Guid // guid2: Guid //返回值说明(return): // bool: function GuidsAreEqual(guid1, guid2) { var isEqual = false; if (guid1 == null || guid2 == null) { isEqual = false; } else { isEqual = guid1.replace(/[{}]/g, "").toLowerCase() == guid2.replace(/[{}]/g, "").toLowerCase(); } return isEqual; }
posted on 2011-08-24 13:12 Lost In Republic 阅读(488) 评论(2) 编辑 收藏 举报