src/Security/Voter/UserTypeVoter.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  4. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  5. use Symfony\Component\Security\Core\Security;
  6. class UserTypeVoter extends Voter
  7. {
  8.     public const EDIT 'POST_EDIT';
  9.     public const VIEW 'POST_VIEW';
  10.     public const ADD 'POST_ADD';
  11.     public const DELETE 'POST_DELETE';
  12.     public function __construct( private readonly Security $security ) {}
  13.     protected function supports(string $attribute$subject): bool
  14.     {
  15.         return in_array($attribute, [self::EDITself::VIEWself::ADDself::DELETE])
  16.             && $subject instanceof \App\Entity\UserType;
  17.     }
  18.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  19.     {
  20.         if ($this->security->isGranted('ROLE_ADMIN'))
  21.             return true;
  22.         if ($this->security->isGranted('ROLE_MANAGER') or $this->security->isGranted('ROLE_USER')) {
  23.             return false;
  24.         }
  25.         return false;
  26.     }
  27. }