src/Security/Voter/Api/ClientVoter.php line 11

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