Skip to content

Commit b994aa2

Browse files
committed
magento2-login-as-customer/issues/104: Global refactoring
- Introduce Magento\LoginAsCustomer\Api\Data\AuthenticationDataInterface
1 parent 0c65df3 commit b994aa2

File tree

7 files changed

+178
-48
lines changed

7 files changed

+178
-48
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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\Data;
9+
10+
use Magento\Framework\Api\ExtensibleDataInterface;
11+
12+
/**
13+
* Authentication data
14+
*
15+
* @api
16+
*/
17+
interface AuthenticationDataInterface extends ExtensibleDataInterface
18+
{
19+
/**
20+
* Get Customer Id
21+
*
22+
* @return int
23+
*/
24+
public function getCustomerId(): int;
25+
26+
/**
27+
* Get Admin Id
28+
*
29+
* @return int
30+
*/
31+
public function getAdminId(): int;
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
public function getExtensionAttributes(): ?AuthenticationDataExtensionInterface;
37+
}

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

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\LoginAsCustomer\Api\Data\AuthenticationDataInterface;
12+
13+
/**
14+
* Get authentication data by secret
15+
*
16+
* @api
17+
*/
18+
interface GetAuthenticationDataBySecretInterface
19+
{
20+
/**
21+
* Load login details based on secret key
22+
*
23+
* @param string $secretKey
24+
* @return AuthenticationDataInterface
25+
* @throws LocalizedException
26+
*/
27+
public function execute(string $secretKey): AuthenticationDataInterface;
28+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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\Model;
9+
10+
use Magento\LoginAsCustomer\Api\Data\AuthenticationDataInterface;
11+
use Magento\LoginAsCustomer\Api\Data\AuthenticationDataExtensionInterface;
12+
13+
/**
14+
* @inheritdoc
15+
*/
16+
class AuthenticationData implements AuthenticationDataInterface
17+
{
18+
/**
19+
* @var int
20+
*/
21+
private $customerId;
22+
23+
/**
24+
* @var int
25+
*/
26+
private $adminId;
27+
28+
/**
29+
* @var AuthenticationDataExtensionInterface|null
30+
*/
31+
private $extensionAttributes;
32+
33+
/**
34+
* @param int $customerId
35+
* @param int $adminId
36+
* @param AuthenticationDataExtensionInterface|null $extensionAttributes
37+
*/
38+
public function __construct(
39+
int $customerId,
40+
int $adminId,
41+
AuthenticationDataExtensionInterface $extensionAttributes = null
42+
) {
43+
$this->customerId = $customerId;
44+
$this->adminId = $adminId;
45+
$this->extensionAttributes = $extensionAttributes;
46+
}
47+
48+
/**
49+
* @return int
50+
*/
51+
public function getCustomerId(): int
52+
{
53+
return $this->customerId;
54+
}
55+
56+
/**
57+
* @return int
58+
*/
59+
public function getAdminId(): int
60+
{
61+
return $this->adminId;
62+
}
63+
64+
/**
65+
* @inheritdoc
66+
*/
67+
public function getExtensionAttributes(): ?AuthenticationDataExtensionInterface
68+
{
69+
return $this->extensionAttributes;
70+
}
71+
}

app/code/Magento/LoginAsCustomer/Model/ResourceModel/GetAuthenticateData.php renamed to app/code/Magento/LoginAsCustomer/Model/ResourceModel/GetAuthenticationDataBySecret.php

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111
use Magento\Framework\Stdlib\DateTime\DateTime;
1212
use Magento\Framework\Exception\LocalizedException;
1313
use Magento\LoginAsCustomer\Api\ConfigInterface;
14-
use Magento\LoginAsCustomer\Api\GetAuthenticateDataInterface;
14+
use Magento\LoginAsCustomer\Api\Data\AuthenticationDataInterface;
15+
use Magento\LoginAsCustomer\Api\Data\AuthenticationDataInterfaceFactor;
16+
use Magento\LoginAsCustomer\Api\GetAuthenticationDataBySecretInterface;
1517

1618
/**
17-
* @api
19+
* @inheritdoc
1820
*/
19-
class GetAuthenticateData implements GetAuthenticateDataInterface
21+
class GetAuthenticationDataBySecret implements GetAuthenticationDataBySecretInterface
2022
{
2123
/**
2224
* @var ResourceConnection
@@ -33,28 +35,33 @@ class GetAuthenticateData implements GetAuthenticateDataInterface
3335
*/
3436
private $config;
3537

38+
/**
39+
* @var AuthenticationDataInterfaceFactor
40+
*/
41+
private $authenticationDataFactory;
42+
3643
/**
3744
* @param ResourceConnection $resourceConnection
3845
* @param DateTime $dateTime
3946
* @param ConfigInterface $config
47+
* @param AuthenticationDataInterfaceFactory $authenticationDataFactory
4048
*/
4149
public function __construct(
4250
ResourceConnection $resourceConnection,
4351
DateTime $dateTime,
44-
ConfigInterface $config
52+
ConfigInterface $config,
53+
AuthenticationDataInterfaceFactory $authenticationDataFactory
4554
) {
4655
$this->resourceConnection = $resourceConnection;
4756
$this->dateTime = $dateTime;
4857
$this->config = $config;
58+
$this->authenticationDataFactory = $authenticationDataFactory;
4959
}
5060

5161
/**
52-
* Load logic details based on secret key
53-
* @return array
54-
* @throws LocalizedException
55-
* @param string $secretKey
62+
* @inheritdoc
5663
*/
57-
public function execute(string $secretKey):array
64+
public function execute(string $secretKey): AuthenticationDataInterface
5865
{
5966
$connection = $this->resourceConnection->getConnection();
6067
$tableName = $this->resourceConnection->getTableName('login_as_customer');
@@ -64,16 +71,22 @@ public function execute(string $secretKey):array
6471
$select = $connection->select()
6572
->from(['main_table' => $tableName])
6673
->where('main_table.secret = ?', $secretKey)
67-
->where('main_table.created_at > ?', $timePoint)
68-
->limit(1);
74+
->where('main_table.created_at > ?', $timePoint);
6975

7076
$data = $connection->fetchRow($select);
7177

7278
if (!$data) {
73-
throw new LocalizedException(__('Secret key is not valid.'));
79+
throw new LocalizedException(__('Secret key is not found or was expired.'));
7480
}
7581

76-
77-
return $data;
82+
/** @var AuthenticationDataInterface $authenticationData */
83+
$authenticationData = $this->authenticationDataFactory->create(
84+
[
85+
'customerId' => (int)$data['admin_id'],
86+
'adminId' => (int)$data['customer_id'],
87+
'extensionAttributes' => null,
88+
]
89+
);
90+
return $authenticationData;
7891
}
7992
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<preference for="Magento\LoginAsCustomer\Api\Data\AuthenticationDataInterface" type="Magento\LoginAsCustomer\Model\AuthenticationData" />
910
<preference for="Magento\LoginAsCustomer\Api\CreateSecretInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\CreateSecret" />
10-
<preference for="Magento\LoginAsCustomer\Api\GetAuthenticateDataInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\GetAuthenticateData" />
11+
<preference for="Magento\LoginAsCustomer\Api\GetAuthenticationDataBySecretInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\GetAuthenticationDataBySecret" />
1112
<preference for="Magento\LoginAsCustomer\Api\AuthenticateCustomerInterface" type="Magento\LoginAsCustomer\Model\AuthenticateCustomer" />
1213
<preference for="Magento\LoginAsCustomer\Api\DeleteSecretInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\DeleteSecret" />
1314
<preference for="Magento\LoginAsCustomer\Api\DeleteOutdatedSecretsInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\DeleteOutdatedSecrets" />

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Magento\Framework\App\Action\HttpGetActionInterface;
1818
use Magento\Framework\Message\ManagerInterface;
1919
use Psr\Log\LoggerInterface;
20-
use Magento\LoginAsCustomer\Api\GetAuthenticateDataInterface;
20+
use Magento\LoginAsCustomer\Api\GetAuthenticationDataBySecretInterface;
2121
use Magento\LoginAsCustomer\Api\AuthenticateCustomerInterface;
2222
use Magento\LoginAsCustomer\Api\DeleteSecretInterface;
2323

@@ -42,14 +42,14 @@ class Index implements HttpGetActionInterface
4242
private $customerRepository;
4343

4444
/**
45-
* @var GetAuthenticateDataInterface
45+
* @var GetAuthenticationDataBySecretInterface
4646
*/
47-
private $getAuthenticateDataProcessor;
47+
private $getAuthenticationDataBySecret;
4848

4949
/**
5050
* @var AuthenticateCustomerInterface
5151
*/
52-
private $authenticateCustomerProcessor;
52+
private $authenticateCustomer;
5353

5454
/**
5555
* @var DeleteSecretInterface
@@ -70,7 +70,7 @@ class Index implements HttpGetActionInterface
7070
* @param ResultFactory $resultFactory
7171
* @param RequestInterface $request
7272
* @param CustomerRepositoryInterface $customerRepository
73-
* @param GetAuthenticateDataInterface $getAuthenticateDataProcessor
73+
* @param GetAuthenticationDataBySecretInterface $getAuthenticateDataProcessor
7474
* @param AuthenticateCustomerInterface $authenticateCustomerProcessor
7575
* @param DeleteSecretInterface $deleteSecretProcessor
7676
* @param ManagerInterface $messageManager
@@ -80,7 +80,7 @@ public function __construct(
8080
ResultFactory $resultFactory,
8181
RequestInterface $request,
8282
CustomerRepositoryInterface $customerRepository,
83-
GetAuthenticateDataInterface $getAuthenticateDataProcessor,
83+
GetAuthenticationDataBySecretInterface $getAuthenticateDataProcessor,
8484
AuthenticateCustomerInterface $authenticateCustomerProcessor,
8585
DeleteSecretInterface $deleteSecretProcessor,
8686
ManagerInterface $messageManager,
@@ -89,8 +89,8 @@ public function __construct(
8989
$this->resultFactory = $resultFactory;
9090
$this->request = $request;
9191
$this->customerRepository = $customerRepository;
92-
$this->getAuthenticateDataProcessor = $getAuthenticateDataProcessor;
93-
$this->authenticateCustomerProcessor = $authenticateCustomerProcessor;
92+
$this->getAuthenticationDataBySecret = $getAuthenticateDataProcessor;
93+
$this->authenticateCustomer = $authenticateCustomerProcessor;
9494
$this->deleteSecretProcessor = $deleteSecretProcessor;
9595
$this->messageManager = $messageManager;
9696
$this->logger = $logger;
@@ -112,20 +112,19 @@ public function execute(): ResultInterface
112112
throw new LocalizedException(__('Cannot login to account. No secret key provided.'));
113113
}
114114

115-
/* Can throw LocalizedException */
116-
$authenticateData = $this->getAuthenticateDataProcessor->execute($secret);
115+
$authenticateData = $this->getAuthenticationDataBySecret->execute($secret);
117116

118117
$this->deleteSecretProcessor->execute($secret);
119118

120119
try {
121-
$customer = $this->customerRepository->getById($authenticateData['customer_id']);
120+
$customer = $this->customerRepository->getById($authenticateData->getCustomerId());
122121
} catch (NoSuchEntityException $e) {
123122
throw new LocalizedException(__('Customer are no longer exist.'));
124123
}
125124

126-
$loggedIn = $this->authenticateCustomerProcessor->execute(
127-
(int)$authenticateData['customer_id'],
128-
(int)$authenticateData['admin_id']
125+
$loggedIn = $this->authenticateCustomer->execute(
126+
$authenticateData->getCustomerId(),
127+
$authenticateData->getAdminId()
129128
);
130129

131130

0 commit comments

Comments
 (0)