Skip to content

Commit c1dee1f

Browse files
authored
Merge pull request #98 from ihorvansach/refactor-code-microservices
Refactor LoginAsCustomer Login Model
2 parents 143168f + bcf441f commit c1dee1f

21 files changed

+617
-303
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
* @api
12+
*/
13+
interface AuthenticateCustomerInterface
14+
{
15+
/**
16+
* Authenticate a customer by customer ID
17+
*
18+
* @return bool
19+
* @param int $customerId
20+
* @param int $adminId
21+
*/
22+
public function execute(int $customerId, int $adminId):bool;
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
* @api
12+
*/
13+
interface CreateSecretInterface
14+
{
15+
/**
16+
* Create a new secret key
17+
* @return string
18+
* @param int $customerId
19+
* @param int $adminId
20+
*/
21+
public function execute(int $customerId, int $adminId):string;
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
* @api
12+
*/
13+
interface DeleteOldSecretsInterface
14+
{
15+
/**
16+
* Delete old secret key records
17+
*/
18+
public function execute():void;
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
* @api
12+
*/
13+
interface DeleteSecretInterface
14+
{
15+
/**
16+
* Delete secret key
17+
*/
18+
public function execute(string $secretKey):void;
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
* @api
12+
*/
13+
interface GetAuthenticateDataInterface
14+
{
15+
/**
16+
* Load login details based on secret key
17+
*/
18+
public function execute(string $secretKey):array;
19+
}

app/code/Magento/LoginAsCustomer/Controller/Adminhtml/Login/Index.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77

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

10-
use Magento\Backend\App\Action\Context;
1110
use Magento\Backend\Model\View\Result\Page;
1211
use Magento\Framework\Controller\ResultFactory;
1312
use Magento\Framework\Controller\ResultInterface;
1413
use Magento\Framework\App\Action\HttpGetActionInterface;
1514
use Magento\Framework\App\Action\HttpPostActionInterface;
1615
use Magento\Backend\App\Action;
17-
use Magento\LoginAsCustomer\Model\Login;
1816

1917
/**
2018
* Login As Customer log grid action
@@ -29,23 +27,6 @@ class Index extends Action implements HttpGetActionInterface, HttpPostActionInte
2927
*/
3028
const ADMIN_RESOURCE = 'Magento_LoginAsCustomer::login_log';
3129

32-
/**
33-
* @var Login
34-
*/
35-
private $loginModel;
36-
37-
/**
38-
* @param Context $context
39-
* @param Login $loginModel
40-
*/
41-
public function __construct(
42-
Context $context,
43-
Login $loginModel
44-
) {
45-
parent::__construct($context);
46-
$this->loginModel = $loginModel;
47-
}
48-
4930
/**
5031
* Login As Customer log grid action
5132
*
@@ -59,8 +40,6 @@ public function execute():ResultInterface
5940
return $resultForward;
6041
}
6142

62-
$this->loginModel->deleteNotUsed();
63-
6443
/** @var Page $resultPage */
6544
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
6645
$resultPage->setActiveMenu('Magento_LoginAsCustomer::login_log')

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

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
use Magento\Framework\App\Action\HttpGetActionInterface;
1212
use Magento\Framework\App\Action\HttpPostActionInterface;
1313
use Magento\Backend\App\Action;
14+
use Magento\Customer\Api\CustomerRepositoryInterface;
15+
use Magento\Framework\Exception\NoSuchEntityException;
16+
use Magento\LoginAsCustomer\Api\CreateSecretInterface;
1417

1518
/**
1619
* Login as customer action
@@ -27,11 +30,6 @@ class Login extends Action implements HttpGetActionInterface, HttpPostActionInte
2730
*/
2831
const ADMIN_RESOURCE = 'Magento_LoginAsCustomer::login_button';
2932

30-
/**
31-
* @var \Magento\LoginAsCustomer\Model\Login
32-
*/
33-
private $loginModel;
34-
3533
/**
3634
* @var \Magento\Backend\Model\Auth\Session
3735
*/
@@ -47,34 +45,47 @@ class Login extends Action implements HttpGetActionInterface, HttpPostActionInte
4745
*/
4846
private $url;
4947

48+
/**
49+
* @var CustomerRepositoryInterface
50+
*/
51+
private $customerRepository;
52+
5053
/**
5154
* @var \Magento\LoginAsCustomer\Model\Config
5255
*/
5356
private $config;
5457

58+
/**
59+
* @var CreateSecretInterface
60+
*/
61+
private $createSecretProcessor;
62+
5563
/**
5664
* Login constructor.
5765
* @param \Magento\Backend\App\Action\Context $context
58-
* @param \Magento\LoginAsCustomer\Model\Login $loginModel
5966
* @param \Magento\Backend\Model\Auth\Session $authSession
6067
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
6168
* @param \Magento\Framework\Url $url
62-
* @param \Magento\LoginAsCustomer\Model\Config $config
69+
* @param CustomerRepositoryInterface $customerRepository
70+
* @param \Magento\LoginAsCustomer\Model\Config $config,
71+
* @param CreateSecretInterface $createSecretProcessor
6372
*/
6473
public function __construct(
6574
\Magento\Backend\App\Action\Context $context,
66-
\Magento\LoginAsCustomer\Model\Login $loginModel,
6775
\Magento\Backend\Model\Auth\Session $authSession,
6876
\Magento\Store\Model\StoreManagerInterface $storeManager,
6977
\Magento\Framework\Url $url,
70-
\Magento\LoginAsCustomer\Model\Config $config
78+
CustomerRepositoryInterface $customerRepository,
79+
\Magento\LoginAsCustomer\Model\Config $config,
80+
CreateSecretInterface $createSecretProcessor
7181
) {
7282
parent::__construct($context);
73-
$this->loginModel = $loginModel;
7483
$this->authSession = $authSession;
7584
$this->storeManager = $storeManager;
7685
$this->url = $url;
86+
$this->customerRepository = $customerRepository;
7787
$this->config = $config;
88+
$this->createSecretProcessor = $createSecretProcessor;
7889
}
7990

8091
/**
@@ -84,48 +95,45 @@ public function __construct(
8495
*/
8596
public function execute(): ResultInterface
8697
{
98+
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
99+
$resultRedirect = $this->resultRedirectFactory->create();
100+
101+
if (!$this->config->isEnabled()) {
102+
$this->messageManager->addErrorMessage(__('Login As Customer is disabled.'));
103+
return $resultRedirect->setPath('customer/index/index');
104+
}
105+
87106
$request = $this->getRequest();
107+
88108
$customerId = (int) $request->getParam('customer_id');
89109
if (!$customerId) {
90110
$customerId = (int) $request->getParam('entity_id');
91111
}
92112

93-
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
94-
$resultRedirect = $this->resultRedirectFactory->create();
95-
96-
if (!$this->config->isEnabled()) {
97-
$this->messageManager->addErrorMessage(__('Login As Customer is disabled.'));
113+
try {
114+
$customer = $this->customerRepository->getById($customerId);
115+
} catch (NoSuchEntityException $e) {
116+
$this->messageManager->addErrorMessage(__('Customer with this ID are no longer exist.'));
98117
return $resultRedirect->setPath('customer/index/index');
99118
}
100119

101120
$customerStoreId = $request->getParam('store_id');
102-
103121
if (!isset($customerStoreId) && $this->config->isManualChoiceEnabled()) {
104122
$this->messageManager->addNoticeMessage(__('Please select a Store View to login in.'));
105123
return $resultRedirect->setPath('loginascustomer/login/manual', ['entity_id' => $customerId ]);
106124
}
107125

108-
$login = $this->loginModel->setCustomerId($customerId);
109-
110-
$login->deleteNotUsed();
111-
112-
$customer = $login->getCustomer();
113-
114-
if (!$customer->getId()) {
115-
$this->messageManager->addErrorMessage(__('Customer with this ID are no longer exist.'));
116-
return $resultRedirect->setPath('customer/index/index');
117-
}
118126

119127
$user = $this->authSession->getUser();
120-
$login->generate($user->getId());
121-
$store = $this->storeManager->getStore();
128+
$secret = $this->createSecretProcessor->execute($customerId, (int)$user->getId());
122129

130+
$store = $this->storeManager->getStore();
123131
if (null === $store) {
124132
$store = $this->storeManager->getDefaultStoreView();
125133
}
126134

127135
$redirectUrl = $this->url->setScope($store)
128-
->getUrl('loginascustomer/login/index', ['secret' => $login->getSecret(), '_nosid' => true]);
136+
->getUrl('loginascustomer/login/index', ['secret' => $secret, '_nosid' => true]);
129137

130138
return $resultRedirect->setUrl($redirectUrl);
131139
}

0 commit comments

Comments
 (0)