Skip to content

Commit 4ce50a1

Browse files
committed
Merge branch 'module-login-as-customer' of github.com:magento/magento2-login-as-customer into module-login-as-customer
2 parents 2bf0af8 + 143168f commit 4ce50a1

File tree

26 files changed

+238
-204
lines changed

26 files changed

+238
-204
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
/**
1111
* Login as customer log
12+
*
13+
* @api
1214
*/
1315
class Login extends \Magento\Backend\Block\Widget\Grid\Container
1416
{

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: 3 additions & 3 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,13 +94,13 @@ 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

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

103-
if (!isset($customerStoreId) && $this->config->getStoreViewLogin()) {
103+
if (!isset($customerStoreId) && $this->config->isManualChoiceEnabled()) {
104104
$this->messageManager->addNoticeMessage(__('Please select a Store View to login in.'));
105105
return $resultRedirect->setPath('loginascustomer/login/manual', ['entity_id' => $customerId ]);
106106
}

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: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,43 @@
44
* See COPYING.txt for license details.
55
*/
66
declare(strict_types=1);
7+
78
namespace Magento\LoginAsCustomer\Controller\Login;
89

10+
use Magento\Backend\Model\View\Result\Page;
911
use Magento\Framework\Controller\ResultFactory;
1012
use Magento\Framework\Controller\ResultInterface;
1113
use Magento\Framework\App\Action\HttpGetActionInterface;
12-
use Magento\Framework\App\Action\Action;
1314

1415
/**
1516
* Login as customer proxy page
1617
* Allows running JavaScript to load customer data to the browser local storage
1718
*/
18-
class Proceed extends Action implements HttpGetActionInterface
19+
class Proceed implements HttpGetActionInterface
1920
{
21+
/**
22+
* @var ResultFactory
23+
*/
24+
private $resultFactory;
25+
26+
/**
27+
* @param ResultFactory $resultFactory
28+
*/
29+
public function __construct(ResultFactory $resultFactory)
30+
{
31+
$this->resultFactory = $resultFactory;
32+
}
33+
2034
/**
2135
* Proxy page
2236
*
2337
* @return ResultInterface
2438
*/
25-
public function execute():ResultInterface
39+
public function execute(): ResultInterface
2640
{
41+
/** @var Page $resultPage */
42+
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
43+
$resultPage->getConfig()->getTitle()->set(__("You are logged in"));
2744
return $this->resultFactory->create(ResultFactory::TYPE_PAGE);
2845
}
2946
}

app/code/Magento/LoginAsCustomer/CustomerData/LoginAsCustomer.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
/**
1515
* Customer data for the logged_as_customer section
16+
*
17+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
1618
*/
1719
class LoginAsCustomer implements SectionSourceInterface
1820
{
@@ -41,6 +43,7 @@ public function __construct(
4143

4244
/**
4345
* Retrieve private customer data for the logged_as_customer section
46+
*
4447
* @return array
4548
*/
4649
public function getSectionData():array
@@ -50,8 +53,8 @@ public function getSectionData():array
5053
}
5154

5255
return [
53-
'admin_user_id' => $this->customerSession->getLoggedAsCustomerAdmindId(),
54-
'website_name' => $this->storeManager->getWebsite()->getName()
56+
'adminUserId' => $this->customerSession->getLoggedAsCustomerAdmindId(),
57+
'websiteName' => $this->storeManager->getWebsite()->getName()
5558
];
5659
}
5760
}

0 commit comments

Comments
 (0)