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

Community Alums
Not applicable

Yes; you can do this using GlideAjax. The first example harishkumar posted above points in the right direction. Getting the user object, passing the info to the client script and using the 'g_form.setDisplay' function.


I still can't get it to work (any ideas?), here is what I have so far:


Script Include (client callable, all application scopes):


var hideCCfield = Class.create();


hideCCfield.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  userMember: function(){


  if(gs.getUser().isMemberOf('All Managers'))


  return true;


  else


  return false;


  },


      type: 'hideCCfield'


});




Catalog Client Script (onLoad):


function onLoad() {


var ajax = new GlideAjax('hideCCfield');


ajax.getXML(myResponse);



function myResponse(response){


        var answer = response.responseXML.documentElement.getAttribute("answer");


        if(answer == false){


            g_form.setDisplay("charge_to_cost_center",false);


        }


}


}


Community Alums
Not applicable

Close; you're missing some key pieces, though. You're not passing the variables and parameters between the scripts. Here's an example of a script I used to query the user record and check their employee level (custom field).



Script include function:



getUserLevel : function() {


  var userID = this.getParameter('sysparm_userID');


  //gs.log("TM===> Getting user record for " + userID);


  var gsp = new GlideRecord('sys_user');


  gsp.addQuery('sys_id', userID);


  gsp.query();


  if(gsp.next()){


  return gsp.u_employee_level;


  }


  },



Client script:



function onLoad() {


  //Type appropriate comment here, and begin script below


  var ga = new GlideAjax('MyAjaxUtils');


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


  ga.addParam('sysparm_userID', g_user.userID);


  ga.getXML(parseLevel);



  function parseLevel(response) {



  var answer = response.responseXML.documentElement.getAttribute("answer");


  if (answer < 4) {


  g_form.setDisplay('surface_pro_3', false);


  }


  //alert("User is level " + answer);


  }


}



So, you need to first call the function in the client script (in your case, ga.addParam('sysparm_name', 'userMember'), then pass the userID from the form back to the script include.



Hope this makes sense!



Cheers,



Tim