src/Security/PermissionsVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\Utilisateur;
  4. use Doctrine\DBAL\Connection;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. /**
  9.  * Gestion des permissions utilisateur
  10.  */
  11. class PermissionsVoter extends Voter
  12. {
  13.     public const CREATE 'create';
  14.     public const EDIT 'edit';
  15.     /** @var Connection $dbconn */
  16.     private $dbconn;
  17.     private $activeStructure;
  18.     private $userLevel;
  19.     public function __construct(Connection $dbconnRequestStack $requestStack)
  20.     {
  21.         $this->dbconn $dbconn;
  22.         $session $requestStack->getSession();
  23.         $this->activeStructure $session->get('activeStructure');
  24.         $this->userLevel $session->get('userLevel');
  25.     }
  26.     /**
  27.      * {@inheritdoc}
  28.      */
  29.     protected function supports(string $attribute$subject): bool
  30.     {
  31.         // if the attribute isn't one we support, return false
  32.         if (!in_array($attribute, [self::CREATEself::EDIT])) {
  33.             return false;
  34.         }
  35.         $subject = (array) $subject;
  36.         if (!in_array($subject[0], ['antenne''couveuse''entrepreneur'])) {
  37.             return false;
  38.         }
  39.         return true;
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  45.     {
  46.         $user $token->getUser();
  47.         if (!$user instanceof Utilisateur) {
  48.             // the user must be logged in; if not, deny access
  49.             return false;
  50.         }
  51.         switch ($attribute) {
  52.             case self::CREATE:
  53.                 return $this->canCreate($user$subject);
  54.             case self::EDIT:
  55.                 return $this->canEdit($user, ...$subject);
  56.         }
  57.         throw new \LogicException('This code should not be reached!');
  58.     }
  59.     /** @param Utilisateur $user */
  60.     private function canCreate($user$subject)
  61.     {
  62.         // 10 UCE
  63.         // 20 Couveuse/Pépinière
  64.         // 30 Antenne couveuse
  65.         // 100 Entrepreneur
  66.         $level null;
  67.         $lvlMax null;
  68.         switch ($subject) {
  69.             case 'antenne':
  70.                 $level 30;
  71.                 $lvlMax 0;
  72.                 break;
  73.             case 'couveuse':
  74.                 $level 20;
  75.                 $lvlMax 0;
  76.                 break;
  77.             case 'entrepreneur':
  78.                 $level 30;
  79.                 $lvlMax 10;
  80.                 break;
  81.         }
  82.         if (
  83.             $this->userLevel == 'admin'
  84.             &&  $this->activeStructure->getNiveau() < $level
  85.             &&  $this->activeStructure->getNiveau() > $lvlMax
  86.         ) {
  87.             return true;
  88.         }
  89.         $query '
  90.             SELECT *
  91.             FROM structure s, utilisateur_structure us
  92.             WHERE us.idstructure = s.idstructure
  93.             AND s.idparent = ' $this->activeStructure->getIdstructure() . '
  94.             AND us.idutilisateur = ' $user->getId();
  95.         $droits $this->dbconn->fetchAllAssociative($query);
  96.         if ($subject != 'couveuse') {
  97.             return !empty($droits);
  98.         }
  99.         return false;
  100.     }
  101.     /** @param Utilisateur $user */
  102.     private function canEdit($user$subject$id)
  103.     {
  104.         $canEdit false;
  105.         if ($this->userLevel == 'admin') {
  106.             return true;
  107.         }
  108.         if ($subject != 'entrepreneur') {
  109.             $query 'SELECT * FROM structure s, ' $subject ' e, utilisateur_structure us WHERE us.idstructure = s.idstructure AND s.idstructure = e.idstructure AND id' $subject ' = ' $id ' AND us.idutilisateur = ' $user->getId();
  110.             $structure $this->dbconn->fetchAssociative($query);
  111.             // L'utilisateur peut modifier une antenne si il en fait partie
  112.             if ($subject == 'antenne') {
  113.                 if ($structure) {
  114.                     $canEdit true;
  115.                 }
  116.             }
  117.             // L'utilisateur peut modifier une couveuse si il en admin
  118.             else {
  119.                 if ($structure['isadmin'] == 't') {
  120.                     $canEdit true;
  121.                 }
  122.             }
  123.         }
  124.         // L'utilisateur peut modifier un entrepreneur si il en admin
  125.         else {
  126.             $query 'SELECT us.isadmin FROM structure s, utilisateur_structure us, utilisateur u, entrepreneur e WHERE us.idstructure = s.idstructure AND s.idstructure = e.idstructure AND u.id = e.idutilisateur AND e.identrepreneur = ' $id ' AND us.idutilisateur = ' $user->getId();
  127.             $droit $this->dbconn->fetchAssociative($query);
  128.             if ($droit) {
  129.                 $canEdit true;
  130.             }
  131.         }
  132.         return $canEdit;
  133.     }
  134. }