How to access user security levels when calling a module python script calls from Designer?

I am able to access user security levels in our module when calling a python script from Perspective.

However, when I call the same script function from the designer script console, SecurityContext.THREAD_LOCAL.get() returns a null value.

var securityContext = SecurityContext.THREAD_LOCAL.get();
if(securityContext == null) { // securityContext is null when called from Designer
    logger.warn("No security context");
} else {
    var levels = securityContext.getSecurityLevels(); // good values when called from Perspective 
    var roles = securityContext.getRoles();
    var zones = securityContext.getSecurityZones();
}

I have also tried accessing user security levels with:

var session = ClientReqSession.get();
var sessionUser = (ClientReqSession.User)session.getAttribute(ClientReqSession.SESSION_USER);
securityContext = SecurityContext.fromAuthenticatedUser(sessionUser.getUser().get());
var levels = securityContext.getSecurityLevels();

But in this case, the assigned security levels in the identity provider user grants screen aren't present in securityContext.getSecurityLevels().

What is the proper way to access a user's security levels when the function is called from Designer?

var session = ClientReqSession.get();
var sessionUser = (ClientReqSession.User)session.getAttribute(ClientReqSession.SESSION_USER);
var levels = sessionUser.getSecurityLevels();
1 Like