Skip to content

Commit 1396ad5

Browse files
committed
magento2-login-as-customer/issues/154: LoginAsCustomer API/extension points refactoring
1 parent cd047b2 commit 1396ad5

File tree

80 files changed

+841
-209
lines changed

Some content is hidden

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

80 files changed

+841
-209
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/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: 2 additions & 8 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

1010
use Magento\Framework\Serialize\Serializer\Json;
1111
use Magento\Store\Ui\Component\Listing\Column\Store\Options as StoreOptions;
1212
use Magento\Backend\Block\Template;
1313
use Magento\LoginAsCustomerApi\Api\ConfigInterface;
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;

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

Lines changed: 13 additions & 23 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,16 +21,14 @@
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
*/
3734
class Login extends Action implements HttpGetActionInterface
@@ -74,9 +71,9 @@ class Login extends Action implements HttpGetActionInterface
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
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

@@ -146,13 +143,14 @@ public function execute(): ResultInterface
146143
return $resultRedirect->setPath('customer/index/index');
147144
}
148145

149-
$storeId = null;
150146
if ($this->config->isStoreManualChoiceEnabled()) {
151147
$storeId = (int)$this->_request->getParam('store_id');
152148
if (empty($storeId)) {
153149
$this->messageManager->addNoticeMessage(__('Please select a Store View to login in.'));
154150
return $resultRedirect->setPath('customer/index/edit', ['id' => $customerId]);
155151
}
152+
} else {
153+
$storeId = (int)$customer->getStoreId();
156154
}
157155

158156
$adminUser = $this->authSession->getUser();
@@ -167,13 +165,9 @@ public function execute(): ResultInterface
167165
]
168166
);
169167

170-
$this->deleteExpiredAuthenticationData->execute($userId);
168+
$this->deleteAuthenticationDataForUser->execute($userId);
171169
$secret = $this->saveAuthenticationData->execute($authenticationData);
172170

173-
if (empty($storeId)) {
174-
$storeId = (int)$customer->getStoreId();
175-
}
176-
177171
$redirectUrl = $this->getLoginProceedRedirectUrl($secret, $storeId);
178172
$resultRedirect->setUrl($redirectUrl);
179173
return $resultRedirect;
@@ -183,17 +177,13 @@ public function execute(): ResultInterface
183177
* Get login proceed redirect url
184178
*
185179
* @param string $secret
186-
* @param int|null $storeId
180+
* @param int $storeId
187181
* @return string
188182
* @throws NoSuchEntityException
189183
*/
190-
private function getLoginProceedRedirectUrl(string $secret, ?int $storeId): string
184+
private function getLoginProceedRedirectUrl(string $secret, int $storeId): string
191185
{
192-
if (empty($storeId)) {
193-
$store = $this->storeManager->getDefaultStoreView();
194-
} else {
195-
$store = $this->storeManager->getStore($storeId);
196-
}
186+
$store = $this->storeManager->getStore($storeId);
197187

198188
return $this->url
199189
->setScope($store)

0 commit comments

Comments
 (0)