src/Security/Voter/EntityVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Utilisateur\Droit;
  4. use App\Entity\Utilisateur\Utilisateur;
  5. use App\Repository\Utilisateur\DroitRepository;
  6. use Psr\Log\LoggerInterface;
  7. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  11. class EntityVoter extends Voter
  12. {
  13.     public const SESSION_KEY 'droit';
  14.     public const SHOW 'voir';
  15.     public const ADD 'ajouter';
  16.     public const UPDATE 'modifier';
  17.     public const DELETE 'supprimer';
  18.     public const EXPORT 'exporter';
  19.     private RequestStack $requestStack;
  20.     private LoggerInterface $logger;
  21.     private ParameterBagInterface $parameterBag;
  22.     public function __construct(RequestStack $requestStackLoggerInterface $securityLoggerParameterBagInterface $parameterBagDroitRepository $droitRepository)
  23.     {
  24.         $this->requestStack $requestStack;
  25.         $this->logger       $securityLogger;
  26.         $this->parameterBag $parameterBag;
  27.         $this->droitRepository $droitRepository;
  28.     }
  29.     protected function supports(string $attribute$subject): bool
  30.     {
  31.         //return true;
  32.         return (!empty($subject));
  33.     }
  34.     /*
  35.     public function vote(TokenInterface $token, $subject, array $attributes): int
  36.     {
  37.         /** @var ?Utilisateur $user *//*
  38.         $user = $token->getUser();
  39.         if ($user === null) {
  40.             return self::ACCESS_ABSTAIN;
  41.         }
  42.         $entityFounded = false;
  43.         /** @var Droit[] $droits *//*
  44.         $droits = $this->requestStack->getSession()->get(self::SESSION_KEY);
  45.         if (is_string($subject) && class_exists($subject)) {
  46.             $objectClassName = $subject;
  47.         } elseif (is_object($subject)) {
  48.             $objectClassName = get_class($subject);
  49.         } else {
  50.             $this->logger->error('This entity doesn\'t exist : '.$subject);
  51.             if($this->parameterBag->get('kernel.debug') === true) {
  52. //                throw new \Exception('This entity doesn\'t exist : '.$subject);
  53.             }
  54.             return self::ACCESS_ABSTAIN;
  55.         }
  56.         foreach ($attributes as $attribute) {
  57.             if ($droits !== null) {
  58.                 foreach ($droits as $line) {
  59.                     if ($line->getEntite() === $objectClassName) {
  60.                         $entityFounded = true;
  61.                         if ($attribute === self::SHOW && $line->getVoir() === true) {
  62.                             return self::ACCESS_GRANTED;
  63.                         } elseif ($attribute === self::ADD && $line->getAjouter() === true) {
  64.                             return self::ACCESS_GRANTED;
  65.                         } elseif ($attribute === self::UPDATE && $line->getModifier() === true) {
  66.                             return self::ACCESS_GRANTED;
  67.                         } elseif ($attribute === self::DELETE && $line->getSupprimer() === true) {
  68.                             return self::ACCESS_GRANTED;
  69.                         } elseif ($attribute === self::EXPORT && $line->getExporter() === true) {
  70.                             return self::ACCESS_GRANTED;
  71.                         }
  72.                     }
  73.                 }
  74.             }
  75.         }
  76.         if ( ! $entityFounded) {
  77.             if($this->parameterBag->get('kernel.debug') === true) {
  78. //                throw new \Exception('This entity not found in rights : '.$objectClassName);
  79.             }
  80.             $this->logger->warning('This entity not found in rights : '.$objectClassName);
  81.         }
  82.         return self::ACCESS_ABSTAIN;
  83.     }
  84. */
  85.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  86.     {
  87.         /** @var ?Utilisateur $user */
  88.         $user $token->getUser();
  89.         if ($user === null) {
  90.             return self::ACCESS_ABSTAIN;
  91.         }
  92.         $entityFounded false;
  93.         /** @var Droit[] $droits */
  94.         $droits $this->droitRepository->findBy(["typesUtilisateur" => $token->getUser()->getType()->getId()]);
  95.         //$droits = $this->requestStack->getSession()->get(self::SESSION_KEY);
  96.         /*
  97.         if($this->requestStack->getSession() === null){
  98.             $session = $this->requestStack->getSession();
  99.             $droits = $this->droitRepository->findBy(["typesUtilisateur" => $token->getUser()->getType()->getId()]);
  100.             $session->set(self::SESSION_KEY, $droits);
  101.         }
  102.         */
  103.         if (is_string($subject) && class_exists($subject)) {
  104.             $objectClassName $subject;
  105.         } elseif (is_object($subject)) {
  106.             $objectClassName get_class($subject);
  107.         } else {
  108.             $this->logger->error('This entity doesn\'t exist : '.$subject);
  109.             if($this->parameterBag->get('kernel.debug') === true) {
  110. //                throw new \Exception('This entity doesn\'t exist : '.$subject);
  111.             }
  112.             return self::ACCESS_ABSTAIN;
  113.         }
  114.         $attributes = [$attribute];
  115.         foreach ($attributes as $attribute) {
  116.             if ($droits !== null) {
  117.                 foreach ($droits as $line) {
  118.                     if ($line->getEntite() === $objectClassName) {
  119.                         $entityFounded true;
  120.                         if ($attribute === self::SHOW && $line->getVoir() === true) {
  121.                             return self::ACCESS_GRANTED;
  122.                         } elseif ($attribute === self::ADD && $line->getAjouter() === true) {
  123.                             return self::ACCESS_GRANTED;
  124.                         } elseif ($attribute === self::UPDATE && $line->getModifier() === true) {
  125.                             return self::ACCESS_GRANTED;
  126.                         } elseif ($attribute === self::DELETE && $line->getSupprimer() === true) {
  127.                             return self::ACCESS_GRANTED;
  128.                         } elseif ($attribute === self::EXPORT && $line->getExporter() === true) {
  129.                             return self::ACCESS_GRANTED;
  130.                         }
  131.                     }
  132.                 }
  133.             }
  134.         }
  135.         if ( ! $entityFounded) {
  136.             if($this->parameterBag->get('kernel.debug') === true) {
  137. //                throw new \Exception('This entity not found in rights : '.$objectClassName);
  138.             }
  139.             $this->logger->warning('This entity not found in rights : '.$objectClassName);
  140.         }
  141.         return self::ACCESS_ABSTAIN;
  142.     }
  143. }