hub.eb?material_id=558&track_id=547

Checking Roles


The Security Manager interface provides methods to retrieve and query roles that may belong to a user. Methods include:

  • getRoles() - returns an array of the role names belonging to the logged in user.
  • hasRole(roleName) - returns true when a logged in user holds a named role, false otherwise.

Roles provide a way to restrict access to certain forms, pages and functions. For example, page behaviour may be changed and controls may be shown or hidden depending on the user's roles.

 

Steps


1

Create a form and add a button called edit.

 

2

Add a Before Form script and hide or show the edit button depending on whether the user has the admin role. Paste the following:

// Check the user has the admin role
if (system.securityManager.hasRole("admin")) {
  // show the edit button for admin users
  controls.edit.show();
}
else {
   // hide the edit button for non-admin users
   controls.edit.hide();
}
3

Run the form and notice how the control is hidden as you do not have the admin role.

Current Module

Related