src/Security/Voter/Api/CoachVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\Api;
  3. use App\Entity\Profile\Coach;
  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 CoachVoter extends Voter
  8. {
  9.     public const EDIT 'edit';
  10.     protected function supports(string $attribute$subject): bool
  11.     {
  12.         return in_array($attribute, [self::EDIT])
  13.             && $subject instanceof Coach;
  14.     }
  15.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  16.     {
  17.         $user $token->getUser();
  18.         if (!$user instanceof UserInterface) {
  19.             return false;
  20.         }
  21.         return match ($attribute) {
  22.             self::EDIT => $subject->getProfile()->getUser() === $user,
  23.             default => false,
  24.         };
  25.     }
  26. }