Magento: Detect if admin is logged in in frontend pages

后端 未结 12 873
轻奢々
轻奢々 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:53

    Christoph Peters posted a link which solved my problem (Detect if admin is logged in in frontend pages):

    //check if adminhtml cookie is set
    if(array_key_exists('adminhtml', $_COOKIE)){
       //get session path and add dir seperator and content field of cookie as data name with magento "sess_" prefix
       $sessionFilePath = Mage::getBaseDir('session').DS.'sess_'.$_COOKIE['adminhtml'];
       //write content of file in var
       $sessionFile = file_get_contents($sessionFilePath);
    
       //save old session
       $oldSession = $_SESSION;
       //decode adminhtml session
       session_decode($sessionFile);
       //save session data from $_SESSION
       $adminSessionData = $_SESSION;
       //set old session back to current session
       $_SESSION = $oldSession;
    
       if(array_key_exists('user', $adminSessionData['admin'])){
          //save Mage_Admin_Model_User object in var
          $adminUserObj = $adminSessionData['admin']['user'];
          echo 'ADMIN USER IS LOGGED IN';
       }
       else
       {
          echo 'ADMIN USER IS NOT LOGGED IN'
       }
    }
    

    Thank you very much Christoph Peters!

提交回复
热议问题