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

Shivaraj G_
Tera Contributor

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0676913

  1. In the address bar, add/replace the current URI with /impersonate_dialog.do, this directly loads the Impersonate Dialog page.
  2. The last option is to replace the current URI in the address bar with /logout.do, sometimes it is the only option if there is a scripted redirect method in place.