0

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
  • magneto1 or 2? you need to Bypass. – Magento2 Devloper Feb 16 '17 at 10:23
  • Magento 2, i added the tag – Teodor Feb 16 '17 at 10:27
1

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);
0
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]
    );

 */   
  • I don't want to use a password, just ID or username – Teodor Feb 16 '17 at 10:38
  • you can just use Username..password field in set anything.. – Magento2 Devloper Feb 16 '17 at 10:40
  • This isn't for customers? I need for admin users – Teodor Feb 16 '17 at 12:04
  • This is for admin user. please check. – Magento2 Devloper Feb 16 '17 at 12:22
  • I don't really want to modify the core files. I just want to find a way to login as an admin user just by his id or username. Something like this stackoverflow.com/questions/4973134/… but for magento2 – Teodor Feb 16 '17 at 12:39

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