A while back I wrote about how to determine if a user has a secured action defined for them in one of their roles.
This is handy but what about if you want to see if a user is in a particular role?
Well the Sage.SalesLogix.API assembly provides this functionlity by using the CurrentUserInRole method in the Security class:
Sage.SalesLogix.API.MySlx.Security.CurrentUserInRole("A Role");
This method accepts a single string parameter of the role to check for. It then returns true or false depending on if the user is a member of the role.
A couple of things to note:
- The admin user will always have a return of true.
- The method will throw an object reference error if you try to see if the current user is in a role that does not exist (unless you are logged in as admin).
- The role name is case sensitive.
To account for the null reference error you should first check to make sure that the role exists. The easiest way to do this is with the EntityFactory's GetRepository.FindByFirstProperty, like so:
Sage.Entity.Interfaces.IRole role = Sage.Platform.EntityFactory.GetRepository<Sage.Entity.Interfaces.IRole>().FindFirstByProperty("RoleName", "A Role");
Now lets look a a full example. Let's say we want to have a field be disabled unless the current user is in a role called "Super User". To do this we can add the following to the smart part's load action:
bool inRole = false;
Sage.Entity.Interfaces.IRole role = Sage.Platform.EntityFactory.GetRepository<Sage.Entity.Interfaces.IRole>().FindFirstByProperty("RoleName", "Super User");
if (role != null)
{
inRole = Sage.SalesLogix.API.MySlx.Security.CurrentUserInRole("Super User");
}
this.myField.Enabled = inRole;