|
| 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\Webapi\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; |
| 16 | + |
| 17 | +/** |
| 18 | + * Plugin to validate anonymous request for synchronous operations containing group id. |
| 19 | + */ |
| 20 | +class SyncRequestCustomerGroupAuthorization |
| 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 AccountManagementApi $accountManagementApi |
| 49 | + * @param CustomerInterface $customer |
| 50 | + * @return void |
| 51 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 52 | + * @throws AuthorizationException |
| 53 | + */ |
| 54 | + public function beforeCreateAccount( |
| 55 | + AccountManagementApi $accountManagementApi, |
| 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 | +} |
0 commit comments