-
Notifications
You must be signed in to change notification settings - Fork 0
/
SecurityRole.js
46 lines (37 loc) · 1.66 KB
/
SecurityRole.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function CheckUserRole() {
// GUID of the custom role that you created.
var CustomViewOnlyRoleId = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
var returnValue = false;
// Get all the roles of the Logged in User.
var currentUserRoles = Xrm.Page.context.getUserRoles();
// Get the Parent roles for each, and then compare.
GetParentRoles(currentUserRoles, function (result) {
debugger;
for (var i = 0; i < result.value.length; i++) {
if (result.value[i]["_parentrootroleid_value"].toLowerCase() == CustomViewOnlyRoleId.toLowerCase())
returnValue = true;
}
}, function (error) {
alert(error);
}
);
return returnValue;
}
function GetParentRoles(roles, successCallback, errorCallback) {
var fetchXml = '';
fetchXml += '<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">';
fetchXml += '<entity name="role">';
fetchXml += ' <attribute name="name" />';
fetchXml += ' <attribute name="businessunitid" />';
fetchXml += ' <attribute name="roleid" />';
fetchXml += ' <attribute name="parentrootroleid" />';
fetchXml += ' <order attribute="name" descending="false" />';
fetchXml += ' <filter type="or">';
for (var cnt = 0; cnt < roles.length; cnt++) {
fetchXml += ' <condition attribute="roleid" operator="eq" value="{' + roles[cnt] + '}" />';
}
fetchXml += ' </filter>';
fetchXml += '</entity>';
fetchXml += '</fetch > ';
MK.WebAPI.Retrieve("roles", null, fetchXml, null, successCallback, errorCallback, true, null, null, false);
}