3

I write a module, which should behave in them same manner as inline translation:

If I am admin and I am logged in, I can see something special on frontend. But if I am not admin, then I can't see anything.

How to let module know that on frontend area admin is logged in?

UPDATE 1

To clarify things I want to describe a little bit my modules behaviour:

Backend - form which simply saves to config one value.

Frontend - Observer, which is hooking to an event. In Observer's function I need to check if

current frontend user is logged into adminpanel, in another words user watching frontend is admin .

Because with this check I want to provide to admin user some functionality based on frontend changing.

UPDATE 2

The thing is that I have following session($_SESSION) dump on frontend:

    [core] => Array
    (
        [_session_validator_data] => Array
            (
                [remote_addr] => ...
                [http_via] => 
                [http_x_forwarded_for] => 
                [http_user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:8.0.1) Gecko/20100101 Firefox/8.0.1
            )

        [session_hosts] => Array
            (
                [...] => 1
            )

        [messages] => Mage_Core_Model_Message_Collection Object
            (
                [_messages:protected] => Array
                    (
                    )

                [_lastAddedMessage:protected] => 
            )

        [just_voted_poll] => 
        [visitor_data] => Array
            (
                [] => 
                [server_addr] => ...
                [remote_addr] => ...
                [http_secure] => 
                [http_host] => ...
                [http_user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:8.0.1) Gecko/20100101 Firefox/8.0.1
                [http_accept_language] => ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3
                [http_accept_charset] => windows-1251,utf-8;q=0.7,*;q=0.7
                [request_uri] => ...
                [session_id] => bb397df22a3410ccb07e567cb0333985
                [http_referer] => ...
                [first_visit_at] => 2011-12-01 09:17:48
                [is_new_visitor] => 
                [last_visit_at] => 2011-12-01 13:21:38
                [visitor_id] => 1536
                [last_url_id] => 2408
            )

        [last_url] => ...
    )



    [admin] => Array
    (
        [_session_validator_data] => Array
            (
                [remote_addr] => ...
                [http_via] => 
                [http_x_forwarded_for] => 
                [http_user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:8.0.1) Gecko/20100101 Firefox/8.0.1
            )

        [session_hosts] => Array
            (
                [...] => 1
            )

    )


    [adminhtml] => Array
    (
        [_session_validator_data] => Array
            (
                [remote_addr] => ...
                [http_via] => 
                [http_x_forwarded_for] => 
                [http_user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:8.0.1) Gecko/20100101 Firefox/8.0.1
            )

        [session_hosts] => Array
            (
                [...] => 1
            )

    )

Perhaps there is no way to access admin users from frontend. Well I try via Cookies.

Jevgeni Smirnov
  • 3,787
  • 5
  • 33
  • 50
  • Is your admin secure? If it starts with a `https://` then it is effectively a different domain from the rest of the site which is `http://`, that means a different cookie so it may not always be possible to detect the admin session from the frontend. Magento's workaround for this is to use the IP address instead of cookies, which is why the dev section in config allows for IP addresses to be input. – clockworkgeek Dec 01 '11 at 12:51
  • Nope same thing. http:// always used as for backend so as for frontend – Jevgeni Smirnov Dec 01 '11 at 13:00

6 Answers6

6

I needed to do this in a controller, specifically the preDispatch() method, which I think is a sensible use case.

// Ensure we're in the admin session namespace for checking the admin user..
Mage::getSingleton('core/session', array('name' => 'adminhtml'))->start();

$admin_logged_in = Mage::getSingleton('admin/session', array('name' => 'adminhtml'))->isLoggedIn();

// ..get back to the original.
Mage::getSingleton('core/session', array('name' => $this->_sessionNamespace))->start();
Nick
  • 6,967
  • 2
  • 34
  • 56
  • See my question about this: http://stackoverflow.com/questions/17428489/magento-show-product-attribute-on-frontend-for-backend-admin-or-specific-user By the way, @Dreadedsemicolon, if this solution works, why not approve the answer? – SPRBRN Jul 02 '13 at 14:40
  • This works. Take note it sets the cookie as 'frontend'. – Corgalore Aug 26 '14 at 16:12
2

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
Meabed
  • 3,828
  • 1
  • 27
  • 37
  • 1
    This doesn't seem to work in newer versions. Read just returns false for the supplied adminhtml session id – PanPipes Nov 08 '15 at 21:00
0

What you need to do is switch the session data. You can do this with the following code:

$switchSessionName = 'adminhtml';
$currentSessionId = Mage::getSingleton('core/session')->getSessionId();
$currentSessionName = Mage::getSingleton('core/session')->getSessionName();
if ($currentSessionId && $currentSessionName && isset($_COOKIE[$currentSessionName])) {
    $switchSessionId = $_COOKIE[$switchSessionName];
    $this->_switchSession($switchSessionName, $switchSessionId);
    $whateverData = Mage::getModel('mymodule/session')->getWhateverData();
    $this->_switchSession($currentSessionName, $currentSessionId);
}

protected function _switchSession($namespace, $id = null) {
    session_write_close();
    $GLOBALS['_SESSION'] = null;
    $session = Mage::getSingleton('core/session');
    if ($id) {
        $session->setSessionId($id);
    }
    $session->start($namespace);
}
Moshe Brodsky
  • 305
  • 1
  • 12
0
            $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;
                }
            }

            if($loggedIn)
            {
             echo "LOGGED IN.";
            }else
            {
             echo "not log in.";
            }
0
Mage::getSingleton('core/session', array('name' => 'adminhtml')); 
$adminSession = Mage::getSingleton('admin/session');

if ( $adminSession->isLoggedIn() ) {
   echo "admin is logged in";
} else {
   echo "admin is not logged in";
}
Mukesh Chapagain
  • 25,063
  • 15
  • 119
  • 120
0

$loggedIn = Mage::getSingleton('admin/session')->getUser()->getId();

Max
  • 8,671
  • 4
  • 33
  • 46