Getting a user object in a UI script

Kyle Ketterer1
Giga Contributor

Hello all,

I understand that UI scripts are not *recommended* at this point, however, for what I need to accomplish I believe it's the only way to get it done.

Basically, I need to detect a user's groups in a UI script and hide certain elements on any given page based on the user's groups. These elements are html objects contained within content blocks. Therefore, attaching roles to content blocks will not accomplish what I need to.

It appears that in my UI script the g_user object is not yet defined. Even when calling getActiveUser() it doesn't appear to create the object. g_user.userID returns undefined at that point.

The end goal is for me to be able to call g_user.hasRole() - or at the very least query sys_user_grmember using g_user.userID. I am aware of the possible overhead here, however, I feel like user object/data should be baseline available to us even when using deprecated UI scripts.

Is there any workaround for this issue? I am running Eureka.

Thanks

1 ACCEPTED SOLUTION

Ok, tested this in a demo instance...



UI Script


userHasRole('admin');


function userHasRole(role){


  var ga = new GlideAjax('CheckUserRole');


  ga.addParam('sysparm_name','u_hasRole');


  ga.addParam('sysparm_role',role);


  ga.getXMLWait();


  alert(ga.getAnswer());


}



Script Include


Name: CheckUserRole


Client Callable: Checked


var CheckUserRole = Class.create();


CheckUserRole.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  u_hasRole: function() {


  var role = this.getParameter("sysparm_role");


  return gs.hasRole(role);


  }


});


View solution in original post

12 REPLIES 12

Hey Scott,

 

window.NOW.user.roles doesn't work in portal, any guidance?

try this

from snprotips:

find_real_file.png

syed_faheem
ServiceNow Employee
ServiceNow Employee

You can use top.window.NOW object which has many different useful attribute as below:

top.window.NOW.instance_name

top.window.NOW.page_id

top.window.NOW.portal_id

top.window.NOW.portal_url_suffix

top.window.NOW.user_display_name

top.window.NOW.user_id

top.window.NOW.user_id_hashed

top.window.NOW.user_impersonating

top.window.NOW.user_initials

top.window.NOW.user_name

 

Cheers

Syed.