src/Security/Voter/StationVoter.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Service\VoterSecurityService;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. class StationVoter extends Voter
  7. {
  8.     public const EDIT 'EDIT';
  9.     public const VIEW 'VIEW';
  10.     public const VIEW_PREMIUM 'VIEW_PREMIUM';
  11.     public function __construct(private readonly VoterSecurityService $voterSecurityService)
  12.     {
  13.     }
  14.     protected function supports(string $attribute$subject): bool
  15.     {
  16.         // replace with your own logic
  17.         // https://symfony.com/doc/current/security/voters.html
  18.         return in_array($attribute, [self::EDITself::VIEWself::VIEW_PREMIUM])
  19.             && $subject instanceof \App\Entity\Station;
  20.     }
  21.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  22.     {
  23.         $user $token->getUser();
  24.         if (!$this->voterSecurityService->isUserAllowed($user)) {
  25.             return false;
  26.         }
  27.         if (!$this->voterSecurityService->isUserInElement($user$subject)) {
  28.             return false;
  29.         }
  30.         return match ($attribute) {
  31.             self::VIEW => true,
  32.             self::VIEW_PREMIUM => $user->isPremium(),
  33.             self::EDIT => $this->voterSecurityService->allowedEditElement($subject),
  34.             default => false,
  35.         };
  36.     }
  37. }