zoukankan      html  css  js  c++  java
  • CRM 2011 查找并判断用户是否有该角色

    注意:用户需要安全角色读的权限,

    function onLoad() {
        CurrentUserHasRole('系统管理员'); //有则是TRUE
    }
    
    //功能说明: 获取当前用户是否具有该角色
    //备注:               
    //参数说明:
    //          roleName: 角色名称
    //返回值说明(return):
    //          bool:
    function CurrentUserHasRole(roleName) {
        //为了避免OData中文检索不出这种问题,使用encodeURIComponent来encode;
        var serverUrl = Xrm.Page.context.getClientUrl();
        var curUserId = Xrm.Page.context.getUserId();
        var retrieveEntityReq = RetrieveEntityRecordByGuid("SystemUser", curUserId);
        retrieveEntityReq.send();
        if (retrieveEntityReq.status == 200) {
            var retrieved = JSON.parse(retrieveEntityReq.responseText).d;
            var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/";
            oDataEndpointUrl += "RoleSet?$filter=Name eq " + "'" + encodeURIComponent(roleName) + "'" + " and BusinessUnitId/Id eq (guid'" + retrieved.BusinessUnitId.Id + "')";
    
            var service = new XMLHttpRequest();
            if (service != null) {
                service.open("GET", oDataEndpointUrl, false);
                service.setRequestHeader("Accept", "application/json");
                service.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                service.send(null);
    
                var requestResults = eval('(' + service.responseText + ')').d;
                if (requestResults != null && requestResults.results.length == 1) {
                    var role = requestResults.results[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;
        }
    }
    
    
    //功能说明: 比较两个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;
    }

    添加OnLoad方法时需选择 传递参数

  • 相关阅读:
    Typora使用腾讯云图床
    2020年8月总结
    113 路径之和II
    103 二叉树的锯齿形层次遍历
    128 最长连续序列
    160 相交链表
    33 搜索旋转排序数组
    学习制作GitHub徽标
    105 从前序与中序遍历序列构造二叉树
    重新封装了layer.tips,自定义跟随弹窗
  • 原文地址:https://www.cnblogs.com/z1984/p/3161232.html
Copyright © 2011-2022 走看看