Determining if a user is in a role in the SalesLogix web client

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:

  1. The admin user will always have a return of true.
  2. 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).
  3. 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; 
ABOUT THE AUTHOR

Kris Halsrud

Kris Halsrud is a Senior Analyst / Developer for Customer FX Corporation.

3 Comments

  1. I have found that this function does not always work correctly. instead you can do your own search like so:

    bool inRole = false;
    if(Sage.SalesLogix.API.MySlx.Security.CurrentSalesLogixUser.Id.ToString().Trim()==”ADMIN”) inRole=true;

    Sage.Entity.Interfaces.IRole role = Sage.Platform.EntityFactory.GetRepository().FindFirstByProperty(“RoleName”, “Some role”);
    if (role != null)
    {
    foreach (Sage.Entity.Interfaces.IUserRole u in role.Users)
    {
    if (u.User.Id.ToString().Trim() == Sage.SalesLogix.API.MySlx.Security.CurrentSalesLogixUser.Id.ToString().Trim()) inRole = true;
    }
    //inRole = Sage.SalesLogix.API.MySlx.Security.CurrentUserInRole(“ReleaseAdmin”);
    }

    Reply
    • The only problem with this method is that if the group have a lot of user (thousands) every time the role is verified, the system have to pass through all users…
      We had a lot of performance problems by using the CurrentUserInRole function from the SLXAPI, that does something similar.
      I think the best is to query the USERROLE table directly with the ROLEID and the USERID, so you will only get 1 record.

    • Thanks for that tip. Sometimes looping through the entity collections like you mentioned can be problematic for large data sets.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter

Join our mailing list to receive the latest Infor CRM (Saleslogix) and Creatio (bpm'online) news and product updates!

You have Successfully Subscribed!