// A namespace defined for the sample code
// As a best practice, you should always define
// a unique namespace for your libraries
var Person = window.Person || {};
(function () {
"use strict"
var userSettings = Xrm.Utility.getGlobalContext().userSettings;
this.enableCheckAccess = function (_primaryControl) {
var isEnableFormConfirm = false;
try {
if (CheckSpecificRoleWithAUser(userSettings.userId, "System Administrator")) {
isEnableFormConfirm = true;
} else {
isEnableFormConfirm = false;
}
} catch (e) {
this.openAlertDialog("Error from EnableFormConfirm: " + e.message);
}
return isEnableFormConfirm;
}
this.checkSpecificRoleWithAUser = function (_systemUserID, _roleName) {
var userHasRole = false;
try {
var fetchData = {
systemuserid: _systemUserID,
name: _roleName
};
var fetchXml = [
"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>",
" <entity name='systemuser'>",
" <attribute name='fullname' />",
" <attribute name='systemuserid' />",
" <order attribute='fullname' descending='false' />",
" <filter type='and'>",
" <condition attribute='systemuserid' operator='eq' value='", fetchData.systemuserid, "'/>",
" </filter>",
" <link-entity name='systemuserroles' from='systemuserid' to='systemuserid' visible='false' intersect='true'>",
" <link-entity name='role' from='roleid' to='roleid' alias='aa'>",
" <filter type='and'>",
" <condition attribute='name' operator='eq' value='", fetchData.name, "'/>",
" </filter>",
" </link-entity>",
" </link-entity>",
" </entity>",
"</fetch>",
].join("");
this.retrieve(globalVariables.WebAPIVersion, "systemusers", fetchXml);
if (globalVariables.Results.length > 0) {
userHasRole = true;
}
} catch (e) {
this.openAlertDialog("Error from CheckSpecificRoleWithAUser: " + e.message);
}
return userHasRole;
};
this.retrieve = function (webAPIVersion, entitysPluralName, fetchXmlQuery) {
try {
var req = new XMLHttpRequest();
req.open(
"GET",
Xrm.Page.context.getClientUrl() +
"/api/data/" + webAPIVersion + "/" + entitysPluralName + "?fetchXml=" +
encodeURIComponent(fetchXmlQuery),
false
);//Sync
req.setRequestHeader("Prefer", 'odata.include-annotations="*"');
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
globalVariables.Results = results.value;
} else {
alert(this.statusText);
}
}
};
req.send();
} catch (e) {
this.openAlertDialog("Error from Retrieve: " + e.message + ".");
}
}
this.openAlertDialog = function (_text) {
try {
Xrm.Navigation.openAlertDialog(_text);
} catch (e) {
this.openAlertDialog("Error in openAlertDialog: " + e.message + ".");
}
}
}).call(Person);
Comments
Post a Comment