src/Security/Voter/ProducerVoter.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 ProducerVoter 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\Producer;
  17.     }
  18.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  19.     {
  20.         $isMyOrganization $this->security->getUser()->getOrganization() == $subject->getOrganization();
  21.         if ($this->security->isGranted('ROLE_ADMIN'))
  22.             return true;
  23.         if ($this->security->isGranted('ROLE_MANAGER')) {
  24.             switch ( $attribute ) {
  25.                 case self::ADD:
  26.                     return true;
  27.                 default:
  28.                     if ($isMyOrganization)
  29.                         return true;
  30.                     return false;
  31.             }
  32.         }
  33.         if ($this->security->isGranted('ROLE_USER')) {
  34.             switch ( $attribute ) {
  35.                 case self::VIEW:
  36.                     return true;
  37.                 default:
  38.                     return false;
  39.             }
  40.         }
  41.         return false;
  42.     }
  43. }