src/ApiPlatform/Voter/ActionPlanTasksVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\ApiPlatform\Voter;
  3. use App\Entity\ActionPlanTasks;
  4. use App\Entity\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  8. use Symfony\Component\Security\Core\Security;
  9. class ActionPlanTasksVoter extends Voter implements VoterInterface
  10. {
  11.     public const CREATE 'CREATE';
  12.     public const EDIT 'EDIT';
  13.     public const DELETE 'DELETE';
  14.     public function __construct(private readonly Security $security)
  15.     {
  16.     }
  17.     protected function supports(string $attribute$subject): bool
  18.     {
  19.         if (!$subject instanceof ActionPlanTasks) {
  20.             return false;
  21.         }
  22.         return in_array($attribute, [self::CREATEself::EDITself::DELETE]);
  23.     }
  24.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  25.     {
  26.         $user $token->getUser();
  27.         if (!$user instanceof User) {
  28.             return false;
  29.         }
  30.         
  31.         if ($user->isMasterAdmin()) {
  32.             return true;
  33.         }
  34.         
  35.         switch ($attribute) {
  36.             case self::CREATE:
  37.                 return $this->canCreate($user$subject);
  38.             case self::EDIT:
  39.             case self::DELETE:
  40.                 return $this->canEditOrDelete($user$subject);
  41.         }
  42.         return false;
  43.     }    private function canCreate(User $userActionPlanTasks $task): bool
  44.     {
  45.         if (!$task->getActionPlan() || !$task->getActionPlan()->getSite()) {
  46.             return false;
  47.         }
  48.         
  49.         $actionPlanSiteId $task->getActionPlan()->getSite()->getId();
  50.         $userHasAccess false;
  51.         
  52.         foreach ($user->getUserSites() as $userSite) {
  53.             if ($userSite->getSite()->getId() === $actionPlanSiteId) {
  54.                 $userHasAccess true;
  55.                 break;
  56.             }
  57.         }
  58.         
  59.         return $userHasAccess;
  60.     }
  61.     private function canEditOrDelete(User $userActionPlanTasks $task): bool
  62.     {
  63.         if (!$task->getActionPlan() || !$task->getActionPlan()->getSite()) {
  64.             return false;
  65.         }
  66.         
  67.         $actionPlanSiteId $task->getActionPlan()->getSite()->getId();
  68.         $userHasAccess false;
  69.         
  70.         // Check if user is associated with the action plan's site
  71.         foreach ($user->getUserSites() as $userSite) {
  72.             if ($userSite->getSite()->getId() === $actionPlanSiteId) {
  73.                 $userHasAccess true;
  74.                 break;
  75.             }
  76.         }
  77.         return $userHasAccess;
  78.     }
  79. }