- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2016 08:10 AM
I am needing to hide a variable (that is part of a variable set), unless the current user is a member of a certain group... is this possible?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-22-2016 12:10 PM
Jon,
I finally figured this out, as it was bugging me. One of the things that helps when troubleshooting is using 'Scripts->Background' (you need to have elevated security). I ran a bunch of script tests in Background and realized you can't just pass a string to 'isMemberOf()', it needs to be an object.
As an example, this does not work:
var user = 'fd5c7b034f4c020093bd7d218110c7ee';
if (user.isMemberOf('Service Desk')) {
gs.print(true);
} else {
gs.print(false);
}
It always evaluates to false.
This does work, though:
var ourUser = gs.getUser().getUserByID('fd5c7b034f4c020093bd7d218110c7ee');
if (ourUser.isMemberOf('Service Desk')) {
gs.print(true);
} else {
gs.print(false);
}
This evaluates to true.
So, I modified the script include:
isGrpMember : function() {
var userID = this.getParameter('sysparm_userID');
var groupName = this.getParameter('sysparm_groupName');
var thisUser = gs.getUser().getUserByID(userID);
//gs.log("TM ===> " + thisUser + " | " + groupName, "Q: isGrpMember Ajax");
if (thisUser.isMemberOf(groupName)) {
return true;
} else {
return false;
}
},
Client script:
function onLoad() {
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('MyAjaxUtils');
ga.addParam('sysparm_name', 'isGrpMember');
ga.addParam('sysparm_userID', g_user.userID);
ga.addParam('sysparm_groupName', 'Service Desk');
ga.getXML(hideField);
// Callback function to process the response returned from the server
function hideField(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
if (answer == 'false') {
g_form.setDisplay('field4', false);
}
}
}
This worked for me on a test catalogue item with some fields. Hopefully this works for you as well. I added the 'sysparm_groupName' to make it a little more re-usable; instead of specifying the group in the script include, you can reuse the function in multiple client scripts with different groups passed to the script.
I found this article which had a similar example which someone noted did not work:
Anyways, hope that helps!
Cheers,
Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2016 11:43 AM
I thought I had it using yours as a guide, but I still can't get it to work... did I mess up somewhere?
Script Include:
var hideCCfield = Class.create();
hideCCfield.prototype = Object.extendsObject(AbstractAjaxProcessor, {
userMember: function(){
var userID = this.getParameter('sysparm_userID');
var gsp = new GlideRecord('sys_user');
gsp.addQuery('sys_id', userID);
gsp.query();
if(gsp.isMemberOf('All Managers')){
return true;
}
},
type: 'hideCCfield'
});
Catalog Client Script:
function onLoad() {
var ajax = new GlideAjax('hideCCfield');
ga.addParam('sysparm_name', 'userMember');
ga.addParam('sysparm_userID', g_user.userID);
ajax.getXML(myResponse);
function myResponse(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer != true){
g_form.setDisplay("charge_to_cost_center",false);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2016 11:50 AM
Looks close; can you try using 'alert' and log statements to see if your variables are correct?
Script include:
gs.log("Getting user record for " + userID); <- check the system logs
Client script:
alert(answer); -> should be the true/false.
Cheers,
Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2016 12:56 PM
The alert was returning 'null'...
I noticed I goofed on lines 2 and 5 of the client script, the var was referred to as 'ajax' instead of 'ga'.
I fixed that.
However, the alert is now returning 'false' for both scenarios. I have double (and triple) checked the group name, and even tried using a sys id instead of the name... still returned false for both...
if statement:
if(gsp.isMemberOf('All Managers')) {
return true;
} else {
return false;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2016 01:02 PM
Did you add the log statement to the script include? Check to make sure it's querying the correct user.
It usually ends up being small things like typos and what not that take the most time to figure out :S
Cheers,
Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2016 01:19 PM
Yeah, the log statement is listing the sys ID of the logged in user (or the person I am impersonating)