src/Security/Voter/SpaceVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Space;
  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 SpaceVoter extends Voter
  9. {
  10.     public const VIEW '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::VIEW])
  16.             && $subject instanceof Space;
  17.     }
  18.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  19.     {
  20.         $user $token->getUser();
  21.         // if the user is anonymous, do not grant access
  22.         if (!$user instanceof UserInterface) {
  23.             return false;
  24.         }
  25.         switch ($attribute) {
  26.             case self::VIEW:
  27.                 return $this->canView($subject$user);
  28.         }
  29.         return false;
  30.     }
  31.     private function canView(Space $spaceUser $user): bool
  32.     {
  33.         return $user->getClient()->getClientType()->hasSpaceAvailable($space);
  34.     }
  35. }