vendor/shopware/core/Framework/Api/Acl/AclAnnotationValidator.php line 37

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Api\Acl;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\Api\Exception\MissingPrivilegeException;
  5. use Shopware\Core\Framework\Routing\Annotation\Acl;
  6. use Shopware\Core\Framework\Routing\KernelListenerPriorities;
  7. use Shopware\Core\Framework\Uuid\Uuid;
  8. use Shopware\Core\PlatformRequest;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. class AclAnnotationValidator implements EventSubscriberInterface
  14. {
  15.     private Connection $connection;
  16.     public function __construct(Connection $connection)
  17.     {
  18.         $this->connection $connection;
  19.     }
  20.     /**
  21.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  22.      */
  23.     public static function getSubscribedEvents()
  24.     {
  25.         return [
  26.             KernelEvents::CONTROLLER => [
  27.                 ['validate'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_SCOPE_VALIDATE],
  28.             ],
  29.         ];
  30.     }
  31.     public function validate(ControllerEvent $event): void
  32.     {
  33.         $request $event->getRequest();
  34.         $acl $request->attributes->get('_acl');
  35.         if (!$acl || !($acl instanceof Acl)) {
  36.             return;
  37.         }
  38.         $privileges $acl->getValue();
  39.         $context $request->attributes->get(PlatformRequest::ATTRIBUTE_CONTEXT_OBJECT);
  40.         if ($context === null) {
  41.             throw new MissingPrivilegeException([]);
  42.         }
  43.         foreach ($privileges as $privilege) {
  44.             if ($privilege === 'app') {
  45.                 if ($context->isAllowed('app.all')) {
  46.                     return;
  47.                 }
  48.                 $privilege $this->getAppPrivilege($request);
  49.             }
  50.             if (!$context->isAllowed($privilege)) {
  51.                 throw new MissingPrivilegeException([$privilege]);
  52.             }
  53.         }
  54.     }
  55.     private function getAppPrivilege(Request $request): string
  56.     {
  57.         $actionId $request->get('id');
  58.         if (empty($actionId)) {
  59.             throw new MissingPrivilegeException();
  60.         }
  61.         $appName $this->connection->fetchOne(
  62.             '
  63.                 SELECT `app`.`name` AS `name`
  64.                 FROM `app`
  65.                 INNER JOIN `app_action_button` ON `app`.`id` = `app_action_button`.`app_id`
  66.                 WHERE `app_action_button`.`id` = :id
  67.             ',
  68.             ['id' => Uuid::fromHexToBytes($actionId)],
  69.         );
  70.         return 'app.' $appName;
  71.     }
  72. }