Skip to content

Commit 7efaa47

Browse files
authored
Merge pull request #6700 from magento-cia/cia-2.4.3-3152021
cia-fixes-2.4.3
2 parents d152fb1 + 716b3c8 commit 7efaa47

File tree

4 files changed

+377
-2
lines changed

4 files changed

+377
-2
lines changed

app/code/Magento/LoginAsCustomerAssistance/Plugin/CustomerPlugin.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77

88
namespace Magento\LoginAsCustomerAssistance\Plugin;
99

10+
use Magento\Authorization\Model\UserContextInterface;
1011
use Magento\Customer\Api\CustomerRepositoryInterface;
1112
use Magento\Customer\Api\Data\CustomerInterface;
13+
use Magento\Framework\App\ObjectManager;
14+
use Magento\Framework\AuthorizationInterface;
1215
use Magento\LoginAsCustomerAssistance\Api\SetAssistanceInterface;
1316
use Magento\LoginAsCustomerAssistance\Model\IsAssistanceEnabled;
1417

@@ -22,13 +25,29 @@ class CustomerPlugin
2225
*/
2326
private $setAssistance;
2427

28+
/**
29+
* @var AuthorizationInterface
30+
*/
31+
private $authorization;
32+
33+
/**
34+
* @var UserContextInterface
35+
*/
36+
private $userContext;
37+
2538
/**
2639
* @param SetAssistanceInterface $setAssistance
40+
* @param AuthorizationInterface|null $authorization
41+
* @param UserContextInterface|null $userContext
2742
*/
2843
public function __construct(
29-
SetAssistanceInterface $setAssistance
44+
SetAssistanceInterface $setAssistance,
45+
?AuthorizationInterface $authorization = null,
46+
?UserContextInterface $userContext = null
3047
) {
3148
$this->setAssistance = $setAssistance;
49+
$this->authorization = $authorization ?? ObjectManager::getInstance()->get(AuthorizationInterface::class);
50+
$this->userContext = $userContext ?? ObjectManager::getInstance()->get(UserContextInterface::class);
3251
}
3352

3453
/**
@@ -45,9 +64,16 @@ public function afterSave(
4564
CustomerInterface $result,
4665
CustomerInterface $customer
4766
): CustomerInterface {
67+
$enoughPermission = true;
68+
if ($this->userContext->getUserType() === UserContextInterface::USER_TYPE_ADMIN
69+
|| $this->userContext->getUserType() === UserContextInterface::USER_TYPE_INTEGRATION
70+
) {
71+
$enoughPermission = $this->authorization->isAllowed('Magento_LoginAsCustomer::allow_shopping_assistance');
72+
}
4873
$customerId = (int)$result->getId();
4974
$customerExtensionAttributes = $customer->getExtensionAttributes();
50-
if ($customerExtensionAttributes && $customerExtensionAttributes->getAssistanceAllowed()) {
75+
76+
if ($enoughPermission && $customerExtensionAttributes && $customerExtensionAttributes->getAssistanceAllowed()) {
5177
$isEnabled = (int)$customerExtensionAttributes->getAssistanceAllowed() === IsAssistanceEnabled::ALLOWED;
5278
$this->setAssistance->execute($customerId, $isEnabled);
5379
}

app/code/Magento/LoginAsCustomerAssistance/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"require": {
55
"php": "~7.3.0||~7.4.0",
66
"magento/framework": "*",
7+
"magento/module-authorization": "*",
78
"magento/module-backend": "*",
89
"magento/module-customer": "*",
910
"magento/module-store": "*",
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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\LoginAsCustomerAssistance\Plugin;
9+
10+
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Customer\Api\Data\CustomerInterface as Customer;
12+
use Magento\Customer\Model\CustomerRegistry;
13+
use Magento\Framework\Api\ExtensibleDataInterface;
14+
use Magento\Framework\Reflection\DataObjectProcessor;
15+
use Magento\Framework\Webapi\Rest\Request;
16+
use Magento\Integration\Model\Oauth\TokenFactory;
17+
use Magento\LoginAsCustomerAssistance\Api\IsAssistanceEnabledInterface;
18+
use Magento\LoginAsCustomerAssistance\Model\ResourceModel\GetLoginAsCustomerAssistanceAllowed;
19+
use Magento\TestFramework\Helper\Bootstrap;
20+
use Magento\TestFramework\TestCase\WebapiAbstract;
21+
22+
/**
23+
* Api test for @see \Magento\LoginAsCustomerAssistance\Plugin\CustomerPlugin::afterSave.
24+
*/
25+
class CustomerMeTest extends WebapiAbstract
26+
{
27+
const SERVICE_VERSION = 'V1';
28+
const SERVICE_NAME = 'customerCustomerRepositoryV1';
29+
const RESOURCE_PATH = '/V1/customers/me';
30+
31+
/**
32+
* @var DataObjectProcessor
33+
*/
34+
private $dataObjectProcessor;
35+
36+
/**
37+
* @var CustomerRepositoryInterface
38+
*/
39+
private $customerRepository;
40+
41+
/**
42+
* @var CustomerRegistry
43+
*/
44+
private $customerRegistry;
45+
46+
/**
47+
* @var GetLoginAsCustomerAssistanceAllowed
48+
*/
49+
private $isAssistanceEnabled;
50+
51+
/**
52+
* @var TokenFactory
53+
*/
54+
private $tokenFactory;
55+
56+
/**
57+
* @inheritDoc
58+
*/
59+
protected function setUp(): void
60+
{
61+
$objectManager = Bootstrap::getObjectManager();
62+
$this->dataObjectProcessor = $objectManager->get(DataObjectProcessor::class);
63+
$this->customerRepository = $objectManager->get(CustomerRepositoryInterface::class);
64+
$this->customerRegistry = $objectManager->get(CustomerRegistry::class);
65+
$this->isAssistanceEnabled = $objectManager->get(GetLoginAsCustomerAssistanceAllowed::class);
66+
$this->tokenFactory = $objectManager->get(TokenFactory::class);
67+
}
68+
69+
/**
70+
* Check that 'assistance_allowed' set as expected.
71+
*
72+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
73+
* @dataProvider assistanceStatesDataProvider
74+
*
75+
* @param int $state
76+
* @param bool $expected
77+
* @return void
78+
*/
79+
public function testUpdateSelf(int $state, bool $expected): void
80+
{
81+
$customerId = (int)$this->customerRepository->get('customer@example.com')->getId();
82+
$tokenModel = $this->tokenFactory->create();
83+
$customerToken = $tokenModel->createCustomerToken($customerId)->getToken();
84+
85+
$updatedLastname = 'Updated lastname';
86+
$customer = $this->getCustomerData($customerId);
87+
$customerData = $this->dataObjectProcessor->buildOutputDataArray($customer, Customer::class);
88+
$customerData[Customer::LASTNAME] = $updatedLastname;
89+
$customerData[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]['assistance_allowed'] = $state;
90+
91+
$requestData['customer'] = TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP
92+
? $customerData
93+
: [
94+
Customer::EMAIL => $customerData['email'],
95+
Customer::FIRSTNAME => $customerData['firstname'],
96+
Customer::LASTNAME => $updatedLastname,
97+
Customer::EXTENSION_ATTRIBUTES_KEY => ['assistance_allowed' => $state],
98+
];
99+
100+
$serviceInfo = $this->getServiceInfo('SaveSelf', $customerToken);
101+
$response = $this->_webApiCall($serviceInfo, $requestData);
102+
$this->assertNotNull($response);
103+
104+
$existingCustomerDataObject = $this->getCustomerData($customerId);
105+
$this->assertEquals($updatedLastname, $existingCustomerDataObject->getLastname());
106+
$this->assertEquals($expected, $this->isAssistanceEnabled->execute($customerId));
107+
}
108+
109+
/**
110+
* @param string $operation
111+
* @param string $token
112+
* @return array
113+
*/
114+
private function getServiceInfo(string $operation, string $token): array
115+
{
116+
return [
117+
'rest' => [
118+
'resourcePath' => self::RESOURCE_PATH,
119+
'httpMethod' => Request::HTTP_METHOD_PUT,
120+
'token' => $token,
121+
],
122+
'soap' => [
123+
'service' => self::SERVICE_NAME,
124+
'serviceVersion' => self::SERVICE_VERSION,
125+
'operation' => self::SERVICE_NAME . $operation,
126+
'token' => $token,
127+
],
128+
];
129+
}
130+
131+
/**
132+
* Retrieve customer data by Id.
133+
*
134+
* @param int $customerId
135+
* @return Customer
136+
*/
137+
private function getCustomerData(int $customerId): Customer
138+
{
139+
$customerData = $this->customerRepository->getById($customerId);
140+
$this->customerRegistry->remove($customerId);
141+
142+
return $customerData;
143+
}
144+
145+
/**
146+
* @return array
147+
*/
148+
public function assistanceStatesDataProvider(): array
149+
{
150+
return [
151+
'Assistance Allowed' => [IsAssistanceEnabledInterface::ALLOWED, true],
152+
'Assistance Denied' => [IsAssistanceEnabledInterface::DENIED, false],
153+
];
154+
}
155+
}

0 commit comments

Comments
 (0)