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. class AclWriteValidator implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var EventDispatcherInterface
  19.      */
  20.     private $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.     private function tryToThrow(array $missingPrivileges): void
  70.     {
  71.         if (!empty($missingPrivileges)) {
  72.             throw new MissingPrivilegeException($missingPrivileges);
  73.         }
  74.     }
  75.     /**
  76.      * @param WriteCommand[] $commands
  77.      */
  78.     private function getPrivilegeForParentWriteOperation(WriteCommand $command, array $commands): string
  79.     {
  80.         $pathSuffix '/translations/' Uuid::fromBytesToHex($command->getPrimaryKey()['language_id']);
  81.         $parentCommandPath str_replace($pathSuffix''$command->getPath());
  82.         $parentCommand $this->findCommandByPath($parentCommandPath$commands);
  83.         // writes to translation need privilege from parent command
  84.         // if we update e.g. a product and add translations for a new language
  85.         // the writeCommand on the translation would be an insert
  86.         if ($parentCommand) {
  87.             return (string) $parentCommand->getPrivilege();
  88.         }
  89.         // if we don't have a parentCommand it must be a update,
  90.         // because the parentEntity must already exist
  91.         return AclRoleDefinition::PRIVILEGE_UPDATE;
  92.     }
  93.     /**
  94.      * @param WriteCommand[] $commands
  95.      */
  96.     private function findCommandByPath(string $commandPath, array $commands): ?WriteCommand
  97.     {
  98.         foreach ($commands as $command) {
  99.             if ($command->getPath() === $commandPath) {
  100.                 return $command;
  101.             }
  102.         }
  103.         return null;
  104.     }
  105. }