Skip to content

Commit 085746e

Browse files
committed
magento2-login-as-customer/issues/104: Global refactoring
- SaveAuthenticationDataInterface refactoring
1 parent 7b468d2 commit 085746e

File tree

5 files changed

+125
-64
lines changed

5 files changed

+125
-64
lines changed

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

Lines changed: 0 additions & 22 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+
* Save authentication data. Return secret key
15+
*
16+
* @api
17+
*/
18+
interface SaveAuthenticationDataInterface
19+
{
20+
/**
21+
* Save authentication data. Return secret key
22+
*
23+
* @param Data\AuthenticationDataInterface $authenticationData
24+
* @return string
25+
* @throws LocalizedException
26+
*/
27+
public function execute(AuthenticationDataInterface $authenticationData): string;
28+
}

app/code/Magento/LoginAsCustomer/Model/ResourceModel/CreateSecret.php renamed to app/code/Magento/LoginAsCustomer/Model/ResourceModel/SaveAuthenticationData.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
use Magento\Framework\App\ResourceConnection;
1111
use Magento\Framework\Stdlib\DateTime\DateTime;
1212
use Magento\Framework\Math\Random;
13-
use Magento\LoginAsCustomer\Api\CreateSecretInterface;
13+
use Magento\LoginAsCustomer\Api\Data\AuthenticationDataInterface;
14+
use Magento\LoginAsCustomer\Api\SaveAuthenticationDataInterface;
1415

1516
/**
16-
* @api
17+
* @inheritdoc
1718
*/
18-
class CreateSecret implements CreateSecretInterface
19+
class SaveAuthenticationData implements SaveAuthenticationDataInterface
1920
{
2021
/**
2122
* @var ResourceConnection
@@ -34,6 +35,8 @@ class CreateSecret implements CreateSecretInterface
3435

3536
/**
3637
* @param ResourceConnection $resourceConnection
38+
* @param DateTime $dateTime
39+
* @param Random $random
3740
*/
3841
public function __construct(
3942
ResourceConnection $resourceConnection,
@@ -46,12 +49,9 @@ public function __construct(
4649
}
4750

4851
/**
49-
* Create a new secret key
50-
* @return string
51-
* @param int $customerId
52-
* @param int $adminId
52+
* @inheritdoc
5353
*/
54-
public function execute(int $customerId, int $adminId):string
54+
public function execute(AuthenticationDataInterface $authenticationData): string
5555
{
5656
$connection = $this->resourceConnection->getConnection();
5757
$tableName = $this->resourceConnection->getTableName('login_as_customer');
@@ -61,13 +61,12 @@ public function execute(int $customerId, int $adminId):string
6161
$connection->insert(
6262
$tableName,
6363
[
64-
'customer_id' => $customerId,
65-
'admin_id' => $adminId,
64+
'customer_id' => $authenticationData->getCustomerId(),
65+
'admin_id' => $authenticationData->getAdminId(),
6666
'secret' => $secret,
6767
'created_at' => $this->dateTime->gmtDate(),
6868
]
6969
);
70-
7170
return $secret;
7271
}
7372
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
99
<preference for="Magento\LoginAsCustomer\Api\Data\AuthenticationDataInterface" type="Magento\LoginAsCustomer\Model\AuthenticationData"/>
10-
<preference for="Magento\LoginAsCustomer\Api\CreateSecretInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\CreateSecret"/>
10+
<preference for="Magento\LoginAsCustomer\Api\SaveAuthenticationDataInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\SaveAuthenticationData"/>
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"/>
1313
<preference for="Magento\LoginAsCustomer\Api\DeleteAuthenticationDataBySecretInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\DeleteAuthenticationDataBySecret"/>

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

Lines changed: 86 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,28 @@
77

88
namespace Magento\LoginAsCustomerUi\Controller\Adminhtml\Login;
99

10+
use Magento\Framework\App\RequestInterface;
11+
use Magento\Framework\Controller\Result\Redirect;
12+
use Magento\Framework\Controller\ResultFactory;
1013
use Magento\Framework\Controller\ResultInterface;
1114
use Magento\Framework\App\Action\HttpGetActionInterface;
1215
use Magento\Framework\App\Action\HttpPostActionInterface;
13-
use Magento\Backend\App\Action;
1416
use Magento\Customer\Api\CustomerRepositoryInterface;
17+
use Magento\Framework\Exception\LocalizedException;
1518
use Magento\Framework\Exception\NoSuchEntityException;
19+
use Magento\Framework\Message\ManagerInterface;
1620
use Magento\LoginAsCustomer\Api\ConfigInterface;
17-
use Magento\LoginAsCustomer\Api\CreateSecretInterface;
21+
use Magento\LoginAsCustomer\Api\Data\AuthenticationDataInterface;
22+
use Magento\LoginAsCustomer\Api\Data\AuthenticationDataInterfaceFactor;
23+
use Magento\LoginAsCustomer\Api\SaveAuthenticationDataInterface;
1824

1925
/**
2026
* Login as customer action
2127
* Generate secret key and forward to the storefront action
2228
*
2329
* This action can be executed via GET request when "Store View To Login In" is disabled, and POST when it is enabled
2430
*/
25-
class Login extends Action implements HttpGetActionInterface, HttpPostActionInterface
31+
class Login implements HttpGetActionInterface, HttpPostActionInterface
2632
{
2733
/**
2834
* Authorization level of a basic admin session
@@ -31,6 +37,21 @@ class Login extends Action implements HttpGetActionInterface, HttpPostActionInte
3137
*/
3238
const ADMIN_RESOURCE = 'Magento_LoginAsCustomerUi::login_button';
3339

40+
/**
41+
* @var ResultFactory
42+
*/
43+
private $resultFactory;
44+
45+
/**
46+
* @var RequestInterface
47+
*/
48+
private $request;
49+
50+
/**
51+
* @var ManagerInterface
52+
*/
53+
private $messageManager;
54+
3455
/**
3556
* @var \Magento\Backend\Model\Auth\Session
3657
*/
@@ -57,58 +78,71 @@ class Login extends Action implements HttpGetActionInterface, HttpPostActionInte
5778
private $config;
5879

5980
/**
60-
* @var CreateSecretInterface
81+
* @var AuthenticationDataInterfaceFactory
6182
*/
62-
private $createSecretProcessor;
83+
private $authenticationDataFactory;
6384

6485
/**
65-
* Login constructor.
66-
* @param \Magento\Backend\App\Action\Context $context
86+
* @var SaveAuthenticationDataInterface
87+
*/
88+
private $saveAuthenticationData;
89+
90+
/**
91+
* @param ResultFactory $resultFactory
92+
* @param RequestInterface $request
93+
* @param ManagerInterface $messageManager
6794
* @param \Magento\Backend\Model\Auth\Session $authSession
6895
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
6996
* @param \Magento\Framework\Url $url
7097
* @param CustomerRepositoryInterface $customerRepository
71-
* @param ConfigInterface $config,
72-
* @param CreateSecretInterface $createSecretProcessor
98+
* @param ConfigInterface $config
99+
* @param AuthenticationDataInterfaceFactory $authenticationDataFactory
100+
* @param SaveAuthenticationDataInterface $saveAuthenticationData
73101
*/
74102
public function __construct(
75-
\Magento\Backend\App\Action\Context $context,
103+
ResultFactory $resultFactory,
104+
RequestInterface $request,
105+
ManagerInterface $messageManager,
76106
\Magento\Backend\Model\Auth\Session $authSession,
77107
\Magento\Store\Model\StoreManagerInterface $storeManager,
78108
\Magento\Framework\Url $url,
79109
CustomerRepositoryInterface $customerRepository,
80110
ConfigInterface $config,
81-
CreateSecretInterface $createSecretProcessor
111+
AuthenticationDataInterfaceFactory $authenticationDataFactory,
112+
SaveAuthenticationDataInterface $saveAuthenticationData
82113
) {
83-
parent::__construct($context);
114+
$this->resultFactory = $resultFactory;
115+
$this->request = $request;
116+
$this->messageManager = $messageManager;
84117
$this->authSession = $authSession;
85118
$this->storeManager = $storeManager;
86119
$this->url = $url;
87120
$this->customerRepository = $customerRepository;
88121
$this->config = $config;
89-
$this->createSecretProcessor = $createSecretProcessor;
122+
$this->authenticationDataFactory = $authenticationDataFactory;
123+
$this->saveAuthenticationData = $saveAuthenticationData;
90124
}
91125

92126
/**
93127
* Login as customer
94128
*
95129
* @return ResultInterface
130+
* @throws NoSuchEntityException
131+
* @throws LocalizedException
96132
*/
97133
public function execute(): ResultInterface
98134
{
99-
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
100-
$resultRedirect = $this->resultRedirectFactory->create();
135+
/** @var Redirect $resultRedirect */
136+
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
101137

102138
if (!$this->config->isEnabled()) {
103139
$this->messageManager->addErrorMessage(__('Login As Customer is disabled.'));
104140
return $resultRedirect->setPath('customer/index/index');
105141
}
106142

107-
$request = $this->getRequest();
108-
109-
$customerId = (int) $request->getParam('customer_id');
143+
$customerId = (int)$this->request->getParam('customer_id');
110144
if (!$customerId) {
111-
$customerId = (int) $request->getParam('entity_id');
145+
$customerId = (int)$this->request->getParam('entity_id');
112146
}
113147

114148
try {
@@ -118,24 +152,46 @@ public function execute(): ResultInterface
118152
return $resultRedirect->setPath('customer/index/index');
119153
}
120154

121-
$customerStoreId = $request->getParam('store_id');
122-
if (!isset($customerStoreId) && $this->config->isStoreManualChoiceEnabled()) {
155+
$storeId = $this->request->getParam('store_id');
156+
if (empty($storeId) && $this->config->isStoreManualChoiceEnabled()) {
123157
$this->messageManager->addNoticeMessage(__('Please select a Store View to login in.'));
124-
return $resultRedirect->setPath('loginascustomer/login/manual', ['entity_id' => $customerId ]);
158+
return $resultRedirect->setPath('loginascustomer/login/manual', ['customer_id' => $customerId]);
125159
}
126160

161+
$adminUser = $this->authSession->getUser();
162+
163+
/** @var AuthenticationDataInterface $authenticationData */
164+
$authenticationData = $this->authenticationDataFactory->create(
165+
[
166+
'customerId' => $customerId,
167+
'adminId' => (int)$adminUser->getId(),
168+
'extensionAttributes' => null,
169+
]
170+
);
171+
$secret = $this->saveAuthenticationData->execute($authenticationData);
172+
173+
$redirectUrl = $this->getLoginProceedRedirectUrl($secret, $storeId);
174+
$resultRedirect->setUrl($redirectUrl);
175+
return $resultRedirect;
176+
}
127177

128-
$user = $this->authSession->getUser();
129-
$secret = $this->createSecretProcessor->execute($customerId, (int)$user->getId());
130-
131-
$store = $this->storeManager->getStore();
132-
if (null === $store) {
178+
/**
179+
* @param string $secret
180+
* @param int|null $storeId
181+
* @return string
182+
* @throws NoSuchEntityException
183+
*/
184+
private function getLoginProceedRedirectUrl(string $secret, ?int $storeId): string
185+
{
186+
if (null === $storeId) {
133187
$store = $this->storeManager->getDefaultStoreView();
188+
} else {
189+
$store = $this->storeManager->getStore($storeId);
134190
}
135191

136-
$redirectUrl = $this->url->setScope($store)
192+
$redirectUrl = $this->url
193+
->setScope($store)
137194
->getUrl('loginascustomer/login/index', ['secret' => $secret, '_nosid' => true]);
138-
139-
return $resultRedirect->setUrl($redirectUrl);
195+
return $redirectUrl;
140196
}
141197
}

0 commit comments

Comments
 (0)