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

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. /**
  14.  * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  15.  */
  16. class AclAnnotationValidator implements EventSubscriberInterface
  17. {
  18.     private Connection $connection;
  19.     /**
  20.      * @internal
  21.      */
  22.     public function __construct(Connection $connection)
  23.     {
  24.         $this->connection $connection;
  25.     }
  26.     /**
  27.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  28.      */
  29.     public static function getSubscribedEvents()
  30.     {
  31.         return [
  32.             KernelEvents::CONTROLLER => [
  33.                 ['validate'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_SCOPE_VALIDATE],
  34.             ],
  35.         ];
  36.     }
  37.     public function validate(ControllerEvent $event): void
  38.     {
  39.         $request $event->getRequest();
  40.         $privileges $request->attributes->get(PlatformRequest::ATTRIBUTE_ACL);
  41.         if (!$privileges) {
  42.             return;
  43.         }
  44.         if ($privileges instanceof Acl) {
  45.             $privileges $privileges->getValue();
  46.         }
  47.         $context $request->attributes->get(PlatformRequest::ATTRIBUTE_CONTEXT_OBJECT);
  48.         if ($context === null) {
  49.             throw new MissingPrivilegeException([]);
  50.         }
  51.         foreach ($privileges as $privilege) {
  52.             if ($privilege === 'app') {
  53.                 if ($context->isAllowed('app.all')) {
  54.                     return;
  55.                 }
  56.                 $privilege $this->getAppPrivilege($request);
  57.             }
  58.             if (!$context->isAllowed($privilege)) {
  59.                 throw new MissingPrivilegeException([$privilege]);
  60.             }
  61.         }
  62.     }
  63.     private function getAppPrivilege(Request $request): string
  64.     {
  65.         $actionId $request->get('id');
  66.         if (empty($actionId)) {
  67.             throw new MissingPrivilegeException();
  68.         }
  69.         $appName $this->connection->fetchOne(
  70.             '
  71.                 SELECT `app`.`name` AS `name`
  72.                 FROM `app`
  73.                 INNER JOIN `app_action_button` ON `app`.`id` = `app_action_button`.`app_id`
  74.                 WHERE `app_action_button`.`id` = :id
  75.             ',
  76.             ['id' => Uuid::fromHexToBytes($actionId)],
  77.         );
  78.         return 'app.' $appName;
  79.     }
  80. }