src/Security/Voter/AvailabilityVoter.php line 9

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