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

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

Thanks. That got me thinking in the right direction for a local logout page that didn't use logout.do.



<g:evaluate var="jvar_status">


                              var id = gs.getSessionID();


                              var gr = new GlideRecord('v_user_session');


                              if (gr.get('session_id',id)) {


                                                              gr.locked = true;


                                                              gr.update();


                              }


</g:evaluate>    


This doesn't work across nodes.