platform/src/Core/Framework/Api/Acl/AclWriteValidator.php line 35

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.     public function __construct(EventDispatcherInterface $eventDispatcher)
  22.     {
  23.         $this->eventDispatcher $eventDispatcher;
  24.     }
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [PreWriteValidationEvent::class => 'preValidate'];
  28.     }
  29.     public function preValidate(PreWriteValidationEvent $event): void
  30.     {
  31.         $context $event->getContext();
  32.         $source $event->getContext()->getSource();
  33.         if ($source instanceof AdminSalesChannelApiSource) {
  34.             $context $source->getOriginalContext();
  35.             $source $context->getSource();
  36.         }
  37.         if ($context->getScope() === Context::SYSTEM_SCOPE || !$source instanceof AdminApiSource || $source->isAdmin()) {
  38.             return;
  39.         }
  40.         $commands $event->getCommands();
  41.         $missingPrivileges = [];
  42.         foreach ($commands as $command) {
  43.             $resource $command->getDefinition()->getEntityName();
  44.             $privilege $command->getPrivilege();
  45.             if ($privilege === null) {
  46.                 continue;
  47.             }
  48.             if (is_subclass_of($command->getDefinition(), EntityTranslationDefinition::class)) {
  49.                 $resource $command->getDefinition()->getParentDefinition()->getEntityName();
  50.                 if ($privilege !== AclRoleDefinition::PRIVILEGE_DELETE) {
  51.                     $privilege $this->getPrivilegeForParentWriteOperation($command$commands);
  52.                 }
  53.             }
  54.             if (!$source->isAllowed($resource ':' $privilege)) {
  55.                 $missingPrivileges[] = $resource ':' $privilege;
  56.             }
  57.             $event = new CommandAclValidationEvent($missingPrivileges$source$command);
  58.             $this->eventDispatcher->dispatch($event);
  59.             $missingPrivileges $event->getMissingPrivileges();
  60.         }
  61.         $this->tryToThrow($missingPrivileges);
  62.     }
  63.     private function tryToThrow(array $missingPrivileges): void
  64.     {
  65.         if (!empty($missingPrivileges)) {
  66.             throw new MissingPrivilegeException($missingPrivileges);
  67.         }
  68.     }
  69.     /**
  70.      * @param WriteCommand[] $commands
  71.      */
  72.     private function getPrivilegeForParentWriteOperation(WriteCommand $command, array $commands): string
  73.     {
  74.         $pathSuffix '/translations/' Uuid::fromBytesToHex($command->getPrimaryKey()['language_id']);
  75.         $parentCommandPath str_replace($pathSuffix''$command->getPath());
  76.         $parentCommand $this->findCommandByPath($parentCommandPath$commands);
  77.         // writes to translation need privilege from parent command
  78.         // if we update e.g. a product and add translations for a new language
  79.         // the writeCommand on the translation would be an insert
  80.         if ($parentCommand) {
  81.             return (string) $parentCommand->getPrivilege();
  82.         }
  83.         // if we don't have a parentCommand it must be a update,
  84.         // because the parentEntity must already exist
  85.         return AclRoleDefinition::PRIVILEGE_UPDATE;
  86.     }
  87.     /**
  88.      * @param WriteCommand[] $commands
  89.      */
  90.     private function findCommandByPath(string $commandPath, array $commands): ?WriteCommand
  91.     {
  92.         foreach ($commands as $command) {
  93.             if ($command->getPath() === $commandPath) {
  94.                 return $command;
  95.             }
  96.         }
  97.         return null;
  98.     }
  99. }