Skip to content

Commit 86ec81c

Browse files
magento/magento2-login-as-customer#188: Introduce Login as Customer "is enabled" check extensibility.
1 parent 74a48fd commit 86ec81c

File tree

12 files changed

+459
-72
lines changed

12 files changed

+459
-72
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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\LoginAsCustomerApi\Api\ConfigInterface;
11+
use Magento\LoginAsCustomerApi\Api\Data\IsLoginAsCustomerEnabledForCustomerResultInterface;
12+
use Magento\LoginAsCustomerApi\Api\IsLoginAsCustomerEnabledForCustomerInterface;
13+
use Magento\LoginAsCustomerApi\Model\IsLoginAsCustomerEnabledForCustomerResolverInterface;
14+
15+
/**
16+
* @inheritdoc
17+
*/
18+
class IsLoginAsCustomerEnabledForCustomerChain implements IsLoginAsCustomerEnabledForCustomerInterface
19+
{
20+
/**
21+
* @var ConfigInterface
22+
*/
23+
private $config;
24+
25+
/**
26+
* @var IsLoginAsCustomerEnabledForCustomerResultFactory
27+
*/
28+
private $resultFactory;
29+
30+
/**
31+
* @var IsLoginAsCustomerEnabledForCustomerResultInterface[]
32+
*/
33+
private $resolvers;
34+
35+
/**
36+
* @param ConfigInterface $config
37+
* @param IsLoginAsCustomerEnabledForCustomerResultFactory $resultFactory
38+
* @param array $resolvers
39+
*/
40+
public function __construct(
41+
ConfigInterface $config,
42+
IsLoginAsCustomerEnabledForCustomerResultFactory $resultFactory,
43+
array $resolvers = []
44+
) {
45+
$this->config = $config;
46+
$this->resultFactory = $resultFactory;
47+
$this->resolvers = $resolvers;
48+
}
49+
50+
/**
51+
* @inheritdoc
52+
*/
53+
public function execute(int $customerId): IsLoginAsCustomerEnabledForCustomerResultInterface
54+
{
55+
$messages = [[]];
56+
/** @var IsLoginAsCustomerEnabledForCustomerResultInterface $resolver */
57+
foreach ($this->resolvers as $resolver) {
58+
$resolverResult = $resolver->execute($customerId);
59+
if (!$resolverResult->isEnabled()) {
60+
$messages[] = $resolverResult->getMessages();
61+
}
62+
}
63+
64+
return $this->resultFactory->create(['messages' => array_merge(...$messages)]);
65+
}
66+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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\LoginAsCustomerApi\Api\Data\IsLoginAsCustomerEnabledForCustomerResultInterface;
11+
12+
/**
13+
* @inheritdoc
14+
*/
15+
class IsLoginAsCustomerEnabledForCustomerResult implements IsLoginAsCustomerEnabledForCustomerResultInterface
16+
{
17+
/**
18+
* @var string[]
19+
*/
20+
private $messages;
21+
22+
/**
23+
* @param array $messages
24+
*/
25+
public function __construct(array $messages = [])
26+
{
27+
$this->messages = $messages;
28+
}
29+
30+
/**
31+
* @inheritdoc
32+
*/
33+
public function isEnabled(): bool
34+
{
35+
return empty($this->messages);
36+
}
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
public function getMessages(): array
42+
{
43+
return $this->messages;
44+
}
45+
46+
/**
47+
* @inheritdoc
48+
*/
49+
public function setMessages(array $messages): void
50+
{
51+
$this->messages = $messages;
52+
}
53+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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\Resolver;
9+
10+
use Magento\LoginAsCustomer\Model\IsLoginAsCustomerEnabledForCustomerResultFactory;
11+
use Magento\LoginAsCustomerApi\Api\ConfigInterface;
12+
use Magento\LoginAsCustomerApi\Api\Data\IsLoginAsCustomerEnabledForCustomerResultInterface;
13+
use Magento\LoginAsCustomerApi\Api\IsLoginAsCustomerEnabledForCustomerInterface;
14+
15+
/**
16+
* @inheritdoc
17+
*/
18+
class IsLoginAsCustomerEnabledResolver implements IsLoginAsCustomerEnabledForCustomerInterface
19+
{
20+
/**
21+
* @var ConfigInterface
22+
*/
23+
private $config;
24+
25+
/**
26+
* @var IsLoginAsCustomerEnabledForCustomerResultFactory
27+
*/
28+
private $resultFactory;
29+
30+
/**
31+
* @param ConfigInterface $config
32+
* @param IsLoginAsCustomerEnabledForCustomerResultFactory $resultFactory
33+
*/
34+
public function __construct(
35+
ConfigInterface $config,
36+
IsLoginAsCustomerEnabledForCustomerResultFactory $resultFactory
37+
) {
38+
$this->config = $config;
39+
$this->resultFactory = $resultFactory;
40+
}
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
public function execute(int $customerId): IsLoginAsCustomerEnabledForCustomerResultInterface
46+
{
47+
$messages = [];
48+
if (!$this->config->isEnabled()) {
49+
$messages[] = __('Login as Customer is disabled.');
50+
}
51+
52+
return $this->resultFactory->create(['messages' => $messages]);
53+
}
54+
}

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,32 @@
55
* See COPYING.txt for license details.
66
*/
77
-->
8-
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9-
<preference for="Magento\LoginAsCustomerApi\Api\Data\AuthenticationDataInterface" type="Magento\LoginAsCustomer\Model\AuthenticationData"/>
10-
<preference for="Magento\LoginAsCustomerApi\Api\SaveAuthenticationDataInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\SaveAuthenticationData"/>
11-
<preference for="Magento\LoginAsCustomerApi\Api\GetAuthenticationDataBySecretInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\GetAuthenticationDataBySecret"/>
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"/>
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
10+
<preference for="Magento\LoginAsCustomerApi\Api\Data\AuthenticationDataInterface"
11+
type="Magento\LoginAsCustomer\Model\AuthenticationData"/>
12+
<preference for="Magento\LoginAsCustomerApi\Api\SaveAuthenticationDataInterface"
13+
type="Magento\LoginAsCustomer\Model\ResourceModel\SaveAuthenticationData"/>
14+
<preference for="Magento\LoginAsCustomerApi\Api\GetAuthenticationDataBySecretInterface"
15+
type="Magento\LoginAsCustomer\Model\ResourceModel\GetAuthenticationDataBySecret"/>
16+
<preference for="Magento\LoginAsCustomerApi\Api\AuthenticateCustomerBySecretInterface"
17+
type="Magento\LoginAsCustomer\Model\AuthenticateCustomerBySecret"/>
18+
<preference for="Magento\LoginAsCustomerApi\Api\DeleteAuthenticationDataForUserInterface"
19+
type="Magento\LoginAsCustomer\Model\ResourceModel\DeleteAuthenticationDataForUser"/>
1420
<preference for="Magento\LoginAsCustomerApi\Api\ConfigInterface" type="Magento\LoginAsCustomer\Model\Config"/>
15-
<preference for="Magento\LoginAsCustomerApi\Api\IsLoginAsCustomerSessionActiveInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\IsLoginAsCustomerSessionActive"/>
21+
<preference for="Magento\LoginAsCustomerApi\Api\IsLoginAsCustomerSessionActiveInterface"
22+
type="Magento\LoginAsCustomer\Model\ResourceModel\IsLoginAsCustomerSessionActive"/>
23+
<preference for="Magento\LoginAsCustomerApi\Api\IsLoginAsCustomerEnabledForCustomerInterface"
24+
type="Magento\LoginAsCustomer\Model\IsLoginAsCustomerEnabledForCustomerChain"/>
25+
<type name="Magento\LoginAsCustomer\Model\IsLoginAsCustomerEnabledForCustomerChain">
26+
<arguments>
27+
<argument name="resolvers" xsi:type="array">
28+
<item name="is_enabled" xsi:type="object">
29+
Magento\LoginAsCustomer\Model\Resolver\IsLoginAsCustomerEnabledResolver
30+
</item>
31+
</argument>
32+
</arguments>
33+
</type>
1634
<type name="Magento\Backend\Model\Auth">
1735
<plugin name="login_as_customer_admin_logout" type="Magento\LoginAsCustomer\Plugin\AdminLogoutPlugin"/>
1836
</type>

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
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\ObjectManager;
1516
use Magento\Framework\Controller\Result\Redirect;
1617
use Magento\Framework\Controller\ResultFactory;
1718
use Magento\Framework\Controller\ResultInterface;
@@ -22,6 +23,7 @@
2223
use Magento\LoginAsCustomerApi\Api\Data\AuthenticationDataInterface;
2324
use Magento\LoginAsCustomerApi\Api\Data\AuthenticationDataInterfaceFactory;
2425
use Magento\LoginAsCustomerApi\Api\DeleteAuthenticationDataForUserInterface;
26+
use Magento\LoginAsCustomerApi\Api\IsLoginAsCustomerEnabledForCustomerInterface;
2527
use Magento\LoginAsCustomerApi\Api\SaveAuthenticationDataInterface;
2628
use Magento\Store\Model\StoreManagerInterface;
2729

@@ -80,16 +82,23 @@ class Login extends Action implements HttpGetActionInterface
8082
*/
8183
private $url;
8284

85+
/**
86+
* @var IsLoginAsCustomerEnabledForCustomerInterface
87+
*/
88+
private $isLoginAsCustomerEnabled;
89+
8390
/**
8491
* @param Context $context
8592
* @param Session $authSession
8693
* @param StoreManagerInterface $storeManager
8794
* @param CustomerRepositoryInterface $customerRepository
8895
* @param ConfigInterface $config
8996
* @param AuthenticationDataInterfaceFactory $authenticationDataFactory
90-
* @param SaveAuthenticationDataInterface $saveAuthenticationData ,
97+
* @param SaveAuthenticationDataInterface $saveAuthenticationData
9198
* @param DeleteAuthenticationDataForUserInterface $deleteAuthenticationDataForUser
9299
* @param Url $url
100+
* @param IsLoginAsCustomerEnabledForCustomerInterface $isLoginAsCustomerEnabled
101+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
93102
*/
94103
public function __construct(
95104
Context $context,
@@ -100,7 +109,8 @@ public function __construct(
100109
AuthenticationDataInterfaceFactory $authenticationDataFactory,
101110
SaveAuthenticationDataInterface $saveAuthenticationData,
102111
DeleteAuthenticationDataForUserInterface $deleteAuthenticationDataForUser,
103-
Url $url
112+
Url $url,
113+
?IsLoginAsCustomerEnabledForCustomerInterface $isLoginAsCustomerEnabled = null
104114
) {
105115
parent::__construct($context);
106116

@@ -112,6 +122,8 @@ public function __construct(
112122
$this->saveAuthenticationData = $saveAuthenticationData;
113123
$this->deleteAuthenticationDataForUser = $deleteAuthenticationDataForUser;
114124
$this->url = $url;
125+
$this->isLoginAsCustomerEnabled = $isLoginAsCustomerEnabled
126+
?? ObjectManager::getInstance()->get(IsLoginAsCustomerEnabledForCustomerInterface::class);
115127
}
116128

117129
/**
@@ -126,20 +138,24 @@ public function execute(): ResultInterface
126138
/** @var Redirect $resultRedirect */
127139
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
128140

129-
if (!$this->config->isEnabled()) {
130-
$this->messageManager->addErrorMessage(__('Login as Customer is disabled.'));
131-
return $resultRedirect->setPath('customer/index/index');
132-
}
133-
134141
$customerId = (int)$this->_request->getParam('customer_id');
135142
if (!$customerId) {
136143
$customerId = (int)$this->_request->getParam('entity_id');
137144
}
138145

146+
$isLoginAsCustomerEnabled = $this->isLoginAsCustomerEnabled->execute($customerId);
147+
if (!$isLoginAsCustomerEnabled->isEnabled()) {
148+
foreach ($isLoginAsCustomerEnabled->getMessages() as $message) {
149+
$this->messageManager->addErrorMessage(__($message));
150+
}
151+
152+
return $resultRedirect->setPath('customer/index/index');
153+
}
154+
139155
try {
140156
$customer = $this->customerRepository->getById($customerId);
141157
} catch (NoSuchEntityException $e) {
142-
$this->messageManager->addErrorMessage(__('Customer with this ID are no longer exist.'));
158+
$this->messageManager->addErrorMessage('Customer with this ID are no longer exist.');
143159
return $resultRedirect->setPath('customer/index/index');
144160
}
145161

0 commit comments

Comments
 (0)