我编写了一个模块,其行为应与内联翻译相同:
如果我是管理员并且我已登录,我可以在前端看到一些特别的内容。但如果我不是管理员,那么我什么也看不见。
如何让模块知道在前端区管理员登录?
更新1
澄清一些我想描述的模块行为:
后端 - 只需保存以配置一个值的表单。
前端 - 观察者,正在与一个事件挂钩。在Observer的功能中,我需要检查是否
当前前端用户已登录adminpanel ,换句话说用户正在观看前端是管理员。
因为通过此检查,我想向管理员用户提供一些基于前端更改的功能。
更新2
问题是我在前端有以下会话($ _ SESSION)转储:
[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。
答案 0 :(得分:6)
我需要在控制器中执行此操作,特别是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();
答案 1 :(得分:2)
上述解决方案不起作用!
这是一个有效的解决方案(它不是那么干净!但是这可以在你的应用程序的任何地方使用phtml视图或模型或控制器或助手!)
$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
答案 2 :(得分:0)
$loggedIn = Mage::getSingleton('admin/session')->getUser()->getId();
答案 3 :(得分:0)
您需要做的是切换会话数据。您可以使用以下代码执行此操作:
$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);
}
答案 4 :(得分:0)
$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.";
}
答案 5 :(得分:0)
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";
}