Include "Impersonate" and "End Impersonation" in Service Portal

heathers_
Kilo Sage

Hello, looking for a way to add "Impersonate" and "End Impersonate" into Service Portal, current version Xanadu. I followed the instructions in How To: Add Impersonate User in Service Portal (using OOB Header Menu) - I verified "impersonate" exists in the Menu drop down, however the modal does not display options.

 

Has anyone achieved this requirement using this method? Is there another way that works?


1. 

heathers__0-1747399464892.png


2. 

heathers__1-1747399464809.png



Widget #1 already exists

Name: Impersonate User

ID: impersonate-user

 

Widget #2

Name: Impersonate User Entry

ID: impersonate_user_entry

 

Server Script:

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

    data.canImpersonate = new GlideImpersonate().canImpersonate(gs.getUserID());
    data.isImpersonating = new GlideImpersonate().isImpersonating();
    data.realUser = gs.getImpersonatingUserName();
    
    // Check if the user has the impersonator role
    var hasImpersonatorRole = gs.hasRole('impersonator');
    
    if ((data.canImpersonate && hasImpersonatorRole) || data.isImpersonating) {
        data.show_menu_entry = true;
    }
    else {
        data.show_menu_entry = false;
    }
    
})();

 

Client Controller:

api.controller=function() {
  /* widget controller */
  var c = this;
};

 

Link:

function link(scope, element, attrs, controller) {

    var $uibModal = $injector.get('$uibModal');

    // Add entry to navbar
    if (scope.data.show_menu_entry && !$('#impersonation')[0]) {
        $('#sp-nav-bar ul li.hidden-xs.dropdown ul.dropdown-menu li:first').after('<li style="cursor: pointer;"><a role="link" id="impersonation">${Impersonate User}</a></li>');
        $('#sp-nav-bar ul li.visible-xs-block:first').after('<li class="visible-xs-block" style="cursor: pointer;"><a role="link" id="impersonation">${Impersonate User}</a></li>');
    }

    $("#impersonation").click(function() {
        $uibModal.open({
            templateUrl: 'impersonate-widget',
            scope: scope
        })
    });
}

 

Angular ng-templates:

ID: impersonate-widget

<div class="panel panel-default">
  <h3 class="padder-md">
      Impersonate User
    </h3>
  <div class="modal-header">
    <widget id="impersonate_user"></widget>
  </div>
</div>



Cloned OOB "Stock Header"

HTML:

<ul class="dropdown-menu" role="menu" aria-label="{{::data.profileBtnMsg}}">
            <li role="presentation"><a tabindex="-1" ng-href="?id=user_profile&sys_id={{::user.sys_id}}" role="menuitem">${Profile}</a></li>
             <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>
            <li ng-if="::!(isViewNative || isViewNativeTablet)" role="presentation"><a tabindex="-1" href="{{::portal.logoutUrl}}" role="menuitem">${Logout}</a></li>

 

Server:

//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:

$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);
        }
    });
}

 

3 REPLIES 3

AshishKM
Kilo Patron
Kilo Patron

Hi @heathers_ ,

Did you check the same drop-down option after impersonating, the "End Impersonation" will display when instance is under 'Impersonate".

 

-Thanks,

AshishKM


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

I'm unable to impersonate thru SP because the modal does not allow me to select a user.

heathers__0-1747401060263.png

 

 

If I impersonate from the classic view (backend), I do see "End Impersonation" but the action does not work.

heathers__1-1747401131119.png

 

 

Marco0o1
Tera Sage

Hi @heathers_ ,

Its problably because the method you are using to call the widget, you are calling as embeded widget but is necessary to call as a modal that will only load when is open and sometimes it crash when you tried to call from a ng-template because the methods using in the ng-template are called from the angular providers.

 

Just try changing something like the OTB solution:

Cloned OOB "Stock Header"

HTML:

<ul class="dropdown-menu" role="menu" aria-label="{{::data.profileBtnMsg}}">
            <li role="presentation"><a tabindex="-1" ng-href="?id=user_profile&sys_id={{::user.sys_id}}" role="menuitem">${Profile}</a></li>



              <li class="header-menu-item" ng-if="data.isimpersonationenabled && (data.canImpersonate || data.isImpersonating)">
        <a tabindex="-1" href="javascript&colon;void(0)" ng-click="impersonate()">${Impersonate}</a>



             <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>
            <li ng-if="::!(isViewNative || isViewNativeTablet)" role="presentation"><a tabindex="-1" href="{{::portal.logoutUrl}}" role="menuitem">${Logout}</a></li>

 

Client Controller:

api.controller=function() {
  /* widget controller */
  var c = this;

    $scope.impersonate = function() {
        spModal.open({
            title: c.data.modalTitle,
            widget: 'impersonate-user',
            scope: scope,
			buttons:[]
        })
    };

};

 

Server:

   data.canImpersonate = new GlideImpersonate().canImpersonate(gs.getUserID());
    data.isImpersonating = new GlideImpersonate().isImpersonating();

 

You really don't need the angular template and get the list from scrath. That already exist and just call the impersonate modal when selecting.