src/Security/Voter/AccountVoter.php line 11

  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Account;
  4. use Symfony\Bundle\SecurityBundle\Security;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Uid\Uuid;
  8. class AccountVoter extends Voter
  9. {
  10.     public function __construct(
  11.         private readonly Security $security
  12.     ) {
  13.     }
  14.     protected function supports(string $attributemixed $subject): bool
  15.     {
  16.         return in_array($attribute, ['ACCESS_ACCOUNT_EDIT''ACCESS_ACCOUNT_READ']) && $subject instanceof Account;
  17.     }
  18.     /**
  19.      * @param string $attribute
  20.      * @param Account $subject
  21.      * @param TokenInterface $token
  22.      * @return bool
  23.      */
  24.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  25.     {
  26.         if ($this->security->isGranted('ROLE_ADMIN')) {
  27.             return true;
  28.         }
  29.         return $this->security->isGranted(str_replace('ACCESS''ROLE'$attribute)) &&
  30.             $subject->getId()->equals(Uuid::fromString($token->getUser()->account));
  31.     }
  32. }