src/Security/Voter/Customer/IllnessVoter.php line 14

  1. <?php
  2. namespace App\Security\Voter\Customer;
  3. use App\Entity\Customer\Illness;
  4. use App\Entity\Admin\CustomerUser;
  5. use App\Repository\Customer\SettingsRepository;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. use Symfony\Bundle\SecurityBundle\Security;
  10. class IllnessVoter extends Voter
  11. {
  12.     final public const EDIT   'ILLNESS_ENTITY_EDIT';
  13.     final public const VIEW   'ILLNESS_ENTITY_VIEW';
  14.     final public const DELETE 'ILLNESS_ENTITY_DELETE';
  15.      
  16.     public function __construct(
  17.             private readonly Security $security, private readonly SettingsRepository $settingsRepository)
  18.     {
  19.     }
  20.     
  21.     protected function supports(string $attributemixed $subject): bool
  22.     {
  23.         if(!in_array($attribute, [self::EDITself::VIEWself::DELETE])){
  24.             return false;
  25.         }
  26.         if (!$subject instanceof Illness) {
  27.             return false;
  28.         }
  29.         
  30.         return true;
  31.     }
  32.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  33.     {
  34.         $user $token->getUser();
  35.         // if the user is anonymous, do not grant access
  36.         if (!$user instanceof UserInterface) {
  37.             return false;
  38.         }
  39.         if (!$this->security->isGranted('ROLE_STAFF')){
  40.             return false;
  41.         }
  42.        
  43.         // ... (check conditions and return true to grant permission) ...
  44.        return match($attribute) {
  45.             self::VIEW => $this->canView($subject$user),
  46.             self::EDIT => $this->canEdit($subject),
  47.             self::DELETE => $this->canDelete(),
  48.             default => throw new \LogicException('This code should not be reached!')
  49.         };
  50.     }
  51.     
  52.     private function canView(Illness $entityCustomerUser $user): bool
  53.     {
  54.          if ($this->canEdit($entity)) {
  55.             return true;
  56.         }
  57.         
  58.         if ($entity->getStaff()->getId() == $user->getStaff()) {
  59.             return true;
  60.         }
  61.        
  62.         return false;
  63.     }
  64.     private function canEdit(?Illness $entity): bool
  65.     {
  66.         if ($entity == null){
  67.             return true;
  68.         }
  69.         
  70.         if ($this->security->isGranted('ROLE_ADMIN')) {
  71.             return true;
  72.         }
  73.         $settings $this->settingsRepository->getByKey("staff_illness_application");
  74.         
  75.         if ($settings->getMetaValueString() == "1" and $entity->getStaff() == null) {
  76.             return true;
  77.         }              
  78.         return false;
  79.     }
  80.     
  81.     private function canDelete(): bool
  82.     {
  83.         if ($this->security->isGranted('ROLE_ADMIN')) {
  84.             return true;
  85.         }
  86.         
  87.         return false;
  88.     }
  89. }