src/Security/Voter/PartnerApi/ChannelVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\PartnerApi;
  3. use App\Entity\Channel\Channel;
  4. use App\Entity\PartnerApi\PartnerApiUser;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class ChannelVoter extends Voter
  8. {
  9.     public const EDIT 'CHANNEL_EDIT';
  10.     public const VIEW 'CHANNEL_VIEW';
  11.     protected function supports(string $attribute$subject): bool
  12.     {
  13.         return in_array($attribute, [self::EDITself::VIEW])
  14.             && $subject instanceof Channel;
  15.     }
  16.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  17.     {
  18.         $user $token->getUser();
  19.         // if the user is anonymous, do not grant access
  20.         if (!$user instanceof PartnerApiUser) {
  21.             return false;
  22.         }
  23.         // ... (check conditions and return true to grant permission) ...
  24.         return match ($attribute) {
  25.             self::EDIT => $this->canEdit($user$subject),
  26.             self::VIEW => $this->canView($user$subject),
  27.             default => false,
  28.         };
  29.     }
  30.     private function canEdit(PartnerApiUser $userChannel $channel): bool
  31.     {
  32.         return $this->canView($user$channel);
  33.     }
  34.     private function canView(PartnerApiUser $userChannel $channel): bool
  35.     {
  36.         return $user->getChannels()->contains($channel);
  37.     }
  38. }