Magento: Detect if admin is logged in in frontend pages

后端 未结 12 623
轻奢々
轻奢々 2021-02-04 00:09

I have created a magento extension. I want to implement access to the extension. The extension creates a page in frontend and i want only admin to access that page. So basically

相关标签:
12条回答
  • 2021-02-04 00:56

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

    $adminhtml  = Mage::getModel('pulsestorm_crossareasession/manager')->getSessionData('adminhtml');
    
    $adminUser = $dataAdminhtml['admin']['user'];
    $loggedIn = $adminUser->getId() && $adminUser->getIsActive();
    
    0 讨论(0)
  • 2021-02-04 00:59

    This code will works

    //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 "404 page not found";
    }
    

    OR

    $adminsession = Mage::getSingleton('admin/session', array('name'=>'adminhtml'));
    
    if($adminsession->isLoggedIn()) {
        //do stuff
    } else {
        echo "404 page not found";
    }
    

    Did you try to dump the $_SESSION variable? Maybe it will help you get on the right track.

    0 讨论(0)
  • 2021-02-04 01:05

    Apart from trying to pull session id from adminhtml cookie, which may or may not work IMHO is better just to "skin" page you need to show to look like its in frontend and use admin controller so it will run under admin session.

    Another solution is to "copy" customer from admin to frontend and log them in before hitting your page and then its the matter of just checking if logged in customer is member of some group.

    0 讨论(0)
  • 2021-02-04 01:07

    The above solutions doesn't work!

    Here is a solution that works ( its not that clean ! but this will work anywhere in your application in phtml view or model or controller or helper ! )

    $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
    
    0 讨论(0)
  • 2021-02-04 01:10

    If you are using cm redis session try this: (worked for me)

    $sesId = isset($_COOKIE['adminhtml']) ? $_COOKIE['adminhtml'] : false ;
    if($sesId){
        $session = Mage::getSingleton('core_mysql4/session')->read($sesId);
    }
    $loggedIn = false;
    if($session)
    {
        if(stristr($session,'Mage_Admin_Model_User'))
        {
            $loggedIn = true;
        }
    }
    
    var_dump($loggedIn);
    

    because if you are using cm redis its rewrites db session module with its own model.

    0 讨论(0)
  • 2021-02-04 01:12
    require_once $dir.'app/Mage.php';
    umask(0);
    
    $apps = Mage::app('default');
    Mage ::getSingleton('core/session', array('name'=>'adminhtml'));
    $adminSession = Mage::getSingleton('admin/session');
    $adminSession->start();
    if ($adminSession->isLoggedIn()) {
       //echo "logged in";
    } 
     else { 
          //echo "Not logged in";
          exit();
     }?> 
    
    0 讨论(0)
提交回复
热议问题