src/Voter/SiteVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Voter;
  3. use App\Entity\Site;
  4. use App\Entity\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class SiteVoter extends Voter
  8. {
  9.     protected function supports(string $attributemixed $subject): bool
  10.     {
  11.         if ($attribute !== 'site') {
  12.             return false;
  13.         }
  14.         return $subject instanceof Site;
  15.     }
  16.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  17.     {
  18.         /** @var User $user */
  19.         $user $token->getUser();
  20.         if (!$user instanceof User) {
  21.             // the user must be logged in; if not, deny access
  22.             return false;
  23.         }
  24.         return $user->isAllowedOnSite($subject);
  25.     }
  26. }