Skip to content

Instantly share code, notes, and snippets.

@learn2reid
Created March 18, 2024 21:27
Show Gist options
  • Save learn2reid/c210ccf62eebe037f260cc6c69fc7dc5 to your computer and use it in GitHub Desktop.
Save learn2reid/c210ccf62eebe037f260cc6c69fc7dc5 to your computer and use it in GitHub Desktop.
// Check if the user has a specific security role
function userHasRole(roleName) {
var userRoles = Xrm.Utility.getGlobalContext().userSettings.roles.get();
for (var i = 0; i < userRoles.length; i++) {
if (userRoles[i].name === roleName) {
return true;
}
}
return false;
}
// Function to hide a view
function hideView(viewName) {
var viewTab = document.querySelector("[data-id='" + viewName + "']");
if (viewTab) {
viewTab.style.display = "none";
}
}
// Function to check user's role and hide the view if not allowed
function restrictViewByRole(viewName, allowedRole) {
if (!userHasRole(allowedRole)) {
hideView(viewName);
}
}
// Usage: Call this function in a form onload event or other appropriate event
function restrictViewsBasedOnUserRole() {
// Specify the view name and the role that is allowed to access it
restrictViewByRole("viewName1", "Administrator");
restrictViewByRole("viewName2", "Manager");
// Add more views and their allowed roles as needed
}
// Call the function when the form loads
restrictViewsBasedOnUserRole();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment