Is there a way to hide/show a variable based on the current user's group?

jonb
Tera Contributor

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?

1 ACCEPTED SOLUTION

Community Alums
Not applicable

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:



ismemberOf question



Anyways, hope that helps!



Cheers,



Tim


View solution in original post

11 REPLIES 11

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);


        }


}


}


Community Alums
Not applicable

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



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;


  }


Community Alums
Not applicable

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


Yeah, the log statement is listing the sys ID of the logged in user (or the person I am impersonating)