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

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