platform/src/Core/Framework/Api/Acl/AclAnnotationValidator.php line 34

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