gs.hasRoleExactly() in server side script.

Kishor O
Tera Sage

Is the gs.hasRoleExactly() method work in servicenow scripting or do we have any alternate method for this?

1 ACCEPTED SOLUTION

Aman Kumar S
Kilo Patron

Hi @Kishor O 

we have g_user.hasRoleExactly which is only accessible in client side.

gs is accessible only in server side scripting, you need build a explicit logic by yourself for server side script.

function hasRoleExactly(role) {
    var au = new ArrayUtil();
    var roles = gs.getSession().getRoles() + ''; 
    var roleArray = roles.split(","); 
    var isAuthorized = au.contains(roleArray, role); 
    return isAuthorized; 
}
Best Regards
Aman Kumar

View solution in original post

5 REPLIES 5

SanketLandge1
Kilo Guru

Hello @Kishor O 

  • hasRole(' ');
  • hasRoles();
  • hasRoleExactly(' ');
  • hasRoleFromList(ā€˜ ',' ',’ ’);

Now we will discuss one by one about these roles:-

Methods and its Significance

g_user.hasRole(' '):-  this method returns true if the current logged-in user has the role which we have provided in (' ') section., otherwise false.it will always returns true if the user has the 'admin' role.

 

g_user.hasRoles():-     this method  return true if current logged in user has any single role atleast.

 

g_user.hasRoleExactly(ā€˜ ’):- this method return true if current logged in user has role we which have provided in (' ') section.

 

g_user.hasRoleFromList(' ',' ',’ ’):- this method return true if current logged in user has admin role or the role/roles from the list which we have provided in (' ') section.

Check the below screen shot which I have implemented in my PDI. Follow the Same Procedure for better understanding. I have logged in as Admin.

 

For server-side use gs object e.g (gs.hasRole(): )

 

Mark helpful or correct if I answered correctly.

Aman Kumar S
Kilo Patron

Hi @Kishor O 

we have g_user.hasRoleExactly which is only accessible in client side.

gs is accessible only in server side scripting, you need build a explicit logic by yourself for server side script.

function hasRoleExactly(role) {
    var au = new ArrayUtil();
    var roles = gs.getSession().getRoles() + ''; 
    var roleArray = roles.split(","); 
    var isAuthorized = au.contains(roleArray, role); 
    return isAuthorized; 
}
Best Regards
Aman Kumar

Hello @Aman Kumar S ,

 

This  is not working in scoped app. Please help.

 

Thanks

Hi @Ankita Kolhe 
What is your problem statement, if above doesn't work in scoped app, you can fetch the currently logged in user and glide against "sys_user_has_role" table.

Best Regards
Aman Kumar