Zachary Schuessler
Zachary Schuessler

Reputation: 3682

Check if Admin is Logged in Within Observer

I'm attempting to check if the administrator is logged in from an observer. The problem is that while this is easy to do when viewing the admin module, viewing the frontend is another story.

There are several similar questions, but unfortunately none of them provide a working solution for Magento 1.6.2.

I wasn't able to successfully get isLoggedIn() to return true in the admin/session class. I also found out that there is a cookie for both frontend and adminhtml, which may help.

The accepted answer in this related question seems to suggest this may not be possible:

Magento - Checking if an Admin and a Customer are logged in

Another related question, with a solution that didn't help my specific case:

Magento : How to check if admin is logged in within a module controller?

Upvotes: 1

Views: 3203

Answers (3)

Moshe Brodsky
Moshe Brodsky

Reputation: 315

It is possible. What you need to do is switch the session data. You can do this with the following code:

  $switchSessionName = 'adminhtml';
  if (!empty($_COOKIE[$switchSessionName])) {
      $currentSessionId = Mage::getSingleton('core/session')->getSessionId();
      $currentSessionName = Mage::getSingleton('core/session')->getSessionName();
      if ($currentSessionId && $currentSessionName && isset($_COOKIE[$currentSessionName])) {
          $switchSessionId = $_COOKIE[$switchSessionName];
          $this->_switchSession($switchSessionName, $switchSessionId);
          $whateverData = Mage::getModel('mymodule/session')->getWhateverData();
          $this->_switchSession($currentSessionName, $currentSessionId);
      }
  }

  protected function _switchSession($namespace, $id = null) {
      session_write_close();
      $GLOBALS['_SESSION'] = null;
      $session = Mage::getSingleton('core/session');
      if ($id) {
          $session->setSessionId($id);
      }
      $session->start($namespace);
  }

Upvotes: 3

Fabian Blechschmidt
Fabian Blechschmidt

Reputation: 4141

Late Answer but if as I found it on google:

This it not possible.

Why? Because the default session name in the frontend is frontend and the session name in the backend is admin. Because of this, the session data of the admin is not available in the frontend.

Upvotes: 1

Oğuz Çelikdemir
Oğuz Çelikdemir

Reputation: 4980

have you tried this :

Mage::getSingleton('admin/session', array('name' => 'adminhtml'))->isLoggedIn();

how about this ( I am not sure it will work or not )

require_once 'app/Mage.php';
umask(0);
$apps = Mage::app('default');
Mage::getSingleton('core/session', array('name'=>'adminhtml'));
$adminSession = Mage::getSingleton('admin/session');
$adminSession->start();
if ($adminSession->isLoggedIn()) {
   // check admin
}

Upvotes: 0

Related Questions