Geneva/UI16: Client side check for impersonation

Chris M3
Tera Guru

Has anyone figured out something you can look at on the client side to see if you are impersonating a user?

Previously we used a UI script to look for a particular element, and then made a modification to the DOM to alert the user that they are impersonating.   Now looking at the DOM, I don't see any differences that indicate I am impersonating.

I have also tried using an Ajax call, but I'm getting GlideAjax is not defined

Also getting g_user is not defined

7 REPLIES 7

Jace Benson
Mega Sage

You cannot control what Servicenow provides on the top frame.   However you can use normal javascript.   Connect it to a processor or a rest api and then return who you are impersonating.


Um.   Let me see if I can make it work.


I made a processor to get that data for you.


I tried a scripted rest api, but it wouldnt let me auth to get the data.


//name: user


//type: script


//path: user


//use example


/*


var returnObj;


var client=new XMLHttpRequest();


client.open("get","/user.do");


client.onreadystatechange = function() {


  if(this.readyState == this.DONE) {


              returnObj = JSON.parse(this.response);


  }


};


client.send();


if(returnObj.impersonating){


console.log('You are impersonating ' + returnObj.impersonatee + ' but never forget yourself ' + returnObj.impersonater + '.')


}


*/


(function process(g_request, g_response, g_processor) {




      // Add your code here


      /*global g_request, g_processor*/


      var SNJSON = JSON;


      var json = new SNJSON();


      var returnObj = {


              impersonating: false,


              impersonater: gs.getUserName(),


              impersonatee: gs.getUserName()


      };


      try {




              if (g_request.getMethod() == 'GET') {


                      var myUserObject = gs.getUser();


                      if (gs.getImpersonatingUserName() !== gs.getUserName() && gs.getImpersonatingUserName() !== null) {


                              returnObj.impersonater = gs.getImpersonatingUserName();


                              returnObj.impersonatee = gs.getUserName();


                              returnObj.impersonating = true;


                      }


              }


      } catch (e) {




      }


      g_processor.writeOutput('application/json;charset=utf-8', json.encode(returnObj));




})(g_request, g_response, g_processor);


Thanks, I was afraid that it might be the only way.   Going to wait a bit and see if anyone has any other ideas.


Chris M3
Tera Guru

I wanted to post an update about the solution I went.



Rather than worrying about what other scripts are available in the top window, my Global UI Script does the check in the main window, by making an Ajax call to determine if you are impersonating.   The script then loads itself in the top window only if you are impersonating.



That script, if it sees that the current window is the top window just does the DOM manipulation.



I borrowed some methods from another script I saw posted here to get this to work, along with the custom ajax call.



Re: Geneva UI16 - Thanks for the Update Set Picker, but...