Skip to content

Commit db2351b

Browse files
AnujNehraAnujNehra
authored andcommitted
ACP2E-1776: Creating customer(-s) via Async REST API ignores group_id
1 parent 4ea7d62 commit db2351b

File tree

8 files changed

+194
-372
lines changed

8 files changed

+194
-372
lines changed

app/code/Magento/Customer/Model/AccountManagementApi.php

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,215 @@
66

77
namespace Magento\Customer\Model;
88

9+
use Magento\Customer\Api\AddressRepositoryInterface;
10+
use Magento\Customer\Api\CustomerMetadataInterface;
11+
use Magento\Customer\Api\CustomerRepositoryInterface;
912
use Magento\Customer\Api\Data\CustomerInterface;
13+
use Magento\Customer\Api\Data\ValidationResultsInterfaceFactory;
14+
use Magento\Customer\Helper\View as CustomerViewHelper;
15+
use Magento\Customer\Model\Config\Share as ConfigShare;
16+
use Magento\Customer\Model\Customer as CustomerModel;
17+
use Magento\Customer\Model\Metadata\Validator;
18+
use Magento\Framework\Api\ExtensibleDataObjectConverter;
19+
use Magento\Framework\App\Config\ScopeConfigInterface;
20+
use Magento\Framework\App\ObjectManager;
21+
use Magento\Framework\AuthorizationInterface;
22+
use Magento\Framework\DataObjectFactory as ObjectFactory;
23+
use Magento\Framework\Encryption\EncryptorInterface as Encryptor;
24+
use Magento\Framework\Event\ManagerInterface;
25+
use Magento\Framework\Exception\AuthorizationException;
26+
use Magento\Framework\Mail\Template\TransportBuilder;
27+
use Magento\Framework\Math\Random;
28+
use Magento\Framework\Reflection\DataObjectProcessor;
29+
use Magento\Framework\Registry;
30+
use Magento\Framework\Stdlib\DateTime;
31+
use Magento\Framework\Stdlib\StringUtils as StringHelper;
32+
use Magento\Store\Model\StoreManagerInterface;
33+
use Psr\Log\LoggerInterface as PsrLogger;
1034

1135
/**
1236
* Account Management service implementation for external API access.
1337
* Handle various customer account actions.
1438
*
1539
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
40+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1641
*/
1742
class AccountManagementApi extends AccountManagement
1843
{
44+
/**
45+
* Authorization level of a basic admin session
46+
*
47+
* @see _isAllowed()
48+
*/
49+
public const ADMIN_RESOURCE = 'Magento_Customer::manage';
50+
51+
/**
52+
* @var PsrLogger
53+
*/
54+
protected $logger;
55+
56+
/**
57+
* @var StringHelper
58+
*/
59+
protected $stringHelper;
60+
61+
/**
62+
* @var DataObjectProcessor
63+
*/
64+
protected $dataProcessor;
65+
66+
/**
67+
* @var Registry
68+
*/
69+
protected $registry;
70+
71+
/**
72+
* @var CustomerViewHelper
73+
*/
74+
protected $customerViewHelper;
75+
76+
/**
77+
* @var DateTime
78+
*/
79+
protected $dateTime;
80+
81+
/**
82+
* @var ObjectFactory
83+
*/
84+
protected $objectFactory;
85+
86+
/**
87+
* @var ExtensibleDataObjectConverter
88+
*/
89+
protected $extensibleDataObjectConverter;
90+
91+
/**
92+
* @var CustomerModel
93+
*/
94+
protected $customerModel;
95+
96+
/**
97+
* @var AuthenticationInterface
98+
*/
99+
protected $authentication;
100+
/**
101+
* @var AuthorizationInterface
102+
*/
103+
private $authorization;
104+
105+
/**
106+
* @param CustomerFactory $customerFactory
107+
* @param ManagerInterface $eventManager
108+
* @param StoreManagerInterface $storeManager
109+
* @param Random $mathRandom
110+
* @param Validator $validator
111+
* @param ValidationResultsInterfaceFactory $validationResultsDataFactory
112+
* @param AddressRepositoryInterface $addressRepository
113+
* @param CustomerMetadataInterface $customerMetadataService
114+
* @param CustomerRegistry $customerRegistry
115+
* @param PsrLogger $logger
116+
* @param Encryptor $encryptor
117+
* @param ConfigShare $configShare
118+
* @param StringHelper $stringHelper
119+
* @param CustomerRepositoryInterface $customerRepository
120+
* @param ScopeConfigInterface $scopeConfig
121+
* @param TransportBuilder $transportBuilder
122+
* @param DataObjectProcessor $dataProcessor
123+
* @param Registry $registry
124+
* @param CustomerViewHelper $customerViewHelper
125+
* @param DateTime $dateTime
126+
* @param CustomerModel $customerModel
127+
* @param ObjectFactory $objectFactory
128+
* @param ExtensibleDataObjectConverter $extensibleDataObjectConverter
129+
* @param AuthorizationInterface|null $authorization
130+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
131+
*/
132+
public function __construct(
133+
CustomerFactory $customerFactory,
134+
ManagerInterface $eventManager,
135+
StoreManagerInterface $storeManager,
136+
Random $mathRandom,
137+
Validator $validator,
138+
ValidationResultsInterfaceFactory $validationResultsDataFactory,
139+
AddressRepositoryInterface $addressRepository,
140+
CustomerMetadataInterface $customerMetadataService,
141+
CustomerRegistry $customerRegistry,
142+
PsrLogger $logger,
143+
Encryptor $encryptor,
144+
ConfigShare $configShare,
145+
StringHelper $stringHelper,
146+
CustomerRepositoryInterface $customerRepository,
147+
ScopeConfigInterface $scopeConfig,
148+
TransportBuilder $transportBuilder,
149+
DataObjectProcessor $dataProcessor,
150+
Registry $registry,
151+
CustomerViewHelper $customerViewHelper,
152+
DateTime $dateTime,
153+
CustomerModel $customerModel,
154+
ObjectFactory $objectFactory,
155+
ExtensibleDataObjectConverter $extensibleDataObjectConverter,
156+
AuthorizationInterface $authorization = null
157+
) {
158+
$objectManager = ObjectManager::getInstance();
159+
$this->authorization = $authorization ?? $objectManager->get(AuthorizationInterface::class);
160+
parent::__construct(
161+
$customerFactory,
162+
$eventManager,
163+
$storeManager,
164+
$mathRandom,
165+
$validator,
166+
$validationResultsDataFactory,
167+
$addressRepository,
168+
$customerMetadataService,
169+
$customerRegistry,
170+
$logger,
171+
$encryptor,
172+
$configShare,
173+
$stringHelper,
174+
$customerRepository,
175+
$scopeConfig,
176+
$transportBuilder,
177+
$dataProcessor,
178+
$registry,
179+
$customerViewHelper,
180+
$dateTime,
181+
$customerModel,
182+
$objectFactory,
183+
$extensibleDataObjectConverter
184+
);
185+
}
186+
19187
/**
20188
* @inheritDoc
21189
*
22190
* Override createAccount method to unset confirmation attribute for security purposes.
23191
*/
24192
public function createAccount(CustomerInterface $customer, $password = null, $redirectUrl = '')
25193
{
194+
$this->validateCustomerRequest($customer);
26195
$customer = parent::createAccount($customer, $password, $redirectUrl);
27196
$customer->setConfirmation(null);
28197

29198
return $customer;
30199
}
200+
201+
/**
202+
* Validate anonymous request
203+
*
204+
* @param CustomerInterface $customer
205+
* @return void
206+
* @throws AuthorizationException
207+
*/
208+
private function validateCustomerRequest(CustomerInterface $customer): void
209+
{
210+
$groupId = $customer->getGroupId();
211+
if (isset($groupId) &&
212+
!$this->authorization->isAllowed(self::ADMIN_RESOURCE)
213+
) {
214+
$params = ['resources' => self::ADMIN_RESOURCE];
215+
throw new AuthorizationException(
216+
__("The consumer isn't authorized to access %resources.", $params)
217+
);
218+
}
219+
}
31220
}

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\AsynchronousOperations\Model\MassSchedule">
589+
<plugin name="anonymousAsyncCustomerRequest"
590+
type="Magento\Customer\Plugin\AsyncRequestCustomerGroupAuthorization"
591+
/>
592+
</type>
588593
</config>

app/code/Magento/Webapi/Plugin/SyncRequestCustomerGroupAuthorization.php

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)