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

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