Sunday, October 18, 2009

MSCRM 4: Another script to check current user's security role

[UPDATE - May, 23 2010] The script is now part of the new CRM Web Service Toolkit.

It's quite common in CRM projects to use JavaScript code to check current user's security roles and then manipulate the CRM form in certain way, such as hiding/showing or enabling/disabling a field, a section or even a tab based on current user's secuiry roles.

CRM MVP Jim Wang previously published a snippet code that checks the current user's security role, which works fine. I tried to come up a little cleaner code that does the same thing by using my CRM JavaScript Web Service Helper.

/**
 * Check if the current user belongs to a particular role. 
 */
IsUserInRole = function()
{
    // Get current user's roles, roles is an array
    var roles = GetCurrentUserRoles();

    for (var i = 0; i < roles.length; i++)
    {
        for (var j = 0; j < arguments.length; j++)
        {
            if (roles[i] === arguments[j])
            {
                return true;
            }
        }
    }

    return false;
};

/**
 * Get all the roles that current user belongs to. 
 */
GetCurrentUserRoles = function()
{
    var xml = 
    "<fetch mapping='logical'>" +
    "   <entity name='role'>" +
    "      <attribute name='name' />" +
    "      <link-entity name='systemuserroles' from='roleid' to='roleid' link-type='inner'>" +
    "         <filter>" +
    "            <condition attribute='systemuserid' operator='eq-userid' />" +
    "         </filter>" +
    "         <link-entity name='systemuser' from='systemuserid' to='systemuserid' link-type='inner' />" +
    "      </link-entity>" +
    "   </entity>" +
    "</fetch>";
    
    var fetchResult = CrmServiceToolkit.Fetch(xml);
    var roles = [];

    if (fetchResult !== null)
    {
        for (var i = 0; i < fetchResult.length; i++)
        {
            roles[i] = fetchResult[i].getValue("name");
        }
    }
    
    return roles;
};

To use the above script, you can make a call like this:
if (IsUserInRole("System Administrator"))
{
    // Do something as I am a System Administrator
}

if (IsUserInRole("System Administrator", "System Customizer"))
{
    // Yeah, I could be either a System Administrator or a System Customizer
}

As you can see, I have made the IsUserInRole() function to accept multiple roles. If the current user belongs to any one of the provided roles, it will return true. Otherwise, it will return false.

Does the code make you a little happier? Maybe, but I am not sure. However, I am a little bit more delighted.

Hope you like it. One thing I would like to to point out is, if you make use of my CRM JavaScript Web Service Helper, you will probably find that your CRM programming life becomes somewhat easier...

[UPDATE - Jan 26, 2010] I have updated the script so it uses the latest CRM Web Service Toolkit.

[UPDATE - May, 23 2010] The script is now part of the new CRM Web Service Toolkit.

No comments:

Post a Comment