Last active
July 28, 2022 20:10
-
-
Save satishgumudavelli/7f0e056380e769be77897eab8244fbc3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (defined('STDIN')) { | |
$_GET['user'] = $argv[1]; | |
// sudo -u www-data -- php pub/autologin.php "Lennon ava" | |
} | |
header('Pragma: no-cache'); | |
header('Cache-Control: private, no-cache, no-store, max-age=0, must-revalidate, proxy-revalidate'); | |
header('Expires: Tue, 04 Sep 2012 05:32:29 GMT'); | |
ini_set('display_errors', 1); | |
ini_set('display_startup_errors', 1); | |
ini_set('memory_limit', '5G'); | |
error_reporting(E_ALL); | |
set_time_limit(0); | |
use Magento\Backend\Setup\ConfigOptionsList as BackendConfigOptionsList; | |
use Magento\Framework\App\Bootstrap; | |
require dirname(realpath(__FILE__)) . '/../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->registry); | |
} | |
public function launch() | |
{ | |
$areaCode = 'adminhtml'; | |
$username = $_GET['user'] ?? 'satish'; | |
$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); | |
$userActiveSession = $this->_objectManager->get(\Magento\Security\Model\AdminSessionInfo::class)->getCollection(); | |
$userActiveSession | |
->addFieldToFilter('user_id', $user->getId()) | |
->addFieldToFilter('status', 1); | |
$userActiveSession->getSelect() | |
->order('created_at desc') | |
->limit(1) | |
->reset('columns') | |
->columns('session_id'); | |
$activeSessionId = $userActiveSession->getConnection()->fetchOne($userActiveSession->getSelect()); | |
$activeSessionId = ''; | |
$sessionName = 'admin'; | |
$backendUrl = $this->_objectManager->get('Magento\Backend\Model\UrlInterface'); | |
if (!$activeSessionId) { | |
/** @var \Magento\Backend\Model\Auth\Session $session */ | |
$session = $this->_objectManager->get(\Magento\Backend\Model\Auth\Session::class); | |
$session->setUser($user); | |
$session->processLogin(); | |
// $this->sessionsManager->processLogin(); | |
$sessionsManager = $this->_objectManager->get(\Magento\Security\Model\AdminSessionsManager::class); | |
$sessionsManager->processLogin(); | |
if ($session->isLoggedIn()) { | |
$activeSessionId = $session->getSessionId(); | |
$sessionName = $session->getName(); | |
} | |
/** @var \Magento\Backend\Model\UrlInterface $backendUrl */ | |
$path = $backendUrl->getStartupPageUrl(); | |
} else { | |
$deploymentConfig = $this->_objectManager->get('\Magento\Framework\App\DeploymentConfig'); | |
$path = $deploymentConfig->get(BackendConfigOptionsList::CONFIG_PATH_BACKEND_FRONTNAME); | |
} | |
if ($activeSessionId) { | |
$cookieManager = $this->_objectManager->get('Magento\Framework\Stdlib\CookieManagerInterface'); | |
$cookieValue = $activeSessionId; | |
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($sessionName, $cookieValue, $cookieMetadata); | |
$date = new DateTime("now"); | |
$date->modify('+1 day'); | |
echo "\n"; | |
echo 'document.cookie ="' . $sessionName . '=' . $cookieValue . '; expires=' . $date->format('Y-m-dTH:i:sZ') . '; path=' . $cookiePath . '; domain=' . $sessionConfig->getCookieDomain() . '; "'; | |
echo "\n"; | |
echo "<br>"; | |
echo "\n"; | |
} | |
$url = $backendUrl->getUrl($path); | |
$url = str_replace('autologin.php', 'index.php', $url); | |
echo "\n"; | |
echo "<a href='" . $url . "' target='_blank'>Click here</a>"; | |
echo "\n"; | |
die; | |
header('Location: ' . $url); | |
exit; | |
} | |
return $this->_response; | |
} | |
} | |
$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); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment