vendor/netgen/layouts-core/lib/Security/Authorization/Voter/PolicyToRoleMapVoter.php line 19

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Netgen\Layouts\Security\Authorization\Voter;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use function is_string;
  8. use function str_starts_with;
  9. /**
  10. * Votes on Netgen Layouts permissions (nglayouts:*) by mapping the permissions to built-in roles (ROLE_NGLAYOUTS_*).
  11. *
  12. * @extends \Symfony\Component\Security\Core\Authorization\Voter\Voter<string, mixed>
  13. */
  14. final class PolicyToRoleMapVoter extends Voter
  15. {
  16. /**
  17. * Map of supported permissions to their respective roles.
  18. */
  19. private const POLICY_TO_ROLE_MAP = [
  20. 'nglayouts:block:add' => self::ROLE_EDITOR,
  21. 'nglayouts:block:edit' => self::ROLE_EDITOR,
  22. 'nglayouts:block:delete' => self::ROLE_EDITOR,
  23. 'nglayouts:block:reorder' => self::ROLE_EDITOR,
  24. 'nglayouts:layout:add' => self::ROLE_ADMIN,
  25. 'nglayouts:layout:edit' => self::ROLE_EDITOR,
  26. 'nglayouts:layout:delete' => self::ROLE_ADMIN,
  27. 'nglayouts:layout:clear_cache' => self::ROLE_ADMIN,
  28. 'nglayouts:mapping:edit' => self::ROLE_ADMIN,
  29. 'nglayouts:mapping:edit_group' => self::ROLE_ADMIN,
  30. 'nglayouts:mapping:activate' => self::ROLE_ADMIN,
  31. 'nglayouts:mapping:activate_group' => self::ROLE_ADMIN,
  32. 'nglayouts:mapping:delete' => self::ROLE_ADMIN,
  33. 'nglayouts:mapping:reorder' => self::ROLE_ADMIN,
  34. 'nglayouts:collection:edit' => self::ROLE_EDITOR,
  35. 'nglayouts:collection:items' => self::ROLE_EDITOR,
  36. 'nglayouts:ui:access' => self::ROLE_ADMIN,
  37. 'nglayouts:api:read' => self::ROLE_API,
  38. ];
  39. /**
  40. * The identifier of the admin role. Users having this role
  41. * have full and unrestricted access to the entire system.
  42. */
  43. private const ROLE_ADMIN = 'ROLE_NGLAYOUTS_ADMIN';
  44. /**
  45. * The identifier of the editor role. Users having this role
  46. * have full access only to the layout editing interface.
  47. */
  48. private const ROLE_EDITOR = 'ROLE_NGLAYOUTS_EDITOR';
  49. /**
  50. * The identifier of the API role. Users having this role
  51. * have access to read only data of the API endpoints.
  52. */
  53. private const ROLE_API = 'ROLE_NGLAYOUTS_API';
  54. private AccessDecisionManagerInterface $accessDecisionManager;
  55. public function __construct(AccessDecisionManagerInterface $accessDecisionManager)
  56. {
  57. $this->accessDecisionManager = $accessDecisionManager;
  58. }
  59. /**
  60. * @param mixed $attribute
  61. * @param mixed $subject
  62. */
  63. protected function supports($attribute, $subject): bool
  64. {
  65. return is_string($attribute) && str_starts_with($attribute, 'nglayouts:');
  66. }
  67. /**
  68. * @param string $attribute
  69. * @param mixed $subject
  70. */
  71. protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
  72. {
  73. if (!isset(self::POLICY_TO_ROLE_MAP[$attribute])) {
  74. return false;
  75. }
  76. return $this->accessDecisionManager->decide(
  77. $token,
  78. [self::POLICY_TO_ROLE_MAP[$attribute]],
  79. $subject,
  80. );
  81. }
  82. }