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

Paul Kunze
Tera Guru

Hi, this is a function which you can put into a Script Include and call from anywhere, also in Scoped Apps:

hasRoleExactly: function(role, ignoreInherited){
	var grUserRoles = new GlideRecord('sys_user_has_role');
	grUserRoles.addQuery('user', gs.getUserID());
	grUserRoles.addQuery('role.name', role);
	if(ignoreInherited) grUserRoles.addQuery('inherited', false);
	grUserRoles.setLimit(1);
	grUserRoles.query();
	return grUserRoles.hasNext();
}

 

You can use it in condition fields like this:

new appUtils().hasRoleExactly('catalog_editor');

 

Or if you want to only check for roles which are directly added and not inherited:

new appUtils().hasRoleExactly('catalog_editor', true);