src/Security/Voter/ProjectVoter.php line 10

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. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. class ProjectVoter extends Voter
  8. {
  9.     public const EDITABLE 'IS_EDITABLE';
  10.     public const OWNER 'IS_OWNER';
  11. //    public const VIEW = 'POST_VIEW';
  12.     protected function supports(string $attribute$subject): bool
  13.     {
  14.         // replace with your own logic
  15.         // https://symfony.com/doc/current/security/voters.html
  16.         return in_array($attribute, [self::EDITABLEself::OWNER])
  17.             && $subject instanceof \App\Entity\Project;
  18.     }
  19.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  20.     {
  21.         $user $token->getUser();
  22.         // if the user is anonymous, do not grant access
  23.         if (!$user instanceof UserInterface) {
  24.             return false;
  25.         }
  26.         if (!$subject instanceof Project) {
  27.             throw new \Exception('Wrong type somehow passed');
  28.         }
  29.         // ... (check conditions and return true to grant permission) ...
  30.         switch ($attribute) {
  31.             case self::EDITABLE:
  32.                 $application $subject->getApplication();
  33.                 return $application->isActive() && $application->isOpen() && $subject->getApplication()->getDistrict()->getUser()->contains($user);
  34.                 break;
  35.             case self::OWNER:
  36.                 return $subject->getApplication()->getDistrict()->getUser()->contains($user);
  37.                 break;
  38.         }
  39.         return false;
  40.     }
  41. }