locked
Hiding Sections Depending On Security Roles RRS feed

  • Question

  • Hi all !

    im searching this for several days on internet and finally i have come a point. im sharing the code below but there is a problem in my IF Condition. It starts from ELSE. Clearly seems the code is not detecting the Security Role 'Customer Relationship' which user have.

    Please help me to fix it.

    Thanks!

    function ShowHideDispatchSection() {
    
        if (UserHasRole("Customer Relationship")) {
    
            Xrm.Page.ui.tabs.get("general").sections.get("general_section_5").setVisible(true);
            alert('Worked');
    }
    else {
            Xrm.Page.ui.tabs.get("general").sections.get("general_section_5").setVisible(false);
            alert('Not Worked');
        }
    }
    function UserHasRole(roleName) {
        var serverUrl = Xrm.Page.context.getServerUrl();
        var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/";
        var org = Xrm.Page.context.getOrgUniqueName()
        var oDataEndpointUrl = "/" + org + "/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.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;
    }
    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;
    }


    • Edited by Eldanar Friday, March 14, 2014 1:33 PM
    Friday, March 14, 2014 10:18 AM

Answers

  • Try this this implementation - the one you are using doesn't work if you have multiple business units. In that case you often have multiple roles with the same name - you either you need to add a business unit to your oData query, or remove the Top 1 portion of the query and iterate through all the results.

    function ShowHideDispatchSection() {
        if (UserHasRole("System Administrator")) {
            Xrm.Page.ui.tabs.get("general").sections.get("general_section_5").setVisible(true);
            alert('Worked');
        } else {
            Xrm.Page.ui.tabs.get("general").sections.get("general_section_5").setVisible(false);
            alert('Not Worked');
        }
    }
    
    function UserHasRole(roleName) {
        var currentUserRoles = Xrm.Page.context.getUserRoles();
        var serverUrl = Xrm.Page.context.getClientUrl();
        var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/RoleSet?$select=RoleId&$filter=Name eq '" + roleName + "'";
        var result = false;
        var retrieveReq = new XMLHttpRequest();
        retrieveReq.open("GET", oDataSelect, false);
        retrieveReq.setRequestHeader("Accept", "application/json");
        retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
        retrieveReq.onreadystatechange = function () {
            if (retrieveReq.readyState === 4) {
                if (retrieveReq.status === 200) {
                    var retrieved = JSON.parse(retrieveReq.responseText).d;
                    if (retrieved.results.length > 0) {
                        for (var i = 0; i < currentUserRoles.length; i++) {
                            for (var j = 0; j < retrieved.results.length; j++) {
                                if (GuidsAreEqual(currentUserRoles[i], retrieved.results[j].RoleId)) {
                                    result = true;
                                }
                            }
                        }
                    }
                }
            }
        };
    
        retrieveReq.send();
        return result;
    };
    
    function GuidsAreEqual(guid1, guid2) {
        var isEqual;
        isEqual = false;
        if (guid1 == null || guid2 == null) {
            isEqual = false;
        } else {
            isEqual = (guid1.replace(/[{}]/g, "").toLowerCase() == guid2.replace(/[{}]/g, "").toLowerCase());
        }
        return isEqual;
    };

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn



    Friday, March 14, 2014 1:15 PM
    Moderator

All replies

  • Hi Eldanar,

    There is a typo in "Customer Releationship" I believe. It should be "Customer Relationship", there's an extra e in Relationship.


    Admin QuikView Solution for CRM 2013

    Friday, March 14, 2014 10:21 AM
  • Thanks Dynamotion but unfortunately it was not the problem.
    Friday, March 14, 2014 11:22 AM
  • Try this this implementation - the one you are using doesn't work if you have multiple business units. In that case you often have multiple roles with the same name - you either you need to add a business unit to your oData query, or remove the Top 1 portion of the query and iterate through all the results.

    function ShowHideDispatchSection() {
        if (UserHasRole("System Administrator")) {
            Xrm.Page.ui.tabs.get("general").sections.get("general_section_5").setVisible(true);
            alert('Worked');
        } else {
            Xrm.Page.ui.tabs.get("general").sections.get("general_section_5").setVisible(false);
            alert('Not Worked');
        }
    }
    
    function UserHasRole(roleName) {
        var currentUserRoles = Xrm.Page.context.getUserRoles();
        var serverUrl = Xrm.Page.context.getClientUrl();
        var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/RoleSet?$select=RoleId&$filter=Name eq '" + roleName + "'";
        var result = false;
        var retrieveReq = new XMLHttpRequest();
        retrieveReq.open("GET", oDataSelect, false);
        retrieveReq.setRequestHeader("Accept", "application/json");
        retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
        retrieveReq.onreadystatechange = function () {
            if (retrieveReq.readyState === 4) {
                if (retrieveReq.status === 200) {
                    var retrieved = JSON.parse(retrieveReq.responseText).d;
                    if (retrieved.results.length > 0) {
                        for (var i = 0; i < currentUserRoles.length; i++) {
                            for (var j = 0; j < retrieved.results.length; j++) {
                                if (GuidsAreEqual(currentUserRoles[i], retrieved.results[j].RoleId)) {
                                    result = true;
                                }
                            }
                        }
                    }
                }
            }
        };
    
        retrieveReq.send();
        return result;
    };
    
    function GuidsAreEqual(guid1, guid2) {
        var isEqual;
        isEqual = false;
        if (guid1 == null || guid2 == null) {
            isEqual = false;
        } else {
            isEqual = (guid1.replace(/[{}]/g, "").toLowerCase() == guid2.replace(/[{}]/g, "").toLowerCase());
        }
        return isEqual;
    };

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn



    Friday, March 14, 2014 1:15 PM
    Moderator
  • Thank you so much Jason !!

    It worked and you helped me really much!

    Have a nice day!

    Friday, March 14, 2014 1:29 PM