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

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