vendor/shopware/core/Framework/Api/Acl/AclWriteValidator.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Api\Acl;
  3. use Shopware\Core\Framework\Api\Acl\Event\CommandAclValidationEvent;
  4. use Shopware\Core\Framework\Api\Acl\Role\AclRoleDefinition;
  5. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  6. use Shopware\Core\Framework\Api\Context\AdminSalesChannelApiSource;
  7. use Shopware\Core\Framework\Api\Exception\MissingPrivilegeException;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityTranslationDefinition;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\WriteCommand;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  12. use Shopware\Core\Framework\Uuid\Uuid;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  15. /**
  16.  * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  17.  */
  18. class AclWriteValidator implements EventSubscriberInterface
  19. {
  20.     private EventDispatcherInterface $eventDispatcher;
  21.     /**
  22.      * @internal
  23.      */
  24.     public function __construct(EventDispatcherInterface $eventDispatcher)
  25.     {
  26.         $this->eventDispatcher $eventDispatcher;
  27.     }
  28.     /**
  29.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  30.      */
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [PreWriteValidationEvent::class => 'preValidate'];
  34.     }
  35.     public function preValidate(PreWriteValidationEvent $event): void
  36.     {
  37.         $context $event->getContext();
  38.         $source $event->getContext()->getSource();
  39.         if ($source instanceof AdminSalesChannelApiSource) {
  40.             $context $source->getOriginalContext();
  41.             $source $context->getSource();
  42.         }
  43.         if ($context->getScope() === Context::SYSTEM_SCOPE || !$source instanceof AdminApiSource || $source->isAdmin()) {
  44.             return;
  45.         }
  46.         $commands $event->getCommands();
  47.         $missingPrivileges = [];
  48.         foreach ($commands as $command) {
  49.             $resource $command->getDefinition()->getEntityName();
  50.             $privilege $command->getPrivilege();
  51.             if ($privilege === null) {
  52.                 continue;
  53.             }
  54.             if (is_subclass_of($command->getDefinition(), EntityTranslationDefinition::class)) {
  55.                 $resource $command->getDefinition()->getParentDefinition()->getEntityName();
  56.                 if ($privilege !== AclRoleDefinition::PRIVILEGE_DELETE) {
  57.                     $privilege $this->getPrivilegeForParentWriteOperation($command$commands);
  58.                 }
  59.             }
  60.             if (!$source->isAllowed($resource ':' $privilege)) {
  61.                 $missingPrivileges[] = $resource ':' $privilege;
  62.             }
  63.             $event = new CommandAclValidationEvent($missingPrivileges$source$command);
  64.             $this->eventDispatcher->dispatch($event);
  65.             $missingPrivileges $event->getMissingPrivileges();
  66.         }
  67.         $this->tryToThrow($missingPrivileges);
  68.     }
  69.     /**
  70.      * @param list<string> $missingPrivileges
  71.      */
  72.     private function tryToThrow(array $missingPrivileges): void
  73.     {
  74.         if (!empty($missingPrivileges)) {
  75.             throw new MissingPrivilegeException($missingPrivileges);
  76.         }
  77.     }
  78.     /**
  79.      * @param WriteCommand[] $commands
  80.      */
  81.     private function getPrivilegeForParentWriteOperation(WriteCommand $command, array $commands): string
  82.     {
  83.         $pathSuffix '/translations/' Uuid::fromBytesToHex($command->getPrimaryKey()['language_id']);
  84.         $parentCommandPath str_replace($pathSuffix''$command->getPath());
  85.         $parentCommand $this->findCommandByPath($parentCommandPath$commands);
  86.         // writes to translation need privilege from parent command
  87.         // if we update e.g. a product and add translations for a new language
  88.         // the writeCommand on the translation would be an insert
  89.         if ($parentCommand) {
  90.             return (string) $parentCommand->getPrivilege();
  91.         }
  92.         // if we don't have a parentCommand it must be a update,
  93.         // because the parentEntity must already exist
  94.         return AclRoleDefinition::PRIVILEGE_UPDATE;
  95.     }
  96.     /**
  97.      * @param WriteCommand[] $commands
  98.      */
  99.     private function findCommandByPath(string $commandPath, array $commands): ?WriteCommand
  100.     {
  101.         foreach ($commands as $command) {
  102.             if ($command->getPath() === $commandPath) {
  103.                 return $command;
  104.             }
  105.         }
  106.         return null;
  107.     }
  108. }