src/ApiPlatform/Voter/SectionVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\ApiPlatform\Voter;
  3. use App\Entity\Section;
  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\Security;
  8. class SectionVoter extends Voter
  9. {
  10.     public const CREATE 'CREATE';
  11.     public const EDIT 'EDIT';
  12.     public const DELETE 'DELETE';
  13.     public function __construct(private readonly Security $security)
  14.     {
  15.     }
  16.     protected function supports(string $attribute$subject): bool
  17.     {
  18.         if (!$subject instanceof Section) {
  19.             return false;
  20.         }
  21.         return in_array($attribute, [self::CREATEself::EDITself::DELETE]);
  22.     }
  23.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  24.     {
  25.         $user $token->getUser();
  26.         if (!$user instanceof User) {
  27.             return false;
  28.         }
  29.         
  30.         if ($user->isMasterAdmin()) {
  31.             return true;
  32.         }
  33.         
  34.         switch ($attribute) {
  35.             case self::CREATE:
  36.                 return $this->canCreate($user$subject);
  37.             case self::EDIT:
  38.             case self::DELETE:
  39.                 return $this->canEditOrDelete($user$subject);
  40.         }
  41.         return false;
  42.     }
  43.     private function canCreate(User $userSection $section): bool
  44.     {
  45.         // For section creation, check if the user has access to any of the sites
  46.         // that the parent document is affectated to
  47.         if (!$section->getDocument() || $section->getDocument()->getAffectations()->isEmpty()) {
  48.             return false;
  49.         }
  50.         
  51.         $userSiteIds = [];
  52.         foreach ($user->getUserSites() as $userSite) {
  53.             $userSiteIds[] = $userSite->getSite()->getId();
  54.         }
  55.         
  56.         foreach ($section->getDocument()->getAffectations() as $affectation) {
  57.             if (!$affectation->getSite()) {
  58.                 continue;
  59.             }
  60.             
  61.             if (in_array($affectation->getSite()->getId(), $userSiteIds)) {
  62.                 return true;
  63.             }
  64.         }
  65.         
  66.         return false;
  67.     }
  68.     private function canEditOrDelete(User $userSection $section): bool
  69.     {
  70.         // For edit/delete, check if the user has access to any of the sites
  71.         // that the parent document is affectated to
  72.         if (!$section->getDocument() || $section->getDocument()->getAffectations()->isEmpty()) {
  73.             return false;
  74.         }
  75.         
  76.         $userSiteIds = [];
  77.         foreach ($user->getUserSites() as $userSite) {
  78.             $userSiteIds[] = $userSite->getSite()->getId();
  79.         }
  80.         
  81.         foreach ($section->getDocument()->getAffectations() as $affectation) {
  82.             if (!$affectation->getSite()) {
  83.                 continue;
  84.             }
  85.             
  86.             if (in_array($affectation->getSite()->getId(), $userSiteIds)) {
  87.                 return true;
  88.             }
  89.         }
  90.         
  91.         return false;
  92.     }
  93. }