Magento 检查前端是否有管理员登录
我编写了一个模块,其行为方式应与内联翻译相同: 如果我是管理员并且已经登录,我可以在前端看到一些特别的东西。但是如果我不是管理员,那么我什么都看不见 如何让模块知道前端区域管理员已登录 更新1 为了澄清问题,我想稍微描述一下我的模块行为: 后端-只保存一个值的表单 前端-观察者,它正在连接到事件。在Observer函数中,我需要检查 当前的前端用户登录到adminpanel,换句话说,观看前端的用户是admin 因为有了这个检查,我想向管理员用户提供一些基于前端更改的功能 更新2 问题是我在前端有以下会话($\会话)转储:Magento 检查前端是否有管理员登录,magento,Magento,我编写了一个模块,其行为方式应与内联翻译相同: 如果我是管理员并且已经登录,我可以在前端看到一些特别的东西。但是如果我不是管理员,那么我什么都看不见 如何让模块知道前端区域管理员已登录 更新1 为了澄清问题,我想稍微描述一下我的模块行为: 后端-只保存一个值的表单 前端-观察者,它正在连接到事件。在Observer函数中,我需要检查 当前的前端用户登录到adminpanel,换句话说,观看前端的用户是admin 因为有了这个检查,我想向管理员用户提供一些基于前端更改的功能 更新2 问题是我在前端
[core] => Array
(
[_session_validator_data] => Array
(
[remote_addr] => ...
[http_via] =>
[http_x_forwarded_for] =>
[http_user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:8.0.1) Gecko/20100101 Firefox/8.0.1
)
[session_hosts] => Array
(
[...] => 1
)
[messages] => Mage_Core_Model_Message_Collection Object
(
[_messages:protected] => Array
(
)
[_lastAddedMessage:protected] =>
)
[just_voted_poll] =>
[visitor_data] => Array
(
[] =>
[server_addr] => ...
[remote_addr] => ...
[http_secure] =>
[http_host] => ...
[http_user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:8.0.1) Gecko/20100101 Firefox/8.0.1
[http_accept_language] => ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3
[http_accept_charset] => windows-1251,utf-8;q=0.7,*;q=0.7
[request_uri] => ...
[session_id] => bb397df22a3410ccb07e567cb0333985
[http_referer] => ...
[first_visit_at] => 2011-12-01 09:17:48
[is_new_visitor] =>
[last_visit_at] => 2011-12-01 13:21:38
[visitor_id] => 1536
[last_url_id] => 2408
)
[last_url] => ...
)
[admin] => Array
(
[_session_validator_data] => Array
(
[remote_addr] => ...
[http_via] =>
[http_x_forwarded_for] =>
[http_user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:8.0.1) Gecko/20100101 Firefox/8.0.1
)
[session_hosts] => Array
(
[...] => 1
)
)
[adminhtml] => Array
(
[_session_validator_data] => Array
(
[remote_addr] => ...
[http_via] =>
[http_x_forwarded_for] =>
[http_user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:8.0.1) Gecko/20100101 Firefox/8.0.1
)
[session_hosts] => Array
(
[...] => 1
)
)
也许无法从前端访问管理员用户。我试着通过Cookies来实现。
$loggedIn=Mage::getSingleton('admin/session')->getUser()->getId()代码>我需要在控制器中执行此操作,特别是preDispatch()
方法,我认为这是一个合理的用例
// Ensure we're in the admin session namespace for checking the admin user..
Mage::getSingleton('core/session', array('name' => 'adminhtml'))->start();
$admin_logged_in = Mage::getSingleton('admin/session', array('name' => 'adminhtml'))->isLoggedIn();
// ..get back to the original.
Mage::getSingleton('core/session', array('name' => $this->_sessionNamespace))->start();
您需要做的是切换会话数据。可以使用以下代码执行此操作:
$switchSessionName = 'adminhtml';
$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);
}
上述解决方案不起作用
这是一个有效的解决方案(它并没有那么干净!但在phtml视图、模型、控制器或助手中的应用程序中的任何位置都可以使用!)
对非对象调用成员函数getId()。这意味着没有用户,但我已登录。您的管理员安全吗?如果它以一个https://
开头,那么它实际上是一个与站点其余部分http://
不同的域,这意味着不同的cookie,因此可能无法始终从前端检测到管理会话。Magento的解决方法是使用IP地址而不是Cookie,这就是为什么config中的dev部分允许输入IP地址的原因。http://always used as for backend至于frontendSee我的问题是:顺便说一句,@DrearedSemicolon,如果这个解决方案有效,为什么不批准这个答案呢?这很有效。请注意,它将cookie设置为“前端”。这在新版本中似乎不起作用。Read just针对提供的adminhtml会话id返回false
$sesId = isset($_COOKIE['adminhtml']) ? $_COOKIE['adminhtml'] : false ;
$session = false;
if($sesId){
$session = Mage::getSingleton('core/resource_session')->read($sesId);
}
$loggedIn = false;
if($session)
{
if(stristr($session,'Mage_Admin_Model_User'))
{
$loggedIn = true;
}
}
var_dump($loggedIn);// this will be true if admin logged in and false if not
$sesId = isset($_COOKIE['adminhtml']) ? $_COOKIE['adminhtml'] : false ;
$session = false;
if($sesId){
$session = Mage::getSingleton('core/resource_session')->read($sesId);
}
$loggedIn = false;
if($session)
{
if(stristr($session,'Mage_Admin_Model_User'))
{
$loggedIn = true;
}
}
if($loggedIn)
{
echo "LOGGED IN.";
}else
{
echo "not log in.";
}
Mage::getSingleton('core/session', array('name' => 'adminhtml'));
$adminSession = Mage::getSingleton('admin/session');
if ( $adminSession->isLoggedIn() ) {
echo "admin is logged in";
} else {
echo "admin is not logged in";
}