custom/plugins/SwagPlatformSecurity/src/Fixes/NEXT14883/SecurityFix.php line 51

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Swag\Security\Fixes\NEXT14883;
  3. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  4. use Shopware\Core\Framework\Api\Controller\Exception\PermissionDeniedException;
  5. use Shopware\Core\Framework\Routing\KernelListenerPriorities;
  6. use Shopware\Core\PlatformRequest;
  7. use Swag\Security\Components\AbstractSecurityFix;
  8. use Symfony\Component\DependencyInjection\ContainerBuilder;
  9. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. class SecurityFix extends AbstractSecurityFix
  12. {
  13.     private const API_ROUTES = [
  14.         'api.integration.create',
  15.         'api.integration.update',
  16.     ];
  17.     public static function getTicket(): string
  18.     {
  19.         return 'NEXT-14883';
  20.     }
  21.     public static function getMinVersion(): string
  22.     {
  23.         return '6.3.3.0';
  24.     }
  25.     public static function getMaxVersion(): ?string
  26.     {
  27.         return '6.4.0.0';
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             KernelEvents::CONTROLLER => [
  33.                 'onKernelRequest'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_CONTEXT_RESOLVE
  34.             ]
  35.         ];
  36.     }
  37.     public static function buildContainer(ContainerBuilder $container): void
  38.     {
  39.         $container->addCompilerPass(new ReplaceIntegrationDefinitionCompilerPass());
  40.     }
  41.     public function onKernelRequest(ControllerEvent $event): void
  42.     {
  43.         $request $event->getRequest();
  44.         $route $request->attributes->get('_route');
  45.         if (!in_array($routeself::API_ROUTEStrue)) {
  46.             return;
  47.         }
  48.         $context $request->attributes->get(PlatformRequest::ATTRIBUTE_CONTEXT_OBJECT);
  49.         /** @var AdminApiSource $source */
  50.         $source $context->getSource();
  51.         $data $request->request->all();
  52.         // only an admin is allowed to set the admin field
  53.         if (
  54.             !$source->isAdmin()
  55.             && isset($data['admin'])
  56.         ) {
  57.             if (class_exists(PermissionDeniedException::class)) {
  58.                 throw new PermissionDeniedException();
  59.             }
  60.             // In early versions that exception class does not exists yet
  61.             throw new \Swag\Security\Fixes\NEXT14883\PermissionDeniedException();
  62.         }
  63.     }
  64. }