custom/plugins/SwagPlatformSecurity/src/Fixes/NEXT20305/SecurityFix.php line 68

Open in your IDE?
  1. <?php
  2. namespace Swag\Security\Fixes\NEXT20305;
  3. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\PlatformRequest;
  6. use Swag\Security\Components\AbstractSecurityFix;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  9. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  10. use Symfony\Component\HttpKernel\Exception\HttpException;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. class SecurityFix extends AbstractSecurityFix
  13. {
  14.     private const API_PROXY_SWITCH_CUSTOMER_PRIVILEGE 'api_proxy_switch-customer';
  15.     public static function getTicket(): string
  16.     {
  17.         return 'NEXT-20305';
  18.     }
  19.     public static function getMinVersion(): string
  20.     {
  21.         return '6.3.1.0';
  22.     }
  23.     public static function getMaxVersion(): ?string
  24.     {
  25.         return '6.4.8.2';
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             KernelEvents::CONTROLLER_ARGUMENTS => 'onSwitchCustomer',
  31.             KernelEvents::RESPONSE => 'onGetAdditionalPrivileges',
  32.         ];
  33.     }
  34.     public function onSwitchCustomer(ControllerArgumentsEvent $event): void
  35.     {
  36.         if ($event->getRequest()->attributes->get('_route') !== 'api.proxy.switch-customer') {
  37.             return;
  38.         }
  39.         /** @var Context|null $context */
  40.         $context $event->getRequest()->attributes->get(PlatformRequest::ATTRIBUTE_CONTEXT_OBJECT);
  41.         if (!$context) {
  42.             return;
  43.         }
  44.         $source $context->getSource();
  45.         if (!$source instanceof AdminApiSource) {
  46.             return;
  47.         }
  48.         if ($source->isAdmin() || $source->isAllowed(self::API_PROXY_SWITCH_CUSTOMER_PRIVILEGE)) {
  49.             return;
  50.         }
  51.         throw new HttpException(Response::HTTP_FORBIDDEN'Missing permission: ' self::API_PROXY_SWITCH_CUSTOMER_PRIVILEGE);
  52.     }
  53.     public function onGetAdditionalPrivileges(ResponseEvent $event): void
  54.     {
  55.         if ($event->getRequest()->attributes->get('_route') !== 'api.acl.privileges.additional.get') {
  56.             return;
  57.         }
  58.         /** @var string[] $privileges */
  59.         $privileges = \json_decode($event->getResponse()->getContent(), true);
  60.         if (!\in_array(self::API_PROXY_SWITCH_CUSTOMER_PRIVILEGE$privilegestrue)) {
  61.             $privileges[] = self::API_PROXY_SWITCH_CUSTOMER_PRIVILEGE;
  62.         }
  63.         $event->getResponse()->setContent(\json_encode(\array_values($privileges)));
  64.     }
  65. }