3

For M1 i had a php file which took care of logging into admin, now i need the same for M2 but i can't get it to work.

This is my external file:

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';

class TestApp
    extends \Magento\Framework\App\Http
    implements \Magento\Framework\AppInterface {

    public function launch()
    {
        $areaCode = 'adminhtml';
        $username = 'admin';
        $this->_state->setAreaCode($areaCode);
        $this->_objectManager->configure($this->_configLoader->load($areaCode));

        /** @var \Magento\User\Model\User $user */
        $user = $this->_objectManager->get('Magento\User\Model\User')->loadByUsername($username);

        /** @var \Magento\Backend\Model\Auth\Session $session */
        $session = $this->_objectManager->get('Magento\Backend\Model\Auth\Session');
        $session->setUser($user);
        $session->processLogin();

        if ($session->isLoggedIn()) {
            /** @var \Magento\Backend\Model\UrlInterface $backendUrl */
            $backendUrl = $this->_objectManager->get('Magento\Backend\Model\UrlInterface');
            $path = $backendUrl->getStartupPageUrl();
            $url = $backendUrl->getUrl($path);
            $url = str_replace('autologin.php', 'index.php', $url);
            echo $url;
            //header('Location:  '.$url);
        } else {
            echo "login failed";
        }
        exit;
    }
}

$bootstrap = Bootstrap::create(BP, $_SERVER);
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('TestApp');
$bootstrap->run($app);

The session returns isLoggedIn = true and i receive the URL: http://local.domain.dev/index.php/admin/admin/dashboard/index/key/2e3a171171d22a5c2331780f874911ba95824f6a6d0e73894771bf9a40f26ce9/

If i visit that link or if i enable the header() part, i receive the login form :(

I see that i'm missing a cookie (which is set when login form is shown) and i traced it to Magento\Backend\Model\Session\AdminConfig but i'm a bit lost putting them together.

3

Found the missing link - posting here if anyone will ever need it :D

public function launch()
{
    $areaCode = 'adminhtml';
    $username = 'admin';

    $this->_request->setPathInfo('/admin');
    $this->_state->setAreaCode($areaCode);
    $this->_objectManager->configure($this->_configLoader->load($areaCode));

    /** @var \Magento\User\Model\User $user */
    $user = $this->_objectManager->get('Magento\User\Model\User')->loadByUsername($username);

    /** @var \Magento\Backend\Model\Auth\Session $session */
    $session = $this->_objectManager->get('Magento\Backend\Model\Auth\Session');
    $session->setUser($user);
    $session->processLogin();

    if ($session->isLoggedIn()) {
        $cookieManager = $this->_objectManager->get('Magento\Framework\Stdlib\CookieManagerInterface');
        $cookieValue = $session->getSessionId();
        if ($cookieValue) {
            $sessionConfig = $this->_objectManager->get('Magento\Backend\Model\Session\AdminConfig');
            $cookiePath = str_replace('autologin.php', 'index.php', $sessionConfig->getCookiePath());
            $cookieMetadata = $this->_objectManager->get('Magento\Framework\Stdlib\Cookie\CookieMetadataFactory')
                ->createPublicCookieMetadata()
                ->setDuration(3600)
                ->setPath($cookiePath)
                ->setDomain($sessionConfig->getCookieDomain())
                ->setSecure($sessionConfig->getCookieSecure())
                ->setHttpOnly($sessionConfig->getCookieHttpOnly());
            $cookieManager->setPublicCookie($session->getName(), $cookieValue, $cookieMetadata);
        }

        /** @var \Magento\Backend\Model\UrlInterface $backendUrl */
        $backendUrl = $this->_objectManager->get('Magento\Backend\Model\UrlInterface');
        $path = $backendUrl->getStartupPageUrl();
        $url = $backendUrl->getUrl($path);
        $url = str_replace('autologin.php', 'index.php', $url);
        header('Location:  '.$url);
        exit;
    }
    return $this->_response;
}

This is the whole function from my question class and it works!

  • For M2.1 there is extra few lines that need to be added after last line of $cookieManager: ` if (class_exists('Magento\Security\Model\AdminSessionsManager')) { /** @var \Magento\Security\Model\AdminSessionsManager $adminSessionManager */ $adminSessionManager = $this->_objectManager->get('Magento\Security\Model\AdminSessionsManager'); $adminSessionManager->processLogin(); } ` (I can't get the code to display properly... :/) – Sumoborac Jul 8 '16 at 9:28
  • To make it work on 2.1.2 I had to replace $session->getName() with $sessionConfig->getName(). Before that it created session cookie with name 'PHPSESSID' and correct name is 'admin' – Viperet Sep 11 '17 at 15:40
  • setAreaCode is not working, it seems like need to use emulateAreaCode, but not sure the value for 2nd and 3rd argument. – Gagan Feb 21 '18 at 8:05
1

Here is free extension with autologin for M2 implementation https://github.com/rogyar/magento2-autologin , work good on 2.2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.