2012-03-11 54 views
1

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

有几个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?

回答

1

迟到的回答,但如果因为我发现它在谷歌:

这是不可能的。

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

+0

我最终加密一个cookie并设置会话在数据库中处理它。你是对的,没有任何运气做任何“官方”的方式。 – 2012-11-04 16:42:47

+0

是的,我们也考虑过这种方式,最终获得了“IP开发人员功能”,就像在magento配置中一样。我认为,如果我们将会话的名称都改为相同的值,那应该没有问题。但这是安全措施吗? – 2012-11-05 10:08:03

+0

我不会将会话更改为相同的,只是因为听起来您可能会遇到无法预料的后果。加密的cookie对我来说非常适合,而且您不必混淆IP。在这里实现:https://github.com/zschuessler/QuarkBar – 2012-11-05 15:45:27

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 
} 
+0

这是在我的一个链接中发布的解决方案。不幸的是,它适用于管理模块,但不适用于前端。 – 2012-03-11 23:18:00

+0

试试这个(http://www.magentocommerce.com/boards/v/viewthread/50307/#t190490) – 2012-03-11 23:20:45

+0

@ZacharySchuessler看看修改后的代码... – 2012-03-11 23:30:20

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); 
    } 
+0

+1表示替代方案,即使它不是我的情况的最佳解决方案。 – 2013-01-16 18:13:27

+0

不错!谢谢你。 – 2013-05-15 20:56:14

+0

如果只是在CMS页面模板上,我将如何获得此工作?不知道如何处理像$ whateverData这样的变量 – joren 2013-05-28 15:06:32