Skip to content

Commit 15c9e22

Browse files
committed
magento2-login-as-customer/issues/90: Tests stabilizatio
UI part refactoring
1 parent d69c0f9 commit 15c9e22

File tree

17 files changed

+118
-257
lines changed

17 files changed

+118
-257
lines changed

app/code/Magento/LoginAsCustomer/Controller/Adminhtml/Login/Index.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77

88
namespace Magento\LoginAsCustomer\Controller\Adminhtml\Login;
99

10+
use Magento\Backend\App\Action\Context;
11+
use Magento\Backend\Model\View\Result\Page;
1012
use Magento\Framework\Controller\ResultFactory;
1113
use Magento\Framework\Controller\ResultInterface;
1214
use Magento\Framework\App\Action\HttpGetActionInterface;
1315
use Magento\Framework\App\Action\HttpPostActionInterface;
1416
use Magento\Backend\App\Action;
17+
use Magento\LoginAsCustomer\Model\Login;
1518

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

2932
/**
30-
* @var \Magento\LoginAsCustomer\Model\Login
33+
* @var Login
3134
*/
3235
private $loginModel;
3336

3437
/**
35-
* Index constructor.
36-
* @param \Magento\Backend\App\Action\Context $context
37-
* @param \Magento\LoginAsCustomer\Model\Login $loginModel
38+
* @param Context $context
39+
* @param Login $loginModel
3840
*/
3941
public function __construct(
40-
\Magento\Backend\App\Action\Context $context,
41-
\Magento\LoginAsCustomer\Model\Login $loginModel
42+
Context $context,
43+
Login $loginModel
4244
) {
4345
parent::__construct($context);
4446
$this->loginModel = $loginModel;
@@ -59,6 +61,7 @@ public function execute():ResultInterface
5961

6062
$this->loginModel->deleteNotUsed();
6163

64+
/** @var Page $resultPage */
6265
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
6366
$resultPage->setActiveMenu('Magento_LoginAsCustomer::login_log')
6467
->addBreadcrumb(__('Customer'), __('Login As Customer Log'));

app/code/Magento/LoginAsCustomer/Controller/Adminhtml/Login/Login.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function __construct(
8282
*
8383
* @return ResultInterface
8484
*/
85-
public function execute():ResultInterface
85+
public function execute(): ResultInterface
8686
{
8787
$request = $this->getRequest();
8888
$customerId = (int) $request->getParam('customer_id');
@@ -94,7 +94,7 @@ public function execute():ResultInterface
9494
$resultRedirect = $this->resultRedirectFactory->create();
9595

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

app/code/Magento/LoginAsCustomer/Controller/Adminhtml/Login/Manual.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\LoginAsCustomer\Controller\Adminhtml\Login;
99

10+
use Magento\Backend\Model\View\Result\Page;
1011
use Magento\Framework\Controller\ResultFactory;
1112
use Magento\Framework\Controller\ResultInterface;
1213
use Magento\Framework\App\Action\HttpGetActionInterface;
@@ -31,6 +32,7 @@ class Manual extends Action implements HttpGetActionInterface
3132
*/
3233
public function execute():ResultInterface
3334
{
35+
/** @var Page $resultPage */
3436
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
3537
$resultPage->setActiveMenu('Magento_LoginAsCustomer::login_button')
3638
->addBreadcrumb(__('Customer'), __('Login As Customer Log'), __('Store View To Login In'));

app/code/Magento/LoginAsCustomer/Controller/Login/Index.php

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,82 +7,117 @@
77

88
namespace Magento\LoginAsCustomer\Controller\Login;
99

10+
use Magento\Framework\App\RequestInterface;
11+
use Magento\Framework\Controller\Result\Redirect;
1012
use Magento\Framework\Controller\ResultFactory;
1113
use Magento\Framework\Controller\ResultInterface;
1214
use Magento\Framework\Exception\LocalizedException;
1315
use Magento\Framework\App\Action\HttpGetActionInterface;
14-
use Magento\Framework\App\Action\Action;
16+
use Magento\Framework\Message\ManagerInterface;
17+
use Magento\LoginAsCustomer\Model\Login;
18+
use Psr\Log\LoggerInterface;
1519

1620
/**
1721
* Login As Customer storefront login action
1822
*/
19-
class Index extends Action implements HttpGetActionInterface
23+
class Index implements HttpGetActionInterface
2024
{
2125
/**
22-
* @var \Magento\LoginAsCustomer\Model\Login
26+
* @var ResultFactory
27+
*/
28+
private $resultFactory;
29+
30+
/**
31+
* @var RequestInterface
32+
*/
33+
private $request;
34+
35+
/**
36+
* @var Login
2337
*/
2438
private $loginModel;
2539

40+
/**
41+
* @var ManagerInterface
42+
*/
43+
private $messageManager;
44+
45+
/**
46+
* @var LoggerInterface
47+
*/
48+
private $logger;
2649

2750
/**
28-
* Index constructor.
29-
* @param \Magento\Framework\App\Action\Context $context
30-
* @param \Magento\LoginAsCustomer\Model\Login $loginModel
51+
* @param ResultFactory $resultFactory
52+
* @param RequestInterface $request
53+
* @param Login $loginModel
54+
* @param ManagerInterface $messageManager
55+
* @param LoggerInterface $logger
3156
*/
3257
public function __construct(
33-
\Magento\Framework\App\Action\Context $context,
34-
\Magento\LoginAsCustomer\Model\Login $loginModel
58+
ResultFactory $resultFactory,
59+
RequestInterface $request,
60+
Login $loginModel,
61+
ManagerInterface $messageManager,
62+
LoggerInterface $logger
3563
) {
36-
parent::__construct($context);
64+
$this->resultFactory = $resultFactory;
65+
$this->request = $request;
3766
$this->loginModel = $loginModel;
67+
$this->messageManager = $messageManager;
68+
$this->logger = $logger;
3869
}
70+
3971
/**
4072
* Login As Customer storefront login
4173
*
4274
* @return ResultInterface
4375
*/
44-
public function execute():ResultInterface
76+
public function execute(): ResultInterface
4577
{
78+
/** @var Redirect $resultRedirect */
4679
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
47-
try {
48-
$login = $this->_initLogin();
4980

50-
/* Log in */
81+
try {
82+
$login = $this->initLogin();
5183
$login->authenticateCustomer();
84+
5285
$this->messageManager->addSuccessMessage(
5386
__('You are logged in as customer: %1', $login->getCustomer()->getName())
5487
);
88+
$resultRedirect->setPath('*/*/proceed');
89+
5590
} catch (LocalizedException $e) {
5691
$this->messageManager->addErrorMessage($e->getMessage());
57-
5892
$resultRedirect->setPath('/');
59-
return $resultRedirect;
60-
6193
} catch (\Exception $e) {
62-
$this->messageManager->addErrorMessage($e->getMessage());
63-
}
94+
$this->logger->error($e->getMessage());
6495

65-
$resultRedirect->setPath('*/*/proceed');
96+
$this->messageManager->addErrorMessage(__('Cannot login to account.'));
97+
$resultRedirect->setPath('/');
98+
}
6699
return $resultRedirect;
67100
}
68101

69102
/**
70103
* Init login info
71-
* @return \Magento\LoginAsCustomer\Model\Login
104+
*
105+
* @return Login
106+
* @throws LocalizedException
72107
*/
73-
private function _initLogin(): \Magento\LoginAsCustomer\Model\Login
108+
private function initLogin(): Login
74109
{
75-
$secret = $this->getRequest()->getParam('secret');
110+
$secret = $this->request->getParam('secret');
76111
if (!$secret) {
77-
throw LocalizedException(__('Cannot login to account. No secret key provided.'));
112+
throw new LocalizedException(__('Cannot login to account. No secret key provided.'));
78113
}
79114

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

82117
if ($login->getId()) {
83118
return $login;
84119
} else {
85-
throw LocalizedException(__('Cannot login to account. Secret key is not valid'));
120+
throw new LocalizedException(__('Cannot login to account. Secret key is not valid'));
86121
}
87122
}
88123
}

app/code/Magento/LoginAsCustomer/Controller/Login/Proceed.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,38 @@
44
* See COPYING.txt for license details.
55
*/
66
declare(strict_types=1);
7+
78
namespace Magento\LoginAsCustomer\Controller\Login;
89

910
use Magento\Framework\Controller\ResultFactory;
1011
use Magento\Framework\Controller\ResultInterface;
1112
use Magento\Framework\App\Action\HttpGetActionInterface;
12-
use Magento\Framework\App\Action\Action;
1313

1414
/**
1515
* Login as customer proxy page
1616
* Allows running JavaScript to load customer data to the browser local storage
1717
*/
18-
class Proceed extends Action implements HttpGetActionInterface
18+
class Proceed implements HttpGetActionInterface
1919
{
20+
/**
21+
* @var ResultFactory
22+
*/
23+
private $resultFactory;
24+
25+
/**
26+
* @param ResultFactory $resultFactory
27+
*/
28+
public function __construct(ResultFactory $resultFactory)
29+
{
30+
$this->resultFactory = $resultFactory;
31+
}
32+
2033
/**
2134
* Proxy page
2235
*
2336
* @return ResultInterface
2437
*/
25-
public function execute():ResultInterface
38+
public function execute(): ResultInterface
2639
{
2740
return $this->resultFactory->create(ResultFactory::TYPE_PAGE);
2841
}

app/code/Magento/LoginAsCustomer/Model/Config.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ class Config
1919
/**
2020
* Extension config path
2121
*/
22-
private const XML_PATH_EXTENSION_ENABLED = 'mfloginascustomer/general/enabled';
23-
private const XML_PATH_KEY = 'mfloginascustomer/general/key';
24-
private const STORE_VIEW_TO_LOGIN_IN = 'mfloginascustomer/general/store_view_login';
22+
private const XML_PATH_EXTENSION_ENABLED = 'loginascustomer/general/enabled';
23+
private const STORE_VIEW_TO_LOGIN_IN = 'loginascustomer/general/store_view_login';
2524

2625
/**
2726
* @var ScopeConfigInterface
2827
*/
2928

3029
private $scopeConfig;
30+
3131
/**
3232
* @var \Magento\Framework\App\ProductMetadataInterface
3333
*/
@@ -64,7 +64,7 @@ public function getConfig($path, $storeId = null)
6464
/**
6565
* @return bool
6666
*/
67-
public function isEnabled():bool
67+
public function isEnabled(): bool
6868
{
6969
return (bool)$this->getConfig(
7070
self::XML_PATH_EXTENSION_ENABLED

app/code/Magento/LoginAsCustomer/Model/PageCache/ConfigPlugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function afterIsEnabled(\Magento\PageCache\Model\Config $subject, $result
4949
{
5050
if ($result) {
5151
$disable = $this->_scopeConfig->getValue(
52-
'mfloginascustomer/general/disable_page_cache',
52+
'loginascustomer/general/disable_page_cache',
5353
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
5454
);
5555
$adminId = $this->_customerSession->getLoggedAsCustomerAdmindId();

app/code/Magento/LoginAsCustomer/Observer/WbsiterestrictionFrontendObserver.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

app/code/Magento/LoginAsCustomer/Block/Adminhtml/Customer/Edit/Login.php renamed to app/code/Magento/LoginAsCustomer/Ui/Customer/Component/Control/LoginAsCustomerButton.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,31 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\LoginAsCustomer\Block\Adminhtml\Customer\Edit;
8+
namespace Magento\LoginAsCustomer\Ui\Customer\Component\Control;
99

1010
use Magento\Customer\Block\Adminhtml\Edit\GenericButton;
1111
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
1212

1313
/**
1414
* Login as customer button
1515
*/
16-
class Login extends GenericButton implements ButtonProviderInterface
16+
class LoginAsCustomerButton extends GenericButton implements ButtonProviderInterface
1717
{
1818
/**
1919
* @var \Magento\Framework\AuthorizationInterface
2020
*/
21-
private $_authorization;
21+
private $authorization;
2222

2323
/**
24-
* Constructor
25-
*
2624
* @param \Magento\Backend\Block\Widget\Context $context
2725
* @param \Magento\Framework\Registry $registry
28-
* @param AccountManagementInterface $customerAccountManagement
2926
*/
3027
public function __construct(
3128
\Magento\Backend\Block\Widget\Context $context,
3229
\Magento\Framework\Registry $registry
3330
) {
3431
parent::__construct($context, $registry);
35-
$this->_authorization = $context->getAuthorization();
32+
$this->authorization = $context->getAuthorization();
3633
}
3734

3835
/**
@@ -42,12 +39,12 @@ public function getButtonData(): array
4239
{
4340
$customerId = $this->getCustomerId();
4441
$data = [];
45-
$canModify = $customerId && $this->_authorization->isAllowed('Magento_LoginAsCustomer::login_button');
42+
$canModify = $customerId && $this->authorization->isAllowed('Magento_LoginAsCustomer::login_button');
4643
if ($canModify) {
4744
$data = [
4845
'label' => __('Login As Customer'),
4946
'class' => 'login login-button',
50-
'on_click' => 'window.open( \'' . $this->getInvalidateTokenUrl() .
47+
'on_click' => 'window.open( \'' . $this->getLoginUrl() .
5148
'\')',
5249
'sort_order' => 70,
5350
];
@@ -58,7 +55,7 @@ public function getButtonData(): array
5855
/**
5956
* @return string
6057
*/
61-
public function getInvalidateTokenUrl(): string
58+
public function getLoginUrl(): string
6259
{
6360
return $this->getUrl('loginascustomer/login/login', ['customer_id' => $this->getCustomerId()]);
6461
}

0 commit comments

Comments
 (0)