Impersonation in scoped application
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2023 10:56 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2023 11:06 AM - edited 08-10-2023 11:24 AM
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().
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2023 12:01 PM
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.
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.
Regards,
Riya Verma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2023 06:58 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2023 07:41 AM
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