src/ApiPlatform/Voter/ComplianceVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\ApiPlatform\Voter;
  3. use App\Entity\Compliance;
  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 ComplianceVoter 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 Compliance) {
  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 $userCompliance $compliance): bool
  44.     {
  45.         // For compliance creation, check if the user has access to any of the sites
  46.         // that the parent document (via section) is affectated to
  47.         if (!$compliance->getSection() || 
  48.             !$compliance->getSection()->getDocument() || 
  49.             $compliance->getSection()->getDocument()->getAffectations()->isEmpty()) {
  50.             return false;
  51.         }
  52.         
  53.         $userSiteIds = [];
  54.         foreach ($user->getUserSites() as $userSite) {
  55.             $userSiteIds[] = $userSite->getSite()->getId();
  56.         }
  57.         
  58.         foreach ($compliance->getSection()->getDocument()->getAffectations() as $affectation) {
  59.             if (!$affectation->getSite()) {
  60.                 continue;
  61.             }
  62.             
  63.             if (in_array($affectation->getSite()->getId(), $userSiteIds)) {
  64.                 return true;
  65.             }
  66.         }
  67.         
  68.         return false;
  69.     }
  70.     private function canEditOrDelete(User $userCompliance $compliance): bool
  71.     {
  72.         // For edit/delete, check if the user has access to any of the sites
  73.         // that the parent document (via section) is affectated to
  74.         if (!$compliance->getSection() || 
  75.             !$compliance->getSection()->getDocument() || 
  76.             $compliance->getSection()->getDocument()->getAffectations()->isEmpty()) {
  77.             return false;
  78.         }
  79.         
  80.         $userSiteIds = [];
  81.         foreach ($user->getUserSites() as $userSite) {
  82.             $userSiteIds[] = $userSite->getSite()->getId();
  83.         }
  84.         
  85.         foreach ($compliance->getSection()->getDocument()->getAffectations() as $affectation) {
  86.             if (!$affectation->getSite()) {
  87.                 continue;
  88.             }
  89.             
  90.             if (in_array($affectation->getSite()->getId(), $userSiteIds)) {
  91.                 return true;
  92.             }
  93.         }
  94.         
  95.         return false;
  96.     }
  97. }