Best practice forum (Archived)

How to get ID of admin logged in as another user

 
John Unnever
How to get ID of admin logged in as another user
door John Unnever - Saturday, 9 January 2016, 08:23 AM
Groep Partners

I know you can use the global USER variable to get information based on who is currently logged in, but how can i get information on the ADMIN who is "logged in as" a user?   I'm doing some custom development and we have a situation where our admins might need to log in as a user and perform some actions on their behalf, and i need to record which admin is performing the task when i insert the record of action in the db.

I'm assuming this information is available considering when you log in as another user, it shows both user account names in the upper right user menu, i'm just not sure how i should access it.

Thanks for your help!!

Simon Coggins
Re: How to get ID of admin logged in as another user
door Simon Coggins - Monday, 11 January 2016, 10:55 AM
Groep Totara

Looking at the login_info() function in lib/outputrenderers.php it looks like there are two methods of interest to you:

\core\session\manager::is_loggedinas()

\core\session\manager::get_realuser();

These are both defined in the file lib/classes/session/manager.php.

Simon

John Unnever
Re: How to get ID of admin logged in as another user
door John Unnever - Monday, 11 January 2016, 20:05 PM
Groep Partners

Thanks Simon..  you're a wealth of information!  Looks like i can grab  the user full name pretty easily..  gonna try and figure out how to get the userid since that might be better for getting an audit log.