0

I'm trying to create a script then trigger it from time to time to check "who" is logged in in admin (users), so I created this:

$adminUser = Mage::getModel('admin/user');
$users = $adminUser->getCollection()->load();
echo '<pre>';
foreach($users as $user){
$name = $user->getUsername();
Mage::getSingleton('core/session', array('name'=>$name));
    if(Mage::getSingleton('admin/session')->isLoggedIn()){
        echo $name.' is logged in<br/>';
    }else{
        echo " go away bad boy<br/>";
    }
}
echo '</pre>';

?>

Simplified code .... still no positive results ....

13
  • You want to get the backend user that is currently logged in or all backend users that are currently logged in? All logged in, right? Feb 2, 2018 at 12:27
  • Hello Christoph, yes, I would like to check if any of the users is logged in into backend. As you can see I'm getting whole collection of users then looping trough but ..... it does not work.
    – Rob D. A.
    Feb 2, 2018 at 12:33
  • Actually I don't think this is possible (though I don't want to discourage you quite yet). However, one of the main issues could be the question of how Magento should figure out whether a user is actually still logged in if s/he isn't doing anything in the backend? I mean to say, what if for example the backend user clears its cookies... Magento would never know (at least not until the user logs in again which should give you a time stamp if I'm not wrong). That being said I believe there are just two options: comparing login time stamps and just assume a user is logged in, or ... Feb 2, 2018 at 12:40
  • 1
    controller_action_predispatch that would be like the most classy event to watch out for (but be aware it is used in frontend and backend) Feb 2, 2018 at 12:54
  • 1
    yes, you should be able to do something like this in the observer: Mage::getSingleton('admin/session')->getUser() which gives you the current user. You should make sure however, that all your logging is happening if it is an adminhtml interaction and only then check for the current user. About your other approach, yes that can work out as well, you'd just need to figure first of all, what you actually want to know and track from your admins ([possibly] active session/login, actual interaction, idle state, ...) Feb 2, 2018 at 13:07

1 Answer 1

3

Use below code:

//get the admin session
Mage::getSingleton('core/session', array('name'=>'adminhtml'));

//verify if the user is logged in to the backend
if(Mage::getSingleton('admin/session')->isLoggedIn()){
  //do stuff
}
else
{
  echo "go away bad boy";
}
2
  • Thanx but you just gave me the same code as I already have and it does actually the same ..... don't work. I have a whole collection loaded, then looping trough to check if user/users are logged in but as a result I'm getting 'user not logged in' even if I'm ;(
    – Rob D. A.
    Feb 2, 2018 at 11:59
  • Slightly modified/simplified code but it still does not work
    – Rob D. A.
    Feb 2, 2018 at 12:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.