Ending impersonations via Service Portal

larstange
Mega Sage

Hi All

I just want to share a little feature I made.

We have enabled the automatic redirect of users without roles to the Service Portal - Redirect to Service Portal after login

This however has a conquence when impersonating an end user - you will not be able to get back to the normal UI to end the impersonation without first logging out of the syste.

This could be quite annoying when performing tests in the system where you need to verify someting in the Service Portal as an end user.

So I have created a new menu item in the header menu, to end any active impersonation. It ends the impersonation and reloads the service portal as "your self".

find_real_file.png

The feature is added to a modified version of the Stock Header.

Service Script

//Check if we are impersonating a user

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

  data.is_impersonating = true;

  data.impersonating = gs.getImpersonatingUserName();

}

else

  data.is_impersonating = false;

Client Script

$scope.impersonate = function(user_name) {

  if (!user_name)

            return;

  $http.post('/api/now/ui/impersonate/' + user_name, {}).success(function() {

            $scope.show_error = false;

            window.location = "/sp";

            }).error(function(response) {

            if (response.error) {

                    $scope.show_error = true;

                      $scope.error = response.error;

            $log.error("Impersonate failed", response.error);

            }

  });

}

New line added to the div containing the dropdown menu (code below does not generate the menu shown in my screen shot but is taken from the Stock Header widget)

<ul class="dropdown-menu">

  <li><a ng-href="?id=user_profile&sys_id={{::user.sys_id}}">${Profile}</a></li>

  <li><a href="{{::portal.logoutUrl}}">${Logout}</a></li>

  <!-- Line below is the new line to add -->

  <li><a ng-if="data.is_impersonating" ng-click="impersonate(data.impersonating)"><i class="fa fa-reply" style = "margin-right: 5px;"></i>${End Impersonation}</a></li>

</ul>

15 REPLIES 15

doombringer
Kilo Contributor

I'm honestly surprised that this isn't part of the OOB portal yet. 🙂 This is pretty awesome, especially given the number of role-less users I've had to impersonate to debug recently. 

For anyone who's curious: 

You can use this functionality and have it refresh the page you're currently on by using

window.location = $location.$$url;

instead of 

window.location = "/sp";

Cheers!

mhedtke24
Tera Expert

Here is a way you can do this without cloning the stock header.

Create a new widget

Widget Name: Impersonate User Entry

Widget ID: impersonate_user_entry

Server Script:

(function() {
	/* populate the 'data' object */
	/* e.g., data.table = $sp.getValue('table'); */

	//Check if we are impersonating a user
	if (gs.getImpersonatingUserName() !== gs.getUserName() && gs.getImpersonatingUserName() !== null) {
		data.show_menu_entry= true;
		data.impersonating = gs.getImpersonatingUserName();
	}
	else {
		data.show_menu_entry = false;
	}
})();

Link Function:

function(scope) {
	
	var $http = $injector.get('$http');
	var $log = $injector.get('$log');
	
	if(scope.data.show_menu_entry && !$('#end_impersonation')[0]) {
		$('#sp-nav-bar ul li.hidden-xs.dropdown ul.dropdown-menu li:first').after('<li style="cursor: pointer;"><a role="link" id="end_impersonation">${End Impersonation}</a></li>');
		$('#sp-nav-bar ul li.visible-xs-block:first').after('<li class="visible-xs-block" style="cursor: pointer;"><a role="link" id="end_impersonation">${End Impersonation}</a></li>');
	}
	$("#end_impersonation").click(function() {
		if (!scope.data.impersonating)
			return;
		$http.post('/api/now/ui/impersonate/' + scope.data.impersonating, {}).success(function() {
			scope.show_error = false;
			window.location = "/sp";
			
		}).error(function(response) {
			if (response.error) {
				scope.show_error = true;
				scope.error = response.error;
				$log.error("Impersonate failed.", response.error);
			}

		});
	});
}

Then all you need to do is add this widget to the home page of your portal and it will be injected into the menu drop down.

I have also created a post on how to have the full functionality of Impersonate User in the portal here using this method of injecting into the menu and calling on another widget in a modal.

Love it, thank you! I'm really confused why ServiceNow hasn't bothered to implement it officially.

Brian Lancaster
Tera Sage

I created an Idea for this to see if we can get it as OOTB functionality.  I have tried searching for this before an never found this post until today.  Are you still using this?  Have you had any issues after upgrades?

Tushar Walveka2
Tera Guru

Instead of creating a new widget or making any changes in OOB widget, You can do simple hack.

 

Just add $c.do at the end of URL

eg. https://instancename.service-now.com/$c.do

 

Here you go ...............!!!

Even though you don't have any role.. it will redirect you to Standard UI. And you can impersonate.


This will just save your time. You don't need to logout everytime. Also you don't need to make any changes in menu header widget