Skip to content

Commit db5bc62

Browse files
authored
Merge pull request #5708 from magento-engcom/module-login-as-customer
[EngCom] Login as Customer
2 parents 2cea321 + a86d2d6 commit db5bc62

File tree

95 files changed

+1094
-563
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+1094
-563
lines changed

app/code/Magento/LoginAsCustomer/Model/AuthenticateCustomer.php renamed to app/code/Magento/LoginAsCustomer/Model/AuthenticateCustomerBySecret.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,45 @@
99

1010
use Magento\Customer\Model\Session;
1111
use Magento\Framework\Exception\LocalizedException;
12-
use Magento\LoginAsCustomerApi\Api\AuthenticateCustomerInterface;
13-
use Magento\LoginAsCustomerApi\Api\Data\AuthenticationDataInterface;
12+
use Magento\LoginAsCustomerApi\Api\AuthenticateCustomerBySecretInterface;
13+
use Magento\LoginAsCustomerApi\Api\GetAuthenticationDataBySecretInterface;
1414

1515
/**
1616
* @inheritdoc
1717
*
1818
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
1919
*/
20-
class AuthenticateCustomer implements AuthenticateCustomerInterface
20+
class AuthenticateCustomerBySecret implements AuthenticateCustomerBySecretInterface
2121
{
22+
/**
23+
* @var GetAuthenticationDataBySecretInterface
24+
*/
25+
private $getAuthenticationDataBySecret;
26+
2227
/**
2328
* @var Session
2429
*/
2530
private $customerSession;
2631

2732
/**
33+
* @param GetAuthenticationDataBySecretInterface $getAuthenticationDataBySecret
2834
* @param Session $customerSession
2935
*/
3036
public function __construct(
37+
GetAuthenticationDataBySecretInterface $getAuthenticationDataBySecret,
3138
Session $customerSession
3239
) {
40+
$this->getAuthenticationDataBySecret = $getAuthenticationDataBySecret;
3341
$this->customerSession = $customerSession;
3442
}
3543

3644
/**
3745
* @inheritdoc
3846
*/
39-
public function execute(AuthenticationDataInterface $authenticationData): void
47+
public function execute(string $secret): void
4048
{
49+
$authenticationData = $this->getAuthenticationDataBySecret->execute($secret);
50+
4151
if ($this->customerSession->getId()) {
4252
$this->customerSession->logout();
4353
}

app/code/Magento/LoginAsCustomer/Model/ResourceModel/DeleteExpiredAuthenticationData.php renamed to app/code/Magento/LoginAsCustomer/Model/ResourceModel/DeleteAuthenticationDataForUser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
use Magento\Framework\App\ResourceConnection;
1111
use Magento\Framework\Stdlib\DateTime\DateTime;
1212
use Magento\LoginAsCustomerApi\Api\ConfigInterface;
13-
use Magento\LoginAsCustomerApi\Api\DeleteExpiredAuthenticationDataInterface;
13+
use Magento\LoginAsCustomerApi\Api\DeleteAuthenticationDataForUserInterface;
1414

1515
/**
1616
* @inheritdoc
1717
*/
18-
class DeleteExpiredAuthenticationData implements DeleteExpiredAuthenticationDataInterface
18+
class DeleteAuthenticationDataForUser implements DeleteAuthenticationDataForUserInterface
1919
{
2020
/**
2121
* @var ResourceConnection

app/code/Magento/LoginAsCustomer/Model/ResourceModel/GetAuthenticationDataBySecret.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function __construct(
6161
/**
6262
* @inheritdoc
6363
*/
64-
public function execute(string $secretKey): AuthenticationDataInterface
64+
public function execute(string $secret): AuthenticationDataInterface
6565
{
6666
$connection = $this->resourceConnection->getConnection();
6767
$tableName = $this->resourceConnection->getTableName('login_as_customer');
@@ -73,7 +73,7 @@ public function execute(string $secretKey): AuthenticationDataInterface
7373

7474
$select = $connection->select()
7575
->from(['main_table' => $tableName])
76-
->where('main_table.secret = ?', $secretKey)
76+
->where('main_table.secret = ?', $secret)
7777
->where('main_table.created_at > ?', $timePoint);
7878

7979
$data = $connection->fetchRow($select);

app/code/Magento/LoginAsCustomerUi/Plugin/AdminLogoutPlugin.php renamed to app/code/Magento/LoginAsCustomer/Plugin/AdminLogoutPlugin.php

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

8-
namespace Magento\LoginAsCustomerUi\Plugin;
8+
namespace Magento\LoginAsCustomer\Plugin;
99

1010
use Magento\Backend\Model\Auth;
1111
use Magento\LoginAsCustomerApi\Api\ConfigInterface;
12-
use Magento\LoginAsCustomerApi\Api\DeleteExpiredAuthenticationDataInterface;
12+
use Magento\LoginAsCustomerApi\Api\DeleteAuthenticationDataForUserInterface;
1313

1414
/**
1515
* Delete all Login as Customer sessions for logging out admin.
@@ -22,20 +22,20 @@ class AdminLogoutPlugin
2222
private $config;
2323

2424
/**
25-
* @var DeleteExpiredAuthenticationDataInterface
25+
* @var DeleteAuthenticationDataForUserInterface
2626
*/
27-
private $deleteExpiredAuthenticationData;
27+
private $deleteAuthenticationDataForUser;
2828

2929
/**
3030
* @param ConfigInterface $config
31-
* @param DeleteExpiredAuthenticationDataInterface $deleteExpiredAuthenticationData
31+
* @param DeleteAuthenticationDataForUserInterface $deleteAuthenticationDataForUser
3232
*/
3333
public function __construct(
3434
ConfigInterface $config,
35-
DeleteExpiredAuthenticationDataInterface $deleteExpiredAuthenticationData
35+
DeleteAuthenticationDataForUserInterface $deleteAuthenticationDataForUser
3636
) {
3737
$this->config = $config;
38-
$this->deleteExpiredAuthenticationData = $deleteExpiredAuthenticationData;
38+
$this->deleteAuthenticationDataForUser = $deleteAuthenticationDataForUser;
3939
}
4040

4141
/**
@@ -47,7 +47,7 @@ public function beforeLogout(Auth $subject): void
4747
{
4848
if ($this->config->isEnabled()) {
4949
$userId = (int)$subject->getUser()->getId();
50-
$this->deleteExpiredAuthenticationData->execute($userId);
50+
$this->deleteAuthenticationDataForUser->execute($userId);
5151
}
5252
}
5353
}

app/code/Magento/LoginAsCustomer/composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
"magento/module-customer": "*",
88
"magento/module-login-as-customer-api": "*"
99
},
10+
"suggest": {
11+
"magento/module-backend": "*"
12+
},
1013
"type": "magento2-module",
1114
"license": [
1215
"OSL-3.0",

app/code/Magento/LoginAsCustomer/etc/db_schema.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
-->
88
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
99
xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
10-
<table name="login_as_customer" resource="default" engine="innodb" comment="Magento Login As Customer Table">
10+
<table name="login_as_customer" resource="default" engine="innodb" comment="Magento Login as Customer Table">
1111
<column xsi:type="varchar" name="secret" nullable="false" length="64" comment="Login Secret"/>
1212
<column xsi:type="int" name="customer_id" nullable="false" comment="Customer ID"/>
1313
<column xsi:type="int" name="admin_id" nullable="false" comment="Admin ID"/>

app/code/Magento/LoginAsCustomer/etc/di.xml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
<preference for="Magento\LoginAsCustomerApi\Api\Data\AuthenticationDataInterface" type="Magento\LoginAsCustomer\Model\AuthenticationData"/>
1010
<preference for="Magento\LoginAsCustomerApi\Api\SaveAuthenticationDataInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\SaveAuthenticationData"/>
1111
<preference for="Magento\LoginAsCustomerApi\Api\GetAuthenticationDataBySecretInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\GetAuthenticationDataBySecret"/>
12-
<preference for="Magento\LoginAsCustomerApi\Api\AuthenticateCustomerInterface" type="Magento\LoginAsCustomer\Model\AuthenticateCustomer"/>
13-
<preference for="Magento\LoginAsCustomerApi\Api\DeleteAuthenticationDataBySecretInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\DeleteAuthenticationDataBySecret"/>
14-
<preference for="Magento\LoginAsCustomerApi\Api\DeleteExpiredAuthenticationDataInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\DeleteExpiredAuthenticationData"/>
12+
<preference for="Magento\LoginAsCustomerApi\Api\AuthenticateCustomerBySecretInterface" type="Magento\LoginAsCustomer\Model\AuthenticateCustomerBySecret"/>
13+
<preference for="Magento\LoginAsCustomerApi\Api\DeleteAuthenticationDataForUserInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\DeleteAuthenticationDataForUser"/>
1514
<preference for="Magento\LoginAsCustomerApi\Api\ConfigInterface" type="Magento\LoginAsCustomer\Model\Config"/>
1615
<preference for="Magento\LoginAsCustomerApi\Api\IsLoginAsCustomerSessionActiveInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\IsLoginAsCustomerSessionActive"/>
16+
<type name="Magento\Backend\Model\Auth">
17+
<plugin name="login_as_customer_admin_logout" type="Magento\LoginAsCustomer\Plugin\AdminLogoutPlugin"/>
18+
</type>
1719
</config>

app/code/Magento/LoginAsCustomerUi/Block/Adminhtml/ConfirmationPopup.php renamed to app/code/Magento/LoginAsCustomerAdminUi/Block/Adminhtml/ConfirmationPopup.php

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

8-
namespace Magento\LoginAsCustomerUi\Block\Adminhtml;
8+
namespace Magento\LoginAsCustomerAdminUi\Block\Adminhtml;
99

10-
use Magento\Framework\Serialize\Serializer\Json;
11-
use Magento\Store\Ui\Component\Listing\Column\Store\Options as StoreOptions;
1210
use Magento\Backend\Block\Template;
11+
use Magento\Framework\Serialize\Serializer\Json;
1312
use Magento\LoginAsCustomerApi\Api\ConfigInterface;
13+
use Magento\Store\Ui\Component\Listing\Column\Store\Options as StoreOptions;
1414

1515
/**
16-
* Admin blog post
16+
* Login confirmation pop-up
1717
*
1818
* @api
1919
*/
2020
class ConfirmationPopup extends Template
2121
{
2222
/**
23-
* Store Options
24-
*
2523
* @var StoreOptions
2624
*/
2725
private $storeOptions;
2826

2927
/**
30-
* Config
31-
*
3228
* @var ConfigInterface
3329
*/
3430
private $config;
3531

3632
/**
37-
* Json Serializer
38-
*
3933
* @var Json
4034
*/
4135
private $json;
@@ -83,15 +77,13 @@ public function getJsLayout()
8377
}
8478

8579
/**
86-
* Render block HTML
87-
*
88-
* @return string
80+
* @inheritdoc
8981
*/
90-
protected function _toHtml()
82+
public function toHtml()
9183
{
9284
if (!$this->config->isEnabled()) {
9385
return '';
9486
}
95-
return parent::_toHtml();
87+
return parent::toHtml();
9688
}
9789
}

app/code/Magento/LoginAsCustomerUi/Controller/Adminhtml/Login/Login.php renamed to app/code/Magento/LoginAsCustomerAdminUi/Controller/Adminhtml/Login/Login.php

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\LoginAsCustomerUi\Controller\Adminhtml\Login;
8+
namespace Magento\LoginAsCustomerAdminUi\Controller\Adminhtml\Login;
99

1010
use Magento\Backend\App\Action;
1111
use Magento\Backend\App\Action\Context;
1212
use Magento\Backend\Model\Auth\Session;
1313
use Magento\Customer\Api\CustomerRepositoryInterface;
1414
use Magento\Framework\App\Action\HttpGetActionInterface;
15-
use Magento\Framework\App\Action\HttpPostActionInterface;
1615
use Magento\Framework\Controller\Result\Redirect;
1716
use Magento\Framework\Controller\ResultFactory;
1817
use Magento\Framework\Controller\ResultInterface;
@@ -22,19 +21,17 @@
2221
use Magento\LoginAsCustomerApi\Api\ConfigInterface;
2322
use Magento\LoginAsCustomerApi\Api\Data\AuthenticationDataInterface;
2423
use Magento\LoginAsCustomerApi\Api\Data\AuthenticationDataInterfaceFactory;
25-
use Magento\LoginAsCustomerApi\Api\DeleteExpiredAuthenticationDataInterface;
24+
use Magento\LoginAsCustomerApi\Api\DeleteAuthenticationDataForUserInterface;
2625
use Magento\LoginAsCustomerApi\Api\SaveAuthenticationDataInterface;
2726
use Magento\Store\Model\StoreManagerInterface;
2827

2928
/**
3029
* Login as customer action
3130
* Generate secret key and forward to the storefront action
3231
*
33-
* This action can be executed via GET request when "Store View To Login In" is disabled, and POST when it is enabled
34-
*
3532
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
3633
*/
37-
class Login extends Action implements HttpGetActionInterface, HttpPostActionInterface
34+
class Login extends Action implements HttpGetActionInterface
3835
{
3936
/**
4037
* Authorization level of a basic admin session
@@ -74,9 +71,9 @@ class Login extends Action implements HttpGetActionInterface, HttpPostActionInte
7471
private $saveAuthenticationData;
7572

7673
/**
77-
* @var DeleteExpiredAuthenticationDataInterface
74+
* @var DeleteAuthenticationDataForUserInterface
7875
*/
79-
private $deleteExpiredAuthenticationData;
76+
private $deleteAuthenticationDataForUser;
8077

8178
/**
8279
* @var Url
@@ -91,7 +88,7 @@ class Login extends Action implements HttpGetActionInterface, HttpPostActionInte
9188
* @param ConfigInterface $config
9289
* @param AuthenticationDataInterfaceFactory $authenticationDataFactory
9390
* @param SaveAuthenticationDataInterface $saveAuthenticationData ,
94-
* @param DeleteExpiredAuthenticationDataInterface $deleteExpiredAuthenticationData
91+
* @param DeleteAuthenticationDataForUserInterface $deleteAuthenticationDataForUser
9592
* @param Url $url
9693
*/
9794
public function __construct(
@@ -102,7 +99,7 @@ public function __construct(
10299
ConfigInterface $config,
103100
AuthenticationDataInterfaceFactory $authenticationDataFactory,
104101
SaveAuthenticationDataInterface $saveAuthenticationData,
105-
DeleteExpiredAuthenticationDataInterface $deleteExpiredAuthenticationData,
102+
DeleteAuthenticationDataForUserInterface $deleteAuthenticationDataForUser,
106103
Url $url
107104
) {
108105
parent::__construct($context);
@@ -113,7 +110,7 @@ public function __construct(
113110
$this->config = $config;
114111
$this->authenticationDataFactory = $authenticationDataFactory;
115112
$this->saveAuthenticationData = $saveAuthenticationData;
116-
$this->deleteExpiredAuthenticationData = $deleteExpiredAuthenticationData;
113+
$this->deleteAuthenticationDataForUser = $deleteAuthenticationDataForUser;
117114
$this->url = $url;
118115
}
119116

@@ -130,7 +127,7 @@ public function execute(): ResultInterface
130127
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
131128

132129
if (!$this->config->isEnabled()) {
133-
$this->messageManager->addErrorMessage(__('Login As Customer is disabled.'));
130+
$this->messageManager->addErrorMessage(__('Login as Customer is disabled.'));
134131
return $resultRedirect->setPath('customer/index/index');
135132
}
136133

@@ -140,16 +137,20 @@ public function execute(): ResultInterface
140137
}
141138

142139
try {
143-
$this->customerRepository->getById($customerId);
140+
$customer = $this->customerRepository->getById($customerId);
144141
} catch (NoSuchEntityException $e) {
145142
$this->messageManager->addErrorMessage(__('Customer with this ID are no longer exist.'));
146143
return $resultRedirect->setPath('customer/index/index');
147144
}
148145

149-
$storeId = (int)$this->_request->getParam('store_id');
150-
if (empty($storeId) && $this->config->isStoreManualChoiceEnabled()) {
151-
$this->messageManager->addNoticeMessage(__('Please select a Store View to login in.'));
152-
return $resultRedirect->setPath('customer/index/edit', ['id' => $customerId]);
146+
if ($this->config->isStoreManualChoiceEnabled()) {
147+
$storeId = (int)$this->_request->getParam('store_id');
148+
if (empty($storeId)) {
149+
$this->messageManager->addNoticeMessage(__('Please select a Store View to login in.'));
150+
return $resultRedirect->setPath('customer/index/edit', ['id' => $customerId]);
151+
}
152+
} else {
153+
$storeId = (int)$customer->getStoreId();
153154
}
154155

155156
$adminUser = $this->authSession->getUser();
@@ -164,7 +165,7 @@ public function execute(): ResultInterface
164165
]
165166
);
166167

167-
$this->deleteExpiredAuthenticationData->execute($userId);
168+
$this->deleteAuthenticationDataForUser->execute($userId);
168169
$secret = $this->saveAuthenticationData->execute($authenticationData);
169170

170171
$redirectUrl = $this->getLoginProceedRedirectUrl($secret, $storeId);
@@ -176,17 +177,13 @@ public function execute(): ResultInterface
176177
* Get login proceed redirect url
177178
*
178179
* @param string $secret
179-
* @param int|null $storeId
180+
* @param int $storeId
180181
* @return string
181182
* @throws NoSuchEntityException
182183
*/
183-
private function getLoginProceedRedirectUrl(string $secret, ?int $storeId): string
184+
private function getLoginProceedRedirectUrl(string $secret, int $storeId): string
184185
{
185-
if (null === $storeId) {
186-
$store = $this->storeManager->getDefaultStoreView();
187-
} else {
188-
$store = $this->storeManager->getStore($storeId);
189-
}
186+
$store = $this->storeManager->getStore($storeId);
190187

191188
return $this->url
192189
->setScope($store)

0 commit comments

Comments
 (0)