src/Evo/Application/Security/Voter/UserVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace Evo\Application\Security\Voter;
  3. use Evo\Application\Security\Permissions;
  4. use Evo\Infrastructure\MappingORM\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\Security;
  8. class UserVoter extends Voter
  9. {
  10.     private const SUPPORTED_ATTRIBUTES = [
  11.         Permissions::USER_DETAILS,
  12.         Permissions::USER_EDIT,
  13.     ];
  14.     private Security $security;
  15.     public function __construct(Security $security)
  16.     {
  17.         $this->security $security;
  18.     }
  19.     protected function supports(string $attribute$subject): bool
  20.     {
  21.         return $subject instanceof User && \in_array($attributeself::SUPPORTED_ATTRIBUTEStrue);
  22.     }
  23.     /**
  24.      * @param User $subject
  25.      */
  26.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  27.     {
  28.         $loggedUser $token->getUser();
  29.         if (false === $loggedUser instanceof User) {
  30.             return false;
  31.         }
  32.         return $subject->getId() === $loggedUser->getId() || $this->security->isGranted('ROLE_ADMIN'$loggedUser);
  33.     }
  34. }