0

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;

    }
}
hek2mgl
  • 152,036
  • 28
  • 249
  • 266

2 Answers2

1

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

Community
  • 1
  • 1
MagePal Extensions
  • 17,646
  • 2
  • 47
  • 62
1

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 :)

Lalit Kaushik
  • 1,062
  • 2
  • 12
  • 30