Suggestion for Impersonation in script

JaineelPetiwale
Tera Contributor

Hi All,

 

Could you help to suggest as a best practice, which function should I use to impersonate in script?

 

Option 1: As per Impersonate a user using a script - Support and Troubleshooting , But seems like this doesn't work in non-interactive session, but not sure.

gs.info(gs.getUserName());

session.onlineImpersonate("local_admin");

// Here you and do whatever you want to
gs.info(gs.getUserName());
// ....
// ....
// ....
// ....
// done

session.onlineUnimpersonate();

 

 Option 2: As per what community suggested, but it's not documented anywhere.

gs.info(gs.getUserName());

var systemUser = gs.getSession().impersonate("local_admin");

// Here you and do whatever you want to
gs.info(gs.getUserName());
// ....
// ....
// ....
// ....
// done

gs.getSession().impersonate(systemUser);

 

Option 3: As per what I got in SNC Docs and little bite help from community

var currentuser = gs.getUserName();

gs.info(gs.getUserName());

var impUser = new GlideImpersonate();

impUser.impersonate("local_admin");

// Here you and do whatever you want to
gs.info(gs.getUserName());
// ....
// ....
// ....
// ....
// done

impUser.impersonate(currentuser);

 

7 REPLIES 7

Bhuvan
Mega Patron

@JaineelPetiwale 

 

For auditing and reporting purposes, execution of a script via impersonation would complicate tracking.

 

Glide Impersonate is used to replicate scenarios to check whether users can execute something and is used for testing & troubleshooting purposes.

 

Depending on your requirements, you can opt for the function that suits you. But make sure you are not using it as part of scheduled jobs or scripts execution.

 

If this helped to answer your query, please mark it helpful & accept the solution.

 

Thanks,

Bhuvan

WillieW
Tera Contributor

@JaineelPetiwale 

// show current user
var usr = gs.getUserName();
gs.info('usr = ' + usr);
// impersonate
session.onlineImpersonate("abel.tuter");
var usr = gs.getUserName();
gs.info('usr = ' + usr);
// end impersonation
session.onlineUnimpersonate();
// show current user
var usr = gs.getUserName();
gs.info('usr = ' + usr);

worked in my instance (run in Scripts - Background).

 

And us a valid 'user_name' value from sys_user table. "local_admin" doesn't exist in my instance.

WillieW
Tera Contributor

Impersonating users

may be helpful.