Skip to content

Commit

Permalink
Merge pull request #91 from magento/magento-commerce/magento2-login-a…
Browse files Browse the repository at this point in the history
…s-customer/issues/90

magento2-login-as-customer/issues/90: Tests stabilization.
  • Loading branch information
naydav committed Apr 27, 2020
2 parents e72015b + 4a30707 commit 143168f
Show file tree
Hide file tree
Showing 27 changed files with 234 additions and 359 deletions.
2 changes: 2 additions & 0 deletions app/code/Magento/LoginAsCustomer/Block/Adminhtml/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

/**
* Login as customer log
*
* @api
*/
class Login extends \Magento\Backend\Block\Widget\Grid\Container
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

namespace Magento\LoginAsCustomer\Controller\Adminhtml\Login;

use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\View\Result\Page;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Backend\App\Action;
use Magento\LoginAsCustomer\Model\Login;

/**
* Login As Customer log grid action
Expand All @@ -27,18 +30,17 @@ class Index extends Action implements HttpGetActionInterface, HttpPostActionInte
const ADMIN_RESOURCE = 'Magento_LoginAsCustomer::login_log';

/**
* @var \Magento\LoginAsCustomer\Model\Login
* @var Login
*/
private $loginModel;

/**
* Index constructor.
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\LoginAsCustomer\Model\Login $loginModel
* @param Context $context
* @param Login $loginModel
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\LoginAsCustomer\Model\Login $loginModel
Context $context,
Login $loginModel
) {
parent::__construct($context);
$this->loginModel = $loginModel;
Expand All @@ -59,6 +61,7 @@ public function execute():ResultInterface

$this->loginModel->deleteNotUsed();

/** @var Page $resultPage */
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$resultPage->setActiveMenu('Magento_LoginAsCustomer::login_log')
->addBreadcrumb(__('Customer'), __('Login As Customer Log'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function __construct(
*
* @return ResultInterface
*/
public function execute():ResultInterface
public function execute(): ResultInterface
{
$request = $this->getRequest();
$customerId = (int) $request->getParam('customer_id');
Expand All @@ -94,13 +94,13 @@ public function execute():ResultInterface
$resultRedirect = $this->resultRedirectFactory->create();

if (!$this->config->isEnabled()) {
$this->messageManager->addErrorMessage(__('Login As Customer is disabled, to enable the extension please navigate to Stores > Configuration > Customers > Login As Customer.'));
$this->messageManager->addErrorMessage(__('Login As Customer is disabled.'));
return $resultRedirect->setPath('customer/index/index');
}

$customerStoreId = $request->getParam('store_id');

if (!isset($customerStoreId) && $this->config->getStoreViewLogin()) {
if (!isset($customerStoreId) && $this->config->isManualChoiceEnabled()) {
$this->messageManager->addNoticeMessage(__('Please select a Store View to login in.'));
return $resultRedirect->setPath('loginascustomer/login/manual', ['entity_id' => $customerId ]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\LoginAsCustomer\Controller\Adminhtml\Login;

use Magento\Backend\Model\View\Result\Page;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\App\Action\HttpGetActionInterface;
Expand All @@ -31,6 +32,7 @@ class Manual extends Action implements HttpGetActionInterface
*/
public function execute():ResultInterface
{
/** @var Page $resultPage */
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$resultPage->setActiveMenu('Magento_LoginAsCustomer::login_button')
->addBreadcrumb(__('Customer'), __('Login As Customer Log'), __('Store View To Login In'));
Expand Down
83 changes: 59 additions & 24 deletions app/code/Magento/LoginAsCustomer/Controller/Login/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,82 +7,117 @@

namespace Magento\LoginAsCustomer\Controller\Login;

use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\Action;
use Magento\Framework\Message\ManagerInterface;
use Magento\LoginAsCustomer\Model\Login;
use Psr\Log\LoggerInterface;

/**
* Login As Customer storefront login action
*/
class Index extends Action implements HttpGetActionInterface
class Index implements HttpGetActionInterface
{
/**
* @var \Magento\LoginAsCustomer\Model\Login
* @var ResultFactory
*/
private $resultFactory;

/**
* @var RequestInterface
*/
private $request;

/**
* @var Login
*/
private $loginModel;

/**
* @var ManagerInterface
*/
private $messageManager;

/**
* @var LoggerInterface
*/
private $logger;

/**
* Index constructor.
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\LoginAsCustomer\Model\Login $loginModel
* @param ResultFactory $resultFactory
* @param RequestInterface $request
* @param Login $loginModel
* @param ManagerInterface $messageManager
* @param LoggerInterface $logger
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\LoginAsCustomer\Model\Login $loginModel
ResultFactory $resultFactory,
RequestInterface $request,
Login $loginModel,
ManagerInterface $messageManager,
LoggerInterface $logger
) {
parent::__construct($context);
$this->resultFactory = $resultFactory;
$this->request = $request;
$this->loginModel = $loginModel;
$this->messageManager = $messageManager;
$this->logger = $logger;
}

/**
* Login As Customer storefront login
*
* @return ResultInterface
*/
public function execute():ResultInterface
public function execute(): ResultInterface
{
/** @var Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
try {
$login = $this->_initLogin();

/* Log in */
try {
$login = $this->initLogin();
$login->authenticateCustomer();

$this->messageManager->addSuccessMessage(
__('You are logged in as customer: %1', $login->getCustomer()->getName())
);
$resultRedirect->setPath('*/*/proceed');

} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());

$resultRedirect->setPath('/');
return $resultRedirect;

} catch (\Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
$this->logger->error($e->getMessage());

$resultRedirect->setPath('*/*/proceed');
$this->messageManager->addErrorMessage(__('Cannot login to account.'));
$resultRedirect->setPath('/');
}
return $resultRedirect;
}

/**
* Init login info
* @return \Magento\LoginAsCustomer\Model\Login
*
* @return Login
* @throws LocalizedException
*/
private function _initLogin(): \Magento\LoginAsCustomer\Model\Login
private function initLogin(): Login
{
$secret = $this->getRequest()->getParam('secret');
$secret = $this->request->getParam('secret');
if (!$secret) {
throw LocalizedException(__('Cannot login to account. No secret key provided.'));
throw new LocalizedException(__('Cannot login to account. No secret key provided.'));
}

$login = $this->loginModel->loadNotUsed($secret);

if ($login->getId()) {
return $login;
} else {
throw LocalizedException(__('Cannot login to account. Secret key is not valid'));
throw new LocalizedException(__('Cannot login to account. Secret key is not valid'));
}
}
}
19 changes: 16 additions & 3 deletions app/code/Magento/LoginAsCustomer/Controller/Login/Proceed.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,39 @@
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\LoginAsCustomer\Controller\Login;

use Magento\Backend\Model\View\Result\Page;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\Action;

/**
* Login as customer proxy page
* Allows running JavaScript to load customer data to the browser local storage
*/
class Proceed extends Action implements HttpGetActionInterface
class Proceed implements HttpGetActionInterface
{
/**
* @var ResultFactory
*/
private $resultFactory;

/**
* @param ResultFactory $resultFactory
*/
public function __construct(ResultFactory $resultFactory)
{
$this->resultFactory = $resultFactory;
}

/**
* Proxy page
*
* @return ResultInterface
*/
public function execute():ResultInterface
public function execute(): ResultInterface
{
/** @var Page $resultPage */
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

/**
* Customer data for the logged_as_customer section
*
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
*/
class LoginAsCustomer implements SectionSourceInterface
{
Expand Down Expand Up @@ -41,6 +43,7 @@ public function __construct(

/**
* Retrieve private customer data for the logged_as_customer section
*
* @return array
*/
public function getSectionData():array
Expand All @@ -50,8 +53,8 @@ public function getSectionData():array
}

return [
'admin_user_id' => $this->customerSession->getLoggedAsCustomerAdmindId(),
'website_name' => $this->storeManager->getWebsite()->getName()
'adminUserId' => $this->customerSession->getLoggedAsCustomerAdmindId(),
'websiteName' => $this->storeManager->getWebsite()->getName()
];
}
}
Loading

0 comments on commit 143168f

Please sign in to comment.