src/Security/Voter/CoachUserVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\CoachUser;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. class CoachUserVoter extends Voter
  8. {
  9.     public const EDIT 'POST_EDIT';
  10.     public const VIEW 'POST_VIEW';
  11.     protected function supports(string $attribute$subject): bool
  12.     {
  13.         // replace with your own logic
  14.         // https://symfony.com/doc/current/security/voters.html
  15.         return in_array($attribute, [self::EDITself::VIEW])
  16.             && $subject instanceof \App\Entity\CoachUser;
  17.     }
  18.     /**
  19.      * @param string $attribute
  20.      * @param CoachUser $subject
  21.      * @param TokenInterface $token
  22.      * @return bool
  23.      */
  24.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  25.     {
  26.         $user $token->getUser();
  27.         // if the user is anonymous, do not grant access
  28.         if (!$user instanceof UserInterface) {
  29.             return false;
  30.         }
  31.         return match ($attribute) {
  32.             self::VIEWself::EDIT => $subject->getCoach()->getProfile()->getUser()->getUserIdentifier() === $user->getUserIdentifier(),
  33.             default => false
  34.         };
  35.     }
  36. }