//获取当前人员是否含有指定角色权限 function UserHasRole(roleName) { //get Current User Roles, oXml is an object var oXml = GetCurrentUserRoles(); if(oXml != null) { //select the node text var roles = oXml.selectNodes("//BusinessEntity/q1:name"); if(roles != null) { for( i = 0; i < roles.length; i++) { if(roles[i].text == roleName) { //return true if user has this role return true; } } } } //otherwise return false return false; }
CRM 2011
Xrm.Page.context.getUserRoles()
function UserHasRole(roleName) { var serverUrl = Xrm.Page.context.getServerUrl(); var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/"; oDataEndpointUrl += "RoleSet?$top=1&$filter=Name eq '" + 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.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; } function GetRequestObject() { if (window.XMLHttpRequest) { return new window.XMLHttpRequest; } else { try { return new ActiveXObject("MSXML2.XMLHTTP.3.0"); } catch (ex) { return null; } } } 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; }
以下文章引自: http://community.dynamics.com/crm/b/crmmitchmilam/archive/2010/11/16/retreiving-user-roles-in-crm-2011
Retreiving User Roles in CRM 2011
As I was producing the associated documentation for my CRM Migration Assistant application, I decided to explore a comparison between a technique that we had to do the “hard way” in CRM 4.0 and a technique that is built into CRM 2011
The technique in question is retrieving a user’s security roles in order to perform some role-specific actions.
CRM 4.0 JavaScript
Jim Wang, friend and fellow MVP has an excellent article and JavaScript describing how to retrieve a user’s security roles in order to perform operations that are specifically for a certain type of CRM user:
//check if the current user has the 'System Administrator' role
alert(UserHasRole("System Administrator"));
function UserHasRole(roleName)
{
//get Current User Roles, oXml is an object
var oXml = GetCurrentUserRoles();
if(oXml != null)
{
//select the node text
var roles = oXml.selectNodes("//BusinessEntity/q1:name");
if(roles != null)
{
for( i = 0; i < roles.length; i++)
{
if(roles[i].text == roleName)
{
//return true if user has this role
return true;
}
}
}
}
//otherwise return false
return false;
}
I use this code in many different situations to show and hide CRM form elements for specific people.
CRM 2011 SDK Samples
If you look at the files included with the CRM 2011 SDK, you’ll find some sample JavaScript in the folder:
sdkSampleCodeJSFormScripts
If you load that solution into Visual Studio, you can see the many cool and interesting additions to the CRM 2011 JavaScript object model.
If you open SDK.ContextSamples.js, you’ll see some of the code I’ll be using today.
Xrm.Page.context.getUserRoles()
Jim’s CRM 4.0 code uses a SOAP call retrieve the security roles for a user. Lucky for us, this functionality is now built into CRM 2011 in the method:
Xrm.Page.context.getUserRoles()
Which returns an array of strings representing the GUID values of each of the security roles that the user is associated with.
This is really great, but I would like to refer to my security roles by name since it’s easier to remember and understand than a GUID. So, I had to add some extra code to handle that requirement.
CRM 2011 JavaScript
As I mentioned, I took the SDK sample code, modified it a bit, and replicated Jim’s functionality exactly, using the following BLOCKED SCRIPT
function UserHasRole(roleName) { var serverUrl = Xrm.Page.context.getServerUrl(); var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/"; oDataEndpointUrl += "RoleSet?$top=1&$filter=Name eq '" + 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.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; } function GetRequestObject() { if (window.XMLHttpRequest) { return new window.XMLHttpRequest; } else { try { return new ActiveXObject("MSXML2.XMLHTTP.3.0"); } catch (ex) { return null; } } } 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; }
Conclusion
As you can see, this is not a lot of code and with CRM 2011’s ability to create a Web Resource, I can add these functions to a JavaScript library, reference that library on the form, and just use the following code where necessary:
If (UserHasRole("System Administrator"))
{
// do something important
}
The secondary affect of using this new code is I don’t have to change my CRM 4.0 JavaScript since I duplicated the CRM 4.0 functionality in CRM 2011 and the usage show above, remains the same.