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

  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. final class PolicyToRoleMapVoter extends Voter
  13. {
  14.     /**
  15.      * Map of supported permissions to their respective roles.
  16.      */
  17.     private const POLICY_TO_ROLE_MAP = [
  18.         'nglayouts:block:add' => self::ROLE_EDITOR,
  19.         'nglayouts:block:edit' => self::ROLE_EDITOR,
  20.         'nglayouts:block:delete' => self::ROLE_EDITOR,
  21.         'nglayouts:block:reorder' => self::ROLE_EDITOR,
  22.         'nglayouts:layout:add' => self::ROLE_ADMIN,
  23.         'nglayouts:layout:edit' => self::ROLE_EDITOR,
  24.         'nglayouts:layout:delete' => self::ROLE_ADMIN,
  25.         'nglayouts:layout:clear_cache' => self::ROLE_ADMIN,
  26.         'nglayouts:mapping:edit' => self::ROLE_ADMIN,
  27.         'nglayouts:mapping:edit_group' => self::ROLE_ADMIN,
  28.         'nglayouts:mapping:activate' => self::ROLE_ADMIN,
  29.         'nglayouts:mapping:activate_group' => self::ROLE_ADMIN,
  30.         'nglayouts:mapping:delete' => self::ROLE_ADMIN,
  31.         'nglayouts:mapping:reorder' => self::ROLE_ADMIN,
  32.         'nglayouts:collection:edit' => self::ROLE_EDITOR,
  33.         'nglayouts:collection:items' => self::ROLE_EDITOR,
  34.         'nglayouts:ui:access' => self::ROLE_ADMIN,
  35.         'nglayouts:api:read' => self::ROLE_API,
  36.     ];
  37.     /**
  38.      * The identifier of the admin role. Users having this role
  39.      * have full and unrestricted access to the entire system.
  40.      */
  41.     private const ROLE_ADMIN 'ROLE_NGLAYOUTS_ADMIN';
  42.     /**
  43.      * The identifier of the editor role. Users having this role
  44.      * have full access only to the layout editing interface.
  45.      */
  46.     private const ROLE_EDITOR 'ROLE_NGLAYOUTS_EDITOR';
  47.     /**
  48.      * The identifier of the API role. Users having this role
  49.      * have access to read only data of the API endpoints.
  50.      */
  51.     private const ROLE_API 'ROLE_NGLAYOUTS_API';
  52.     private AccessDecisionManagerInterface $accessDecisionManager;
  53.     public function __construct(AccessDecisionManagerInterface $accessDecisionManager)
  54.     {
  55.         $this->accessDecisionManager $accessDecisionManager;
  56.     }
  57.     /**
  58.      * @param mixed $attribute
  59.      * @param mixed $subject
  60.      */
  61.     protected function supports($attribute$subject): bool
  62.     {
  63.         return is_string($attribute) && str_starts_with($attribute'nglayouts:');
  64.     }
  65.     /**
  66.      * @param string $attribute
  67.      * @param mixed $subject
  68.      */
  69.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  70.     {
  71.         if (!isset(self::POLICY_TO_ROLE_MAP[$attribute])) {
  72.             return false;
  73.         }
  74.         return $this->accessDecisionManager->decide(
  75.             $token,
  76.             [self::POLICY_TO_ROLE_MAP[$attribute]],
  77.             $subject,
  78.         );
  79.     }
  80. }