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
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();
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.
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.
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
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.
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();
}?>