src/Security/Voter/AccessVoter.php line 12

  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Account;
  4. use App\Entity\AccountInterface;
  5. use Symfony\Bundle\SecurityBundle\Security;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Uid\Uuid;
  9. class AccessVoter extends Voter
  10. {
  11.     public function __construct(private readonly Security $security)
  12.     {
  13.     }
  14.     protected function supports(string $attributemixed $subject): bool
  15.     {
  16.         return $attribute === 'CAN_ACCESS' && ($subject instanceof AccountInterface or $subject instanceof Account);
  17.     }
  18.     /**
  19.      * @param string $attribute
  20.      * @param AccountInterface $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.         if ($subject instanceof Account) {
  30.             return $subject->getId()->equals(Uuid::fromString($token->getUser()->account));
  31.         }
  32.         return $subject->getAccount()->getId()->equals(Uuid::fromString($token->getUser()->account));
  33.     }
  34. }