server equivalent of hasroleExactly

sreedharkaliset
Mega Expert

Hi,

Is there a server equivalent of g_user.hasRoleExactly

gs.hasRole returns true for admin and userHasRoleExactly works only client side

 

Regards,

Sreedhar

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Sreedhar,

you don't have server side function for that; use below code and store it in some script include; then you can call this and re-use it like from every server side script

hasRoleExactlyServerSide: function(){

var au = new ArrayUtil();
var roles = gs.getSession().getRoles() + ''; 
var roleArray = roles.split(","); 
var isRolePresent = au.contains(roleArray, role); 

return isRolePresent;

}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

10 REPLIES 10

this is also checking contains by getting the index of the role and is not checking the 'exactly' validation for the role  

so the problem with indexOf, is that it don't natively understand partial names overlaps.
e.g. a roll called xyz and a roll called xyz_user
if xzy is what you looked for and the user had xyz_user it would return true whan you didn't want it to.
still a good solution, but require naming convention.
or or something like (...getRoles().toString() + ',').indexOf (roleName+',')

au.contains(roleArray, role); 


We are checking contains here, it is similar to hasRole and not hasRoleExactly()


If Manager has Assessor role.

and I need to put validation for hasRoleExactly('Assessor'), contains method will give True for a user who is Manager.

Did anyone find any other way ?

I tried putting inherit=false as additional condition in query while fetching list of roles and it is not always the case when using Groups to assign roles to user.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Sreedhar,

Can you also mark answer as helpful.

Regards

Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Tanya Rathor1
Tera Contributor

Hi,  

 As per the solution it should be  > -1 in below code instead of > 0

    if (gs.getSession().getRoles().toString().indexOf(roleName)>0)

 

It should be     if (gs.getSession().getRoles().toString().indexOf(roleName) > -1)