|
| 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\WebapiAsync\Plugin\AsynchronousOperations; |
| 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\AsynchronousOperations\Model\MassSchedule as SubjectMassSchedule; |
| 16 | + |
| 17 | +/** |
| 18 | + * Plugin to check anonymous request contains group id. |
| 19 | + */ |
| 20 | +class MassSchedule |
| 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 SubjectMassSchedule $subjectMassSchedule |
| 49 | + * @param string $topic |
| 50 | + * @param array $entitiesArray |
| 51 | + * @return void |
| 52 | + * @throws AuthorizationException |
| 53 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 54 | + */ |
| 55 | + public function beforePublishMass( |
| 56 | + SubjectMassSchedule $subjectMassSchedule, |
| 57 | + string $topic, |
| 58 | + array $entitiesArray |
| 59 | + ): void { |
| 60 | + foreach ($entitiesArray as $entityParams) { |
| 61 | + foreach ($entityParams as $customer) { |
| 62 | + if ($customer instanceof CustomerInterface) { |
| 63 | + $groupId = $customer->getGroupId(); |
| 64 | + if (isset($groupId) && !$this->authorization->isAllowed(self::ADMIN_RESOURCE)) { |
| 65 | + $params = ['resources' => self::ADMIN_RESOURCE]; |
| 66 | + throw new AuthorizationException( |
| 67 | + __("The consumer isn't authorized to access %resources.", $params) |
| 68 | + ); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments