custom/plugins/swagplatformsecurity6.4/src/Fixes/GHSAp5892ff83wfw/CloneProtectionSubscriber.php line 34

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Swag\Security\Fixes\GHSAp5892ff83wfw;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\WriteProtected;
  6. use Shopware\Core\Framework\Validation\WriteConstraintViolationException;
  7. use Shopware\Core\PlatformRequest;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  10. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Component\Validator\ConstraintViolation;
  13. use Symfony\Component\Validator\ConstraintViolationList;
  14. class CloneProtectionSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var DefinitionInstanceRegistry
  18.      */
  19.     private $registry;
  20.     public function __construct(DefinitionInstanceRegistry $registry)
  21.     {
  22.         $this->registry $registry;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [KernelEvents::CONTROLLER_ARGUMENTS => 'onControllerArguments'];
  27.     }
  28.     public function onControllerArguments(ControllerArgumentsEvent $event): void
  29.     {
  30.         // isMainRequest() existiert erst ab Symfony 5.3, isMasterRequest() ist dort deprecated
  31.         $isMainRequest method_exists($event'isMainRequest') ? $event->isMainRequest() : $event->isMasterRequest();
  32.         if (!$isMainRequest) {
  33.             return;
  34.         }
  35.         $request $event->getRequest();
  36.         if ($request->attributes->get('_route') !== 'api.clone') {
  37.             return;
  38.         }
  39.         $entity $request->attributes->get('entity');
  40.         if (\in_array($entity, ['user''integration'], true)) {
  41.             throw new AccessDeniedHttpException(sprintf('Clone access for entity "%s" is not allowed.'$entity));
  42.         }
  43.         if (!\is_string($entity)) {
  44.             return;
  45.         }
  46.         $context $request->attributes->get(PlatformRequest::ATTRIBUTE_CONTEXT_OBJECT);
  47.         if (!$context instanceof Context) {
  48.             return;
  49.         }
  50.         $definition $this->registry->getByEntityName(str_replace('-''_'$entity));
  51.         foreach ($request->request->all('overwrites') as $propertyName => $value) {
  52.             $field $definition->getFields()->get($propertyName);
  53.             if ($field === null) {
  54.                 continue;
  55.             }
  56.             $writeProtection $field->getFlag(WriteProtected::class);
  57.             if ($writeProtection === null || $writeProtection->isAllowed($context->getScope())) {
  58.                 continue;
  59.             }
  60.             throw new WriteConstraintViolationException(new ConstraintViolationList([
  61.                 new ConstraintViolation('This field is write-protected.''This field is write-protected.', [], $value$propertyName$value),
  62.             ]), '/' $propertyName);
  63.         }
  64.     }
  65. }