src/Security/Voter/PageVoter.php line 12

  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Cms\Page;
  4. use App\Entity\Cms\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. class PageVoter extends Voter
  10. {
  11.     public const EDIT   'EDIT-PAGE';
  12.     public const VIEW   'VIEW-PAGE';
  13.     public const ADD    'ADD-PAGE';
  14.     public const DELETE 'DELETE-PAGE';
  15.     private AccessDecisionManagerInterface $decisionManager;
  16.     public function __construct(AccessDecisionManagerInterface $decisionManager)
  17.     {
  18.         $this->decisionManager $decisionManager;
  19.     }
  20.     protected function supports(string $attributemixed $subject): bool
  21.     {
  22.         return in_array($attribute, [self::EDITself::VIEWself::ADDself::DELETE])
  23.             && $subject instanceof Page;
  24.     }
  25.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  26.     {
  27.         $user $token->getUser();
  28.         // if the user is anonymous, do not grant access
  29.         if (!$user instanceof UserInterface or !$user instanceof User) {
  30.             return false;
  31.         }
  32.         if ($this->decisionManager->decide($token, array('ROLE_SUPER_ADMIN'))) {
  33.             return true;
  34.         }
  35.         $userSites $user->getSites();
  36.         if ($userSites != NULL) {
  37.             $pageSiteId $subject->getSite()->getId();
  38.             if (!in_array($pageSiteId$userSites)) {
  39.                 // L'utilisateur n'a pas les droits sur ce site
  40.                 return false;
  41.             }
  42.         }
  43.         if ($attribute == 'ADD') {
  44.             return $this->checkAuthorization($subject$user'ADD');
  45.         } else {
  46.             // On verifie si la page est autorisé pour les groupes de l'utilisateur
  47.             $pageGroups $subject->getGroups();
  48.             if (!$pageGroups->isEmpty()) {
  49.                 $validGroup false;
  50.                 $userGroups $user->getGroups();
  51.                 foreach ($pageGroups as $pageGroup) {
  52.                     foreach ($userGroups as $userGroup) {
  53.                         if ($pageGroup->getId() == $userGroup->getId()) {
  54.                             $validGroup true;
  55.                             break 2;
  56.                         }
  57.                     }
  58.                 }
  59.                 if (!$validGroup) {
  60.                     // L'utilisateur n'a pas de groupe autorisé pour cette page
  61.                     return false;
  62.                 }
  63.             }
  64.             // ... (check conditions and return true to grant permission) ...
  65.             switch ($attribute) {
  66.                 case self::VIEW:
  67.                     return $this->checkAuthorization($subject$user'VIEW');
  68.                 case self::EDIT:
  69.                     return $this->checkAuthorization($subject$user'EDIT');
  70.                 case self::DELETE:
  71.                     return $this->checkAuthorization($subject$user'DELETE');
  72.             }
  73.         }
  74.         throw new \LogicException('Vous n\'avez pas les droits pour être ici !');
  75.     }
  76.     private function checkAuthorization(Page $pageUser $user$code)
  77.     {
  78.         // On verifie les droits du groupe
  79.         $userGroups $user->getGroups();
  80.         if (!$userGroups->isEmpty()) {
  81.             $validAuthorization false;
  82.             foreach($userGroups as $userGroup) {
  83.                 if ($userGroup->hasAuthorization('Page'$code)) {
  84.                     $validAuthorization true;
  85.                     break;
  86.                 }
  87.             }
  88.             if (!$validAuthorization) {
  89.                 // L'utilisateur n'a pas l'autorisation demandé
  90.                 return false;
  91.             }
  92.         } else {
  93.             // L'utilisateur n'a aucun groupe, il n'a rien à faire là
  94.             return false;
  95.         }
  96.         return true;
  97.     }
  98. }