如何在自定义路由上验证用户是否为管理员或客户的匹配功能

时间:2014-05-21 13:44:52

标签: php magento session controller routes

我需要知道访问特定路线的人是否已被记录,如果是客户或管理员用户,任何想法如何在匹配功能controllador路线上执行此操作?

自定义路由控制器的代码:

class Ceicom_Boleto_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract
{
    public function initControllerRouters($observer)
    {
        $front = $observer->getEvent()->getFront();
        $boleto = new Ceicom_Boleto_Controller_Router();
        $front->addRouter('boleto',$boleto);
    }
    public function match(Zend_Controller_Request_Http $request)
    {
       /*
         if is admin and is logged
       */
       Mage::app()->getFrontController()->getResponse()
                  ->setRedirect("/boleto/admin/view/")
                  ->sendResponse();
       exit;
      /*
         if is user and is logged
      */
       Mage::app()->getFrontController()->getResponse()
                  ->setRedirect("/boleto/user/view/")
                  ->sendResponse();
       exit;

    }
}

2 个答案:

答案 0 :(得分:1)

看看@ Magento: Detect if admin is logged in in frontend pages

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

假设您使用的是基于数据库的会话

答案 1 :(得分:1)

试试这段代码:

class Ceicom_Boleto_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract
{
    public function initControllerRouters($observer)
    {
        $front = $observer->getEvent()->getFront();
        $boleto = new Ceicom_Boleto_Controller_Router();
        $front->addRouter('boleto',$boleto);
    }
    public function match(Zend_Controller_Request_Http $request)
    {
       /*
         if is admin and is logged
       */
        //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()){

           Mage::app()->getFrontController()->getResponse()
                      ->setRedirect("/boleto/admin/view/")
                      ->sendResponse();
           exit;
       }

      /*
         if is user and is logged
      */
      if(Mage::getSingleton('customer/session')->isLoggedIn()){
           Mage::app()->getFrontController()->getResponse()
                      ->setRedirect("/boleto/user/view/")
                      ->sendResponse();
           exit;
       }

    }
}

希望这有帮助! 一切顺利:)