src/Security/Voter/ProjectVoter.php line 8

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Project;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. class ProjectVoter extends AbstractVoter
  6. {
  7.     public const VIEW 'Project:View';
  8.     public const EDIT 'Project:Edit';
  9.     private ?Project $project null;
  10.     protected function supports(string $attributemixed $subject): bool
  11.     {
  12.         return in_array($attribute, [self::VIEWself::EDIT])
  13.             && $subject instanceof Project;
  14.     }
  15.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  16.     {
  17.         $this->user $token->getUser();
  18.         $this->project $subject;
  19.         return match ($attribute) {
  20.             self::VIEW => $this->canViewProject(),
  21.             self::EDIT => $this->canEditProject(),
  22.             default => false,
  23.         };
  24.     }
  25.     private function canViewProject(): bool
  26.     {
  27.         return $this->security->isGranted(MagasinVoter::VIEWsubject$this->project->getMagasin());
  28.     }
  29.     private function canEditProject(): bool
  30.     {
  31.         return $this->security->isGranted(MagasinVoter::EDITsubject$this->project->getMagasin());
  32.     }
  33. }