Skip to content

Commit ca9bf2c

Browse files
AnujNehraAnujNehra
authored andcommitted
ACP2E-1776: Creating customer(-s) via Async REST API ignores group_id
1 parent 80ae262 commit ca9bf2c

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
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+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Customer\Plugin;
10+
11+
use Magento\Customer\Api\Data\CustomerInterface;
12+
use Magento\Framework\App\ObjectManager;
13+
use Magento\Framework\AuthorizationInterface;
14+
use Magento\Framework\Exception\AuthorizationException;
15+
use Magento\Customer\Model\AccountManagementApi as SubjectAccountManagementApi;
16+
17+
/**
18+
* Plugin to validate anonymous request for synchronous operations contains group id.
19+
*/
20+
class AccountManagementApi
21+
{
22+
/**
23+
* Authorization level of a basic admin session
24+
*
25+
* @see _isAllowed()
26+
*/
27+
public const ADMIN_RESOURCE = 'Magento_Customer::manage';
28+
29+
/**
30+
* @var AuthorizationInterface
31+
*/
32+
private $authorization;
33+
34+
/**
35+
*
36+
* @param AuthorizationInterface|null $authorization
37+
*/
38+
public function __construct(
39+
AuthorizationInterface $authorization = null
40+
) {
41+
$objectManager = ObjectManager::getInstance();
42+
$this->authorization = $authorization ?? $objectManager->get(AuthorizationInterface::class);
43+
}
44+
45+
/**
46+
* Validate groupId for anonymous request
47+
*
48+
* @param SubjectAccountManagementApi $subjectAccountManagementApi
49+
* @param CustomerInterface $customer
50+
* @return void
51+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
52+
* @throws AuthorizationException
53+
*/
54+
public function beforeCreateAccount(
55+
SubjectAccountManagementApi $subjectAccountManagementApi,
56+
CustomerInterface $customer
57+
): void {
58+
$groupId = $customer->getGroupId();
59+
if (isset($groupId) && !$this->authorization->isAllowed(self::ADMIN_RESOURCE)) {
60+
$params = ['resources' => self::ADMIN_RESOURCE];
61+
throw new AuthorizationException(
62+
__("The consumer isn't authorized to access %resources.", $params)
63+
);
64+
}
65+
}
66+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,4 +585,9 @@
585585
</argument>
586586
</arguments>
587587
</type>
588+
<type name="Magento\Customer\Model\AccountManagementApi">
589+
<plugin name="anonymousRequestForSynchronousOperations"
590+
type="Magento\Customer\Plugin\AccountManagementApi"
591+
/>
592+
</type>
588593
</config>

app/code/Magento/WebapiAsync/Plugin/AsynchronousOperations/MassSchedule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Magento\AsynchronousOperations\Model\MassSchedule as SubjectMassSchedule;
1616

1717
/**
18-
* Plugin to check anonymous request contains group id.
18+
* Plugin to validate anonymous request for asynchronous operations contains group id.
1919
*/
2020
class MassSchedule
2121
{

app/code/Magento/WebapiAsync/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"magento/framework": "*",
1010
"magento/module-webapi": "*",
1111
"magento/module-asynchronous-operations": "*",
12-
"magento/module-store": "*"
12+
"magento/module-store": "*",
13+
"magento/module-customer": "*"
1314
},
1415
"suggest": {
1516
"magento/module-user": "*",

0 commit comments

Comments
 (0)