vendor/easycorp/easyadmin-bundle/src/Security/SecurityVoter.php line 19

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\Security;
  3. use EasyCorp\Bundle\EasyAdminBundle\Contracts\Provider\AdminContextProviderInterface;
  4. use EasyCorp\Bundle\EasyAdminBundle\Dto\ActionDto;
  5. use EasyCorp\Bundle\EasyAdminBundle\Dto\CrudDto;
  6. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  7. use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
  8. use EasyCorp\Bundle\EasyAdminBundle\Dto\MenuItemDto;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  11. use Symfony\Component\Security\Core\Authorization\Voter\Vote;
  12. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  13. /**
  14.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  15.  */
  16. final class SecurityVoter extends Voter
  17. {
  18.     public function __construct(
  19.         private readonly AuthorizationCheckerInterface $authorizationChecker,
  20.         private readonly AdminContextProviderInterface $adminContextProvider,
  21.     ) {
  22.     }
  23.     protected function supports(string $permissionNamemixed $subject): bool
  24.     {
  25.         return $this->supportsAttribute($permissionName);
  26.     }
  27.     public function supportsAttribute(string $permissionName): bool
  28.     {
  29.         return Permission::exists($permissionName);
  30.     }
  31.     protected function voteOnAttribute($permissionName$subjectTokenInterface $token, ?Vote $vote null): bool
  32.     {
  33.         if (Permission::EA_VIEW_MENU_ITEM === $permissionName) {
  34.             return $this->voteOnViewMenuItemPermission($subject);
  35.         }
  36.         if (Permission::EA_EXECUTE_ACTION === $permissionName) {
  37.             return $this->voteOnExecuteActionPermission($this->adminContextProvider->getContext()->getCrud(), $subject['action'] ?? null$subject['entity'] ?? null$subject['entityFqcn'] ?? null);
  38.         }
  39.         if (Permission::EA_VIEW_FIELD === $permissionName) {
  40.             return $this->voteOnViewPropertyPermission($subject);
  41.         }
  42.         if (Permission::EA_ACCESS_ENTITY === $permissionName) {
  43.             return $this->voteOnViewEntityPermission($subject);
  44.         }
  45.         if (Permission::EA_EXIT_IMPERSONATION === $permissionName) {
  46.             return $this->voteOnExitImpersonationPermission();
  47.         }
  48.         return true;
  49.     }
  50.     private function voteOnViewMenuItemPermission(MenuItemDto $menuItemDto): bool
  51.     {
  52.         // users can see the menu item if they have the permission required by the menu item
  53.         return $this->authorizationChecker->isGranted($menuItemDto->getPermission(), $menuItemDto);
  54.     }
  55.     private function voteOnExecuteActionPermission(CrudDto $crudDtoActionDto|string $actionNameOrDto, ?EntityDto $entityDto, ?string $entityFqcn): bool
  56.     {
  57.         // users can run the Crud action if:
  58.         // * they have the required permission to execute the action on the given entity instance
  59.         // * the action is not disabled
  60.         $actionName \is_string($actionNameOrDto) ? $actionNameOrDto $actionNameOrDto->getName();
  61.         $actionPermission $crudDto->getActionsConfig()->getActionPermissions()[$actionName] ?? null;
  62.         $disabledActionNames $crudDto->getActionsConfig()->getDisabledActions();
  63.         $subject $entityDto?->getInstance() ?? $entityFqcn;
  64.         return !\in_array($actionName$disabledActionNamestrue) && $this->authorizationChecker->isGranted($actionPermission$subject);
  65.     }
  66.     private function voteOnViewPropertyPermission(FieldDto $field): bool
  67.     {
  68.         // users can see the field if they have the permission required by the field
  69.         return $this->authorizationChecker->isGranted($field->getPermission(), $field);
  70.     }
  71.     private function voteOnViewEntityPermission(EntityDto $entityDto): bool
  72.     {
  73.         // users can see the entity if they have the required permission on the specific entity instance
  74.         return $this->authorizationChecker->isGranted($entityDto->getPermission(), $entityDto->getInstance());
  75.     }
  76.     private function voteOnExitImpersonationPermission(): bool
  77.     {
  78.         // users can exit impersonation if they are currently impersonating another user.
  79.         // In Symfony, that means that current user has the special impersonator permission
  80.         if (\defined('Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter::IS_IMPERSONATOR')) {
  81.             $impersonatorPermission 'IS_IMPERSONATOR';
  82.         } else {
  83.             $impersonatorPermission 'ROLE_PREVIOUS_ADMIN';
  84.         }
  85.         return $this->authorizationChecker->isGranted($impersonatorPermission);
  86.     }
  87. }