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

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

问题描述:

I needed to know who is accessing a particular route is logged and if it is a customer or admin User, any idea how to do that on match function controllador route?

Code of the custom route controller:

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;

    }
}

我需要知道访问特定路线的人是谁,如果是客户或管理员用户,任何想法 如何在匹配函数controllador路由上做到这一点? p>

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

 类Ceicom_Boleto_Controller_Router扩展Mage_Core_Controller_Varien_Router_Abstract 
 {
公共函数initControllerRouters($ observer)
  {
 $ front = $ observer-> getEvent() - > getFront(); 
 $ boleto = new Ceicom_Boleto_Controller_Router(); 
 $ front-> addRouter('boleto',$ boleto); 
  } 
公共函数匹配(Zend_Controller_Request_Http $ request)
 {
 / * 
如果是admin并且已记录
 * / 
 Mage :: app() - > getFrontController() - > getResponse()  
  - > setRedirect(“/ boleto / admin / view /”)
  - > sendResponse(); 
退出; 
 / * 
如果是用户并且已记录
 * / 
 Mage:  :app() - > getFrontController() - > getResponse()
  - > setRedirect(“/ boleto / user / view /”)
  - > sendResponse(); 
 exit; 
 
  } 
} 
  code>  pre> 
  div>

try this code:

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;
       }

    }
}

Hope this helps! All the best :)

Take a look @ 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);

Assuming that you are using DB based sessions