Impersonation in scoped application

deepak50
Tera Contributor

Hi,

Is there a way to impersonate a user through script in scoped application.

I tried impersonating using both the apis gs.getSession().impersonate and GlideImpersonate but getting error as these functions work only in global scope.

 

Thanks

7 REPLIES 7

Mohith Devatte
Tera Sage

Hello @deepak50 ,

I recently came across a nice "is not allowed in scope" guide.

Follow this article and there is a workaround for impersonation API with name GlideImpersonate.impersonate().

 

https://www.servicenow.com/community/developer-blog/is-not-allowed-in-scope-the-only-workaround-guid...

 

Also follow this link for more explanation of  the  API mentioned in the above approach.

https://developer.servicenow.com/dev.do#!/reference/api/utah/server_legacy/GlideEvaluatorAPI

 

Hope this helps 

Mark my answer correct if this helps you 

Thanks

Riya Verma
Kilo Sage

Hi @deepak50 ,

 

Hope you are doing great.

 

Impersonating users within a scoped application can indeed be challenging, as certain API methods are restricted to the global scope. There's a workaround to achieve impersonation using the GlideImpersonate.impersonate() method, and I'll guide you through the process step by step.

  1. Import the GlideImpersonate API: First, ensure that you import the GlideImpersonate API in your scoped application. This will allow you to use the GlideImpersonate.impersonate() method.

 

var GlideImpersonate = Packages.com.glide.impersonate.impersonate;

 

  • Perform Impersonation: Now you can use the GlideImpersonate.impersonate() method to impersonate a user. Here's how you can do it:

 

// Define the user you want to impersonate by their sys_id
var targetUserSysId = 'USER_SYS_ID_GOES_HERE';

// Perform the impersonation
try {
    GlideImpersonate.impersonate(targetUserSysId);
    // Your code to execute as the impersonated user goes here
} finally {
    // Always revert back to the original user after impersonation
    GlideImpersonate.undo();
}

 

  • It's crucial to ensure that you revert to the original user after completing the operations that need impersonation. This is done using GlideImpersonate.undo().

    The try-finally block ensures that the impersonation is undone even if an exception occurs during the impersonated operations.

 

Getting exception as below :

 

java.lang.SecurityException: GlideImpersonate is not allowed in scoped applications: com.glide.script.fencing.GlidePackageScopeHandler.found(GlidePackageScopeHandler.java:28)

 

Thanks

Deepak

 

 

Hi @Riya Verma   ,

 

I tried the above method for the user impersonation , but didn't work on scoped application , getting error as

 Use of Packages calls is not permitted in scoped applications: 	

could please help me to achieve user impersonate on scoped application with any other alternate method .

 

 

Regards,

Varshini