Skip to content

Commit 21f6154

Browse files
committed
MC-39523: Fix customer assistance plugin
1 parent e6c4e28 commit 21f6154

File tree

4 files changed

+365
-2
lines changed

4 files changed

+365
-2
lines changed

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
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\AuthorizationInterface;
1214
use Magento\LoginAsCustomerAssistance\Api\SetAssistanceInterface;
1315
use Magento\LoginAsCustomerAssistance\Model\IsAssistanceEnabled;
1416

@@ -22,13 +24,29 @@ class CustomerPlugin
2224
*/
2325
private $setAssistance;
2426

27+
/**
28+
* @var AuthorizationInterface
29+
*/
30+
private $authorization;
31+
32+
/**
33+
* @var UserContextInterface
34+
*/
35+
private $userContext;
36+
2537
/**
2638
* @param SetAssistanceInterface $setAssistance
39+
* @param AuthorizationInterface $authorization
40+
* @param UserContextInterface $userContext
2741
*/
2842
public function __construct(
29-
SetAssistanceInterface $setAssistance
43+
SetAssistanceInterface $setAssistance,
44+
AuthorizationInterface $authorization,
45+
UserContextInterface $userContext
3046
) {
3147
$this->setAssistance = $setAssistance;
48+
$this->authorization = $authorization;
49+
$this->userContext = $userContext;
3250
}
3351

3452
/**
@@ -45,9 +63,15 @@ public function afterSave(
4563
CustomerInterface $result,
4664
CustomerInterface $customer
4765
): CustomerInterface {
66+
$enoughPermission = true;
67+
if ($this->userContext->getUserType() === UserContextInterface::USER_TYPE_ADMIN
68+
|| $this->userContext->getUserType() === UserContextInterface::USER_TYPE_INTEGRATION) {
69+
$enoughPermission = $this->authorization->isAllowed('Magento_LoginAsCustomer::allow_shopping_assistance');
70+
}
4871
$customerId = (int)$result->getId();
4972
$customerExtensionAttributes = $customer->getExtensionAttributes();
50-
if ($customerExtensionAttributes && $customerExtensionAttributes->getAssistanceAllowed()) {
73+
74+
if ($enoughPermission && $customerExtensionAttributes && $customerExtensionAttributes->getAssistanceAllowed()) {
5175
$isEnabled = (int)$customerExtensionAttributes->getAssistanceAllowed() === IsAssistanceEnabled::ALLOWED;
5276
$this->setAssistance->execute($customerId, $isEnabled);
5377
}

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: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
return $customerData;
142+
}
143+
144+
/**
145+
* @return array
146+
*/
147+
public function assistanceStatesDataProvider(): array
148+
{
149+
return [
150+
'Assistance Allowed' => [IsAssistanceEnabledInterface::ALLOWED, true],
151+
'Assistance Denied' => [IsAssistanceEnabledInterface::DENIED, false],
152+
];
153+
}
154+
}

0 commit comments

Comments
 (0)