Skip to content

Commit d6562e3

Browse files
Merge branch 'magento/magento2-login-as-customer#188' into 144+188
# Conflicts: # app/code/Magento/LoginAsCustomer/etc/di.xml # app/code/Magento/LoginAsCustomerAdminUi/Controller/Adminhtml/Login/Login.php
2 parents 75ac691 + 86ec81c commit d6562e3

File tree

12 files changed

+455
-71
lines changed

12 files changed

+455
-71
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: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,35 @@
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"/>
1521
<preference for="Magento\LoginAsCustomerApi\Api\IsLoginAsCustomerSessionActiveInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\IsLoginAsCustomerSessionActive"/>
1622
<preference for="Magento\LoginAsCustomerApi\Api\GetLoggedAsCustomerAdminIdInterface" type="Magento\LoginAsCustomer\Model\GetLoggedAsCustomerAdminId"/>
1723
<preference for="Magento\LoginAsCustomerApi\Api\GetLoggedAsCustomerCustomerIdInterface" type="Magento\LoginAsCustomer\Model\GetLoggedAsCustomerCustomerId"/>
1824
<preference for="Magento\LoginAsCustomerApi\Api\SetLoggedAsCustomerAdminIdInterface" type="Magento\LoginAsCustomer\Model\SetLoggedAsCustomerAdminId"/>
1925
<preference for="Magento\LoginAsCustomerApi\Api\SetLoggedAsCustomerCustomerIdInterface" type="Magento\LoginAsCustomer\Model\SetLoggedAsCustomerCustomerId"/>
26+
<preference for="Magento\LoginAsCustomerApi\Api\IsLoginAsCustomerEnabledForCustomerInterface"
27+
type="Magento\LoginAsCustomer\Model\IsLoginAsCustomerEnabledForCustomerChain"/>
28+
<type name="Magento\LoginAsCustomer\Model\IsLoginAsCustomerEnabledForCustomerChain">
29+
<arguments>
30+
<argument name="resolvers" xsi:type="array">
31+
<item name="is_enabled" xsi:type="object">
32+
Magento\LoginAsCustomer\Model\Resolver\IsLoginAsCustomerEnabledResolver
33+
</item>
34+
</argument>
35+
</arguments>
36+
</type>
2037
<type name="Magento\Backend\Model\Auth">
2138
<plugin name="login_as_customer_admin_logout" type="Magento\LoginAsCustomer\Plugin\AdminLogoutPlugin"/>
2239
</type>

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Magento\LoginAsCustomerApi\Api\Data\AuthenticationDataInterface;
2424
use Magento\LoginAsCustomerApi\Api\Data\AuthenticationDataInterfaceFactory;
2525
use Magento\LoginAsCustomerApi\Api\DeleteAuthenticationDataForUserInterface;
26+
use Magento\LoginAsCustomerApi\Api\IsLoginAsCustomerEnabledForCustomerInterface;
2627
use Magento\LoginAsCustomerApi\Api\SaveAuthenticationDataInterface;
2728
use Magento\LoginAsCustomerApi\Api\SetLoggedAsCustomerCustomerIdInterface;
2829
use Magento\Store\Model\StoreManagerInterface;
@@ -87,17 +88,23 @@ class Login extends Action implements HttpGetActionInterface
8788
*/
8889
private $setLoggedAsCustomerCustomerId;
8990

91+
/**
92+
* @var IsLoginAsCustomerEnabledForCustomerInterface
93+
*/
94+
private $isLoginAsCustomerEnabled;
95+
9096
/**
9197
* @param Context $context
9298
* @param Session $authSession
9399
* @param StoreManagerInterface $storeManager
94100
* @param CustomerRepositoryInterface $customerRepository
95101
* @param ConfigInterface $config
96102
* @param AuthenticationDataInterfaceFactory $authenticationDataFactory
97-
* @param SaveAuthenticationDataInterface $saveAuthenticationData ,
103+
* @param SaveAuthenticationDataInterface $saveAuthenticationData
98104
* @param DeleteAuthenticationDataForUserInterface $deleteAuthenticationDataForUser
99105
* @param Url $url
100106
* @param SetLoggedAsCustomerCustomerIdInterface $setLoggedAsCustomerCustomerId
107+
* @param IsLoginAsCustomerEnabledForCustomerInterface $isLoginAsCustomerEnabled
101108
*
102109
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
103110
*/
@@ -111,7 +118,8 @@ public function __construct(
111118
SaveAuthenticationDataInterface $saveAuthenticationData,
112119
DeleteAuthenticationDataForUserInterface $deleteAuthenticationDataForUser,
113120
Url $url,
114-
?SetLoggedAsCustomerCustomerIdInterface $setLoggedAsCustomerCustomerId = null
121+
?SetLoggedAsCustomerCustomerIdInterface $setLoggedAsCustomerCustomerId = null,
122+
?IsLoginAsCustomerEnabledForCustomerInterface $isLoginAsCustomerEnabled = null
115123
) {
116124
parent::__construct($context);
117125

@@ -124,6 +132,8 @@ public function __construct(
124132
$this->deleteAuthenticationDataForUser = $deleteAuthenticationDataForUser;
125133
$this->url = $url;
126134
$this->setLoggedAsCustomerCustomerId = $setLoggedAsCustomerCustomerId ?? ObjectManager::getInstance()->get(SetLoggedAsCustomerCustomerIdInterface::class);
135+
$this->isLoginAsCustomerEnabled = $isLoginAsCustomerEnabled
136+
?? ObjectManager::getInstance()->get(IsLoginAsCustomerEnabledForCustomerInterface::class);
127137
}
128138

129139
/**
@@ -138,20 +148,24 @@ public function execute(): ResultInterface
138148
/** @var Redirect $resultRedirect */
139149
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
140150

141-
if (!$this->config->isEnabled()) {
142-
$this->messageManager->addErrorMessage(__('Login as Customer is disabled.'));
143-
return $resultRedirect->setPath('customer/index/index');
144-
}
145-
146151
$customerId = (int)$this->_request->getParam('customer_id');
147152
if (!$customerId) {
148153
$customerId = (int)$this->_request->getParam('entity_id');
149154
}
150155

156+
$isLoginAsCustomerEnabled = $this->isLoginAsCustomerEnabled->execute($customerId);
157+
if (!$isLoginAsCustomerEnabled->isEnabled()) {
158+
foreach ($isLoginAsCustomerEnabled->getMessages() as $message) {
159+
$this->messageManager->addErrorMessage(__($message));
160+
}
161+
162+
return $resultRedirect->setPath('customer/index/index');
163+
}
164+
151165
try {
152166
$customer = $this->customerRepository->getById($customerId);
153167
} catch (NoSuchEntityException $e) {
154-
$this->messageManager->addErrorMessage(__('Customer with this ID are no longer exist.'));
168+
$this->messageManager->addErrorMessage('Customer with this ID are no longer exist.');
155169
return $resultRedirect->setPath('customer/index/index');
156170
}
157171

0 commit comments

Comments
 (0)