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

ark6
Mega Guru

Hi Lars,



Very nice. Appreciate it.


Till now I used to navigate to navpage.do from SP page and impersonate back .



Regards,


Ark


Blaze2
Kilo Guru

Can you provide this in share?



I think a lot of people would like to use this


larstange
Mega Sage

I have no plans of doing this.



Sharing modifications to out of the box widgets is not easy, since they are read only and you therefor have to make a clone.


It will then not be possible to provide an update set which just works when imported, but requires further configuration on the instance. This requires some sort of instructions and if the user have already modified their header the code has to be carried over manually anyways.



So a post in the community covers the amount of effort I am prepared to put into sharing this.


lomouhamadou
Kilo Guru

Hello LArs,



Thanks for sharing this.


I just tried it but I it is not ending the impersonation.


From the console, it says this:



find_real_file.png



Can you help me out on this, please ?