php Magento:如何检查管理员是否在模块控制器中登录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3342165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:24:41  来源:igfitidea点击:

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

phpzend-frameworksessionmagento

提问by Calua

I'm creating a Magento Module. Within the controller, I want to check if an admin is logged in or not. So the controller only will be accessible if there is a logged in admin.

我正在创建一个 Magento 模块。在控制器中,我想检查管理员是否已登录。因此,只有在有登录管理员的情况下才能访问控制器。

I'm trying to use this code on my controller.

我正在尝试在我的控制器上使用此代码。

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

// Use the 'admin/session' object to check loggedIn status
if ( $session->isLoggedIn() ) {
   echo "logged in";
} else {
   echo "not logged in";
}

but I always get "not logged in", even if I'm already logged in to the magento admin.

但我总是“未登录”,即使我已经登录到 magento 管理员。

Can anybody help me to resolve this issue?? any help will be much appreciated. Thanks

有人可以帮我解决这个问题吗??任何帮助都感激不尽。谢谢

回答by silvo

That is really strange. I use almost exactly the same code and it works all the time:

这真的很奇怪。我使用几乎完全相同的代码并且它一直有效:

//get the admin session
Mage::getSingleton('core/session', array('name'=>'adminhtml'));

//verify if the user is logged in to the backend
if(Mage::getSingleton('admin/session')->isLoggedIn()){
  //do stuff
}
else
{
  echo "go away bad boy";
}

Did you try var_dumping the $session variable? Maybe it will help you get on the right track...

您是否尝试过 var_dumping $session 变量?也许它会帮助你走上正轨......

回答by Calua

Make sure that your module's adminhtml controller is extending Mage_Adminhtml_Controller_Action. You can't check if an admin is logged in from a front end controller.

确保您的模块的 adminhtml 控制器正在扩展 Mage_Adminhtml_Controller_Action。您无法检查管理员是否从前端控制器登录。

回答by Sumith Harshan

$user = Mage::getSingleton('admin/session');

$user = Mage::getSingleton('admin/session');

if($user->getUser()->getUserId()) {
   // admin logged
}
else {
   // not admin logged
}

回答by Roman Snitko

As David Tay said, you should extend your controller from Mage_Adminhtml_Controller_Action.
Anyway, the shortest way to check if admin is logged in is to call this helper method:

正如 David Tay 所说,您应该从Mage_Adminhtml_Controller_Action.
无论如何,检查管理员是否登录的最短方法是调用这个辅助方法:

Mage::helper('adminhtml')->getCurrentUserId();

回答by Kevin

there is a new magento module, written by alan storm: https://github.com/astorm/Magento_CrossAreaSessions

有一个新的 magento 模块,由 alan Storm 编写:https: //github.com/astorm/Magento_CrossAreaSessions