src/Listener/Jwt/JWTDecodedListener.php line 63

Open in your IDE?
  1. <?php
  2. namespace App\Listener\Jwt;
  3. use App\DataFixtures\Data\DataInit;
  4. use App\Doctrine\DBAL\DossierConnection;
  5. use App\Entity\Main\Dossier;
  6. use App\Entity\Main\DroitsAcces;
  7. use App\Entity\Main\Parametres;
  8. use App\Entity\Main\Session;
  9. use App\Entity\Main\Utilisateur;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Doctrine\ORM\Query;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTDecodedEvent;
  14. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. class JWTDecodedListener
  17. {
  18.     /**
  19.      * @var RequestStack
  20.      */
  21.     private $requestStack;
  22.     /**
  23.      * @var EntityManagerInterface
  24.      */
  25.     private $em;
  26.     /**
  27.      * @var ManagerRegistry
  28.      */
  29.     private $managerRegistry;
  30.     /**
  31.      * @var EventDispatcherInterface
  32.      */
  33.     protected $dispatcher;
  34.     /**
  35.      * @param RequestStack $requestStack
  36.      * @param EntityManagerInterface $em
  37.      * @param ManagerRegistry $managerRegistry
  38.      * @param EventDispatcherInterface $dispatcher
  39.      */
  40.     public function __construct(RequestStack $requestStackEntityManagerInterface $emManagerRegistry $managerRegistryEventDispatcherInterface $dispatcher)
  41.     {
  42.         $this->requestStack $requestStack;
  43.         $this->em $em;
  44.         $this->managerRegistry $managerRegistry;
  45.         $this->dispatcher $dispatcher;
  46.     }
  47.     /**
  48.      * @param JWTDecodedEvent $event
  49.      *
  50.      * @return void
  51.      */
  52.     public function onJWTDecoded(JWTDecodedEvent $event)
  53.     {
  54.         // si mise en cours, couper la connexion
  55.         if($_ENV['AZIZ_MISEAJOURS_BLOQUER_CONNEXION'] == "1"){
  56.             $event->markAsInvalid();
  57.         }else {
  58.             $payload $event->getPayload();
  59.             $request $this->requestStack->getCurrentRequest();
  60.             $query $request->query->all();
  61.             //1- tester si la session == utilisateur.sessionActive
  62.             // 1.1 recupérer la session via le front
  63.             if (isset($payload['username']) && isset($query['uid'])) {
  64.                 $uidFront $query['uid'];
  65.                 if (!empty($uidFront)) {
  66.                     /** @var Session $session */
  67.                     $session $this->em->getRepository(Session::class)->findOneBy(["uid" => $uidFront]);
  68.                     /** @var Utilisateur $utilisateur */
  69.                     $utilisateur $this->em->getRepository(Utilisateur::class)->findOneBy(["username" => $payload['username']]);
  70.                     if ($session && $utilisateur) {
  71.                         //- 1 si la session stcoké dans le front ne correspond pas à la session active, alors decionnectrer l'utilisteur
  72.                         if ($session->getId() != $utilisateur->getSessionActive()) {
  73.                             $event->markAsInvalid();
  74.                         }
  75.                         //2- vérifier la fermeture de session
  76.                         $idClient $utilisateur->getAbonnement()->getClient()->getId();
  77.                         $updateDateTimeSession true;
  78.                         if ($idClient) {
  79.                             /** @var Parametres $parametres */
  80.                             $parametres $this->em->getRepository(Parametres::class)->findOneBy(["client" => $idClient]);
  81.                             $dateFinSession $session->getDateTimeFin();
  82.                             if ($parametres) {
  83.                                 $parms $parametres->getParametres();
  84.                                 if (sizeof($parms) > && isset($parms[0]['fermetureSession'])) {
  85.                                     $sessionTimeout $parms[0]['fermetureSession'];
  86.                                 } else {
  87.                                     $sessionTimeout 20// en minutes !
  88.                                 }
  89.                             } else {
  90.                                 $sessionTimeout 20// en minutes !
  91.                             }
  92.                             if ($dateFinSession) {
  93.                                 // calculer le temps session incative
  94.                                 $now = new \DateTime();
  95.                                 $interval $dateFinSession->diff($nowtrue);
  96.                                 $interval_minutes $interval->format('%i');
  97.                                 $interval_heure $interval->format('%h');
  98.                                 $interval_days $interval->format('%d');
  99.                                 $interval_month $interval->format('%m');
  100.                                 $dif = ($interval_minutes $interval_heure 60 $interval_days 24 60 $interval_month 30 24 60) - $sessionTimeout;
  101.                                 if ($dif 0) {
  102.                                     $event->markAsInvalid();
  103.                                     $updateDateTimeSession false;
  104.                                 }
  105.                             }
  106.                         }
  107.                         if (isset($request->attributes->all()["_route"])) {
  108.                             if ($request->attributes->all()["_route"] == "api_historiqueCollaborateur_getAll") {
  109.                                 $updateDateTimeSession false;
  110.                             }
  111.                         }
  112.                         if ($updateDateTimeSession) {
  113.                             //3- update session
  114.                             $session->setDateTimeFin(new \DateTime());
  115.                             $this->em->persist($session);
  116.                             $this->em->flush();
  117.                         }
  118.                     }
  119.                 }
  120.             }
  121. //        if(isset($payload['markAsInvalid'])){
  122. //            if($payload['markAsInvalid'] == "1"){
  123. //                $event->markAsInvalid();
  124. //                return;
  125. //            }
  126. //        }
  127.             //2- Tester droits d'accèes aux dossier demandé
  128.             if (isset($request->attributes->all()['db']) && isset($payload['username'])) {
  129.                 $nomBase $request->attributes->all()['db'];
  130.                 $username $payload['username'];
  131.                 $sql "
  132.                 SELECT
  133.                 droitsacces.id,
  134.                 dossier.utilisable       
  135.                 FROM " DroitsAcces::class . " as droitsacces
  136.                 LEFT JOIN " Utilisateur::class . " as utilisateur WITH droitsacces.utilisateur=utilisateur.id
  137.                 left join " Dossier::class . " as dossier WITH droitsacces.dossier=dossier.id 
  138.                 WHERE utilisateur.username='" $username "' AND dossier.nomBase='" $nomBase "'";
  139.                 /** @var Query $query */
  140.                 $query $this->em->createQuery($sql);
  141.                 $result $query->getResult();
  142.                 if (!$result || sizeof($result) == 0) {
  143.                     $event->markAsInvalid();
  144.                 } else {
  145.                     // changer connextion
  146.                     /** @var DossierConnection $connection */
  147.                     $connection $this->managerRegistry->getConnection('dossier');
  148.                     $connection->changeParams($nomBase);
  149.                     $connection->reconnect();
  150.                 }
  151.             }
  152.         }
  153.     }
  154. }