Thursday, August 8, 2019

How to check if current user has a specific security role in Dynamic CRM



vjCity.CleanseId = function (id) 
{
    return id.replace("{", "").replace("}", "");
}

vjCity.CurrentUserHasRole = function (roleName) 
{
    var userId = Xrm.Page.context.getUserId();
    return vjCity.HasRoleByName(roleName, userId);
}

vjCity.HasRoleByName = function (roleName, userId) 
{
    ///
    ///     Determines whether user has role.
    ///

    /// Name of the role.
    /// The user identifier.
    /// true if user has role otherwise false. 

    var roles = vjCity.GetUserRoles(true, userId);
    for (var i = 0; i < roles.length; i++) {
        if (roles[i].Name === roleName) {
            return true;
        }
    }
    return false;
}

vjCity.GetUserRoles = function (useCachedRole, userId) 
{
    ///
    ///     Gets the user roles.
    ///

    /// The use cached role(boolean).
    /// The user identifier. 
    userId = vjCity.CleanseId(userId);
    if (!useCachedRole || cachedUserRole[userId] == null) {
        var userRoles = [];
        var data = vjCity.GetEntitiesByCriteria('systemuserrolescollection', "?$select=roleid&$filter=systemuserid eq " + userId);
        if (data != null && data.value.length > 0) {
            for (var i = 0; i < data.value.length; i++) {
                var roleId = data.value[i].roleid;
                var name = vjCity.GetEntityById(roleId, "roles", "?$select=name").name;
                if (name != null) {
                    var role = {};
                    role.RoleId = vjCity.CleanseId(roleId);
                    role.Name = name;
                    userRoles.push(role);
                }
            }
            cachedUserRole[userId] = userRoles;
        }
    }
    return cachedUserRole[userId];
}

vjCity.GetEntityById = function (entityId, entityPlurarName, query) 
{
    entityId = vjCity.CleanseId(entityId);
    var data = null;
    var req = new XMLHttpRequest();
    req.open('GET', vjCity.GetWebAPIUrl() + entityPlurarName + "(" + entityId + ")" + query, false);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Prefer", "odata.include-annotations=*");
    req.send();
    if (req.readyState == 4 /* complete */) {
        if (req.status == 200) {
            data = JSON.parse(req.response);
        }
        else {
            var error = JSON.parse(req.response).error;
            console.log(error.message);
        }
    }
    return data;
}

vjCity.GetEntitiesByCriteria = function (entityPlurarName, query) 
{
    var data = null;
    var req = new XMLHttpRequest();
    req.open('GET', vjCity.GetWebAPIUrl() + entityPlurarName + query, 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.send();

    if (req.readyState == 4) {
        if (req.status == 200) {
            data = JSON.parse(req.response);
        }
        else {
            var error = JSON.parse(req.response).error;
            console.log(error.message);
        }
    }
    return data
}

No comments: