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