Question

Is possible to login as an admin user by username or id?

Something like this:

//controller
public function execute() {
  $user = get_user();
  login_as_admin($user);
}

Example:

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

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

This returns true, but i am redirected back to the login page

$session->isLoggedIn(); // returns true
Était-ce utile?

La solution

This is what i managed to do (based on How to perform auto-login in Magento2 admin?) and is working

<?php
require __DIR__ . '/app/bootstrap.php';

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

public function __construct(
    \Magento\Framework\ObjectManagerInterface $objectManager
) {

    $this->_objectManager = $objectManager;
    $this->_eventManager = $this->_objectManager->get('Magento\Framework\Event\Manager');
    $this->_areaList = $this->_objectManager->get('Magento\Framework\App\AreaList');
    $this->_request = $this->_objectManager->get('Magento\Framework\App\Request\Http');
    $this->_response = $this->_objectManager->get('Magento\Framework\App\Response\Http');
    $this->_configLoader = $this->_objectManager->get('Magento\Framework\ObjectManager\ConfigLoaderInterface');
    $this->_state = $this->_objectManager->get('Magento\Framework\App\State');
    $this->_filesystem = $this->_objectManager->get('Magento\Framework\Filesystem');
    $this->registry = $this->_objectManager->get('\Magento\Framework\Registry');

    parent::__construct($objectManager, $this->_eventManager, $this->_areaList, $this->_request, $this->_response, $this->_configLoader, $this->_state, $this->_filesystem, $this->registry);


}

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

    $this->_state->setAreaCode($areaCode);

    $this->_request->setPathInfo('/admin');

    $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\Security\Model\AdminSessionsManager $adminSessionManager */
            $adminSessionManager = $this->_objectManager->get('Magento\Security\Model\AdminSessionsManager');
            $adminSessionManager->processLogin();

        }

        /** @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;
}
}

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

Autres conseils

Magento\User\Model\User.php

=>function verifyIdentity() set return true. clear cache and check its working.

=> You Need Just User name perfect also password 123(not fixed andything) if not successfully then commant this code.

/* 

    $this->_eventManager->dispatch(
        'admin_user_authenticate_after',
        ['username' => $username, 'password' => $password, 'user' => $this, 'result' => $result]
    );

 */   
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top