Skip to content

Commit f893209

Browse files
committed
magento2-login-as-customer/issues/104: Global refactoring
- \Magento\LoginAsCustomer\Api\DeleteAuthenticationDataBySecretInterface refactoring
1 parent daed603 commit f893209

File tree

5 files changed

+40
-44
lines changed

5 files changed

+40
-44
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\LoginAsCustomer\Api;
9+
10+
/**
11+
* Delete authentication data by secret
12+
*
13+
* @api
14+
*/
15+
interface DeleteAuthenticationDataBySecretInterface
16+
{
17+
/**
18+
* Delete authentication data by secret
19+
*
20+
* @param string $secret
21+
* @return void
22+
*/
23+
public function execute(string $secret): void;
24+
}

app/code/Magento/LoginAsCustomer/Api/DeleteSecretInterface.php

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

app/code/Magento/LoginAsCustomer/Model/ResourceModel/DeleteSecret.php renamed to app/code/Magento/LoginAsCustomer/Model/ResourceModel/DeleteAuthenticationDataBySecret.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
namespace Magento\LoginAsCustomer\Model\ResourceModel;
99

1010
use Magento\Framework\App\ResourceConnection;
11-
use Magento\LoginAsCustomer\Api\DeleteSecretInterface;
11+
use Magento\LoginAsCustomer\Api\DeleteAuthenticationDataBySecretInterface;
1212

1313
/**
14-
* @api
14+
* @inheritdoc
1515
*/
16-
class DeleteSecret implements DeleteSecretInterface
16+
class DeleteAuthenticationDataBySecret implements DeleteAuthenticationDataBySecretInterface
1717
{
1818
/**
1919
* @var ResourceConnection
@@ -30,17 +30,17 @@ public function __construct(
3030
}
3131

3232
/**
33-
* Delete old secret key records
33+
* @inheritdoc
3434
*/
35-
public function execute(string $secretKey):void
35+
public function execute(string $secret): void
3636
{
3737
$connection = $this->resourceConnection->getConnection();
3838
$tableName = $this->resourceConnection->getTableName('login_as_customer');
3939

4040
$connection->delete(
4141
$tableName,
4242
[
43-
'secret = ?' => $secretKey
43+
'secret = ?' => $secret
4444
]
4545
);
4646
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<preference for="Magento\LoginAsCustomer\Api\CreateSecretInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\CreateSecret" />
1111
<preference for="Magento\LoginAsCustomer\Api\GetAuthenticationDataBySecretInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\GetAuthenticationDataBySecret" />
1212
<preference for="Magento\LoginAsCustomer\Api\AuthenticateCustomerInterface" type="Magento\LoginAsCustomer\Model\AuthenticateCustomer" />
13-
<preference for="Magento\LoginAsCustomer\Api\DeleteSecretInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\DeleteSecret" />
13+
<preference for="Magento\LoginAsCustomer\Api\DeleteAuthenticationDataBySecretInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\DeleteAuthenticationDataBySecret" />
1414
<preference for="Magento\LoginAsCustomer\Api\DeleteOutdatedSecretsInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\DeleteOutdatedSecrets" />
1515
<preference for="Magento\LoginAsCustomer\Api\ConfigInterface" type="Magento\LoginAsCustomer\Model\Config" />
1616
</config>

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

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use Psr\Log\LoggerInterface;
2020
use Magento\LoginAsCustomer\Api\GetAuthenticationDataBySecretInterface;
2121
use Magento\LoginAsCustomer\Api\AuthenticateCustomerInterface;
22-
use Magento\LoginAsCustomer\Api\DeleteSecretInterface;
22+
use Magento\LoginAsCustomer\Api\DeleteAuthenticationDataBySecretInterface;
2323

2424
/**
2525
* Login As Customer storefront login action
@@ -52,9 +52,9 @@ class Index implements HttpGetActionInterface
5252
private $authenticateCustomer;
5353

5454
/**
55-
* @var DeleteSecretInterface
55+
* @var DeleteAuthenticationDataBySecretInterface
5656
*/
57-
private $deleteSecretProcessor;
57+
private $deleteAuthenticationDataBySecret;
5858

5959
/**
6060
* @var ManagerInterface
@@ -72,7 +72,7 @@ class Index implements HttpGetActionInterface
7272
* @param CustomerRepositoryInterface $customerRepository
7373
* @param GetAuthenticationDataBySecretInterface $getAuthenticateDataProcessor
7474
* @param AuthenticateCustomerInterface $authenticateCustomerProcessor
75-
* @param DeleteSecretInterface $deleteSecretProcessor
75+
* @param DeleteAuthenticationDataBySecretInterface $deleteSecretProcessor
7676
* @param ManagerInterface $messageManager
7777
* @param LoggerInterface $logger
7878
*/
@@ -82,7 +82,7 @@ public function __construct(
8282
CustomerRepositoryInterface $customerRepository,
8383
GetAuthenticationDataBySecretInterface $getAuthenticateDataProcessor,
8484
AuthenticateCustomerInterface $authenticateCustomerProcessor,
85-
DeleteSecretInterface $deleteSecretProcessor,
85+
DeleteAuthenticationDataBySecretInterface $deleteSecretProcessor,
8686
ManagerInterface $messageManager,
8787
LoggerInterface $logger
8888
) {
@@ -91,7 +91,7 @@ public function __construct(
9191
$this->customerRepository = $customerRepository;
9292
$this->getAuthenticationDataBySecret = $getAuthenticateDataProcessor;
9393
$this->authenticateCustomer = $authenticateCustomerProcessor;
94-
$this->deleteSecretProcessor = $deleteSecretProcessor;
94+
$this->deleteAuthenticationDataBySecret = $deleteSecretProcessor;
9595
$this->messageManager = $messageManager;
9696
$this->logger = $logger;
9797
}
@@ -108,30 +108,21 @@ public function execute(): ResultInterface
108108

109109
try {
110110
$secret = $this->request->getParam('secret');
111-
if (!$secret || !is_string($secret)) {
111+
if (empty($secret) || !is_string($secret)) {
112112
throw new LocalizedException(__('Cannot login to account. No secret key provided.'));
113113
}
114114

115115
$authenticateData = $this->getAuthenticationDataBySecret->execute($secret);
116116

117-
$this->deleteSecretProcessor->execute($secret);
117+
$this->deleteAuthenticationDataBySecret->execute($secret);
118118

119119
try {
120120
$customer = $this->customerRepository->getById($authenticateData->getCustomerId());
121121
} catch (NoSuchEntityException $e) {
122122
throw new LocalizedException(__('Customer are no longer exist.'));
123123
}
124124

125-
$loggedIn = $this->authenticateCustomer->execute(
126-
$authenticateData->getCustomerId(),
127-
$authenticateData->getAdminId()
128-
);
129-
130-
131-
if (!$loggedIn) {
132-
throw new LocalizedException(__('Login was not successful.'));
133-
}
134-
125+
$this->authenticateCustomer->execute($authenticateData);
135126

136127
$this->messageManager->addSuccessMessage(
137128
__('You are logged in as customer: %1', $customer->getFirstname() . ' ' . $customer->getLastname())

0 commit comments

Comments
 (0)