检查Admin是否登录Observer

时间:2012-03-11 22:55:10

标签: magento

我正在尝试检查管理员是否从观察者登录。问题是虽然在查看管理模块时很容易做到,但查看前端是另一回事。

有几个similar questions,但不幸的是,它们都没有为Magento 1.6.2提供有效的解决方案。

我无法在isLoggedIn()课程中成功获得admin/session返回true。我还发现前端和adminhtml都有一个cookie,这可能有所帮助。

这个相关问题中接受的答案似乎表明这可能是不可能的:

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

另一个相关问题,解决方案对我的具体案例没有帮助:

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

3 个答案:

答案 0 :(得分:3)

有可能。您需要做的是切换会话数据。您可以使用以下代码执行此操作:

  $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);
  }

答案 1 :(得分:1)

延迟回答,但如果我在google上找到它:

这是不可能的。

为什么呢?因为前端中的默认会话名称为frontend,后端中的会话名称为admin。因此,管理员的会话数据在前端不可用。

答案 2 :(得分:0)

你尝试过这个:

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

这个怎么样(我不确定它是否会起作用)

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
}