hasRole in Server and Client

Sachin G K1
Kilo Sage

Hi, 

 

We know that g_user.hasRole('itil'); will return true if user has itil role or admin role. Similarly does, gs.hasRole('itil'); works the same or does it return true only when user has itil role.

1 ACCEPTED SOLUTION

Harish KM
Kilo Patron

Hi @Sachin G K1 both method works the same, the difference is

g_user.hasRole() // client side call

gs.hasRole() // server side call

 

you could also use gs.hasRoleExactly()// it will check for admins as well

View solution in original post

9 REPLIES 9

Hi @Sachin G K1 you can use below code to check for admins in server side, this will return false if admin donot have itil role

    var role= gs.hasRole('itil');
hasRoleExactly(role);// call hasRoleExactly function

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

Harish Bainsla
Kilo Patron

Hi both are different one is client side other one is server side

 g_user.hasRole('itil'); its client side

 gs.hasRole('itil'); its server side for more understanding check below link

https://blog.snowycode.com/post/user-object-cheat-sheet

 

 

SachinGK1_0-1709109022137.png

Hi Harish,

Thanks for response, i went through the link you shared. Please have a look at picture i found there. My queries is simple, does gs.hasRole('itil'), will this return true if user doesn't have itil role but has admin role 

Jyoti Jadhav9
Tera Guru

Hi @Sachin G K1 ,

g_user.hasRole('itil')  >> Returns true if the current user has the specified role or the admin role.

gs.hasRole('itil') >> Return true if the current user has at least one of the passed-in roles. Also returns true if the user has the administrator role.

For more information, you can refer below URLs:

1. https://developer.servicenow.com/dev.do#!/reference/api/utah/client/c_GlideUserAPI

2. https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/server_legacy/c_GlideSystemAPI

 

Please hit the like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

 

Thanks & Regards

Jyoti Jadhav

 

Vaibhav_Nikam
Tera Guru

Hi @Sachin G K1 

 

 g_user.hasRole('itil') & gs.hasRole('itil') will return true if the user has itil role or admin role.

Both of them work similarly as g_user.hasRole() is on the client side & gs.hasRole() is on the server side.

Please mark it helpful, if the answer assists in resolving your question.

 

Best Regards,
Vaibhav Nikam