- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2015 04:48 PM
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
Solved! Go to Solution.
- Labels:
-
User Interface (UI)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2015 06:01 PM
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);
}
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2015 05:00 PM
I would also like to add that *strangely enough* when using the code:
var g_user = ((g_user == null ) ? getActiveUser() : g_user);
alert ( g_user.hasRole('custom_role') );
Returns true/false ONLY when this UI script runs from the admin interface. Even stranger, g_user.userID returns undefined!
When this UI script runs from the ESS, even calling getActiveUser() does not populate the g_user object. hasRole() is undefined when running on the ESS along with all other properties/methods.
Just figured I'd add that in there...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2015 05:33 PM
I think as a workaround you could use a GlideAjax call and return whatever you need.
Have not personally tried it but should be possible
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2015 06:01 PM
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);
}
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2015 08:05 AM
Alexander,
I would like to thank you for your solution - it works perfectly.
One caveat to anybody who needs to use this, if you are 'impersonating' a user - this will not work. This will pull roles directly related to your session only.
The script can probably be modified to account for this somehow - but it works as it is now.
Thanks for the help!