Terminating user session through code once the allocated time expires.

Musab Rasheed
Tera Sage
Tera Sage

Hi,

I have a service catalog user which a user who doesn't have admin role can raise a request and once request is raised user will be provided with admin role automatically for certain duration. moreover once the duration expires then admin role will be automatically revoked but i want to end the session for the logged in user once the duration expires. can anyone provide me the sample code so that i can it in my workflow which will help me to revoke admin access as well to end user session.?

Thanks

Please hit like and mark my response as correct if that helps
Regards,
Musab
1 ACCEPTED SOLUTION

Here is working code.



var sessions = GlideSessions.get().getLoggedInSessionList();  


var it = sessions.iterator();  


while(it.hasNext()) {  


        var session = it.next();


  gs.log('musab1');


        // If session is current users session, invalidate the session  


        if (session.getUser() == current.opened_by.user_name) {  


  gs.log('musab2');


                  var httpSession = session.getHS();  


                  httpSession.setAttribute("locked_out", "true");  


        }  


}


Please hit like and mark my response as correct if that helps
Regards,
Musab

View solution in original post

7 REPLIES 7

Julian Hoch
ServiceNow Employee
ServiceNow Employee

This sounds quite dangerous - giving admin rights automatically to end users without an approval does not seem like a good idea.


Can you explain the use case behind this? Perhaps there is a better way to achieve your goals.


Hi Julian,



Here is the scenario.



1) There are users in my company who do not have admin access , they are mostly support people.


2) Sometimes there could be possibility that they might need admin access hence for such users i have developed a service catalog using which they can raise request for admin access.


3) The moment request is raised then they will be granted admin role for certain duration say for one hour.


4) In Catalog UI itself i have provided one field using which user can select number of hours they require admin role hence emails will be automatically triggered when admin access is granted and revoked so they are fully aware about it.


5) Now what i want to achieve is that the moment allocated time expires then i want to end their session. i want to do this in workflow as I'm revoking their admin access from workflow itself.


Please hit like and mark my response as correct if that helps
Regards,
Musab

What is the specific action they need admin access for? Perhaps it's better to create an access concept that allows them to perform only these specific actions directly (or via an elevated role) or using automated change requests, rather than giving them complete admin rights.


Requirement itself is like that and they need admin access for doing development work. if you have any sample code kindly share it


Please hit like and mark my response as correct if that helps
Regards,
Musab