src/Application/Symfony/EventSubscriber/Kernel/LoginSubscriber.php line 78

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the MADIS - RGPD Management application.
  4.  *
  5.  * @copyright Copyright (c) 2018-2019 Soluris - Solutions Numériques Territoriales Innovantes
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU Affero General Public License as published by
  9.  * the Free Software Foundation, either version 3 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU Affero General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Affero General Public License
  18.  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  19.  */
  20. declare(strict_types=1);
  21. namespace App\Application\Symfony\EventSubscriber\Kernel;
  22. use App\Domain\Reporting\Dictionary\LogJournalActionDictionary;
  23. use App\Domain\Reporting\Dictionary\LogJournalSubjectDictionary;
  24. use App\Domain\Reporting\Model\LogJournal;
  25. use App\Domain\Reporting\Repository\LogJournal as LogRepository;
  26. use App\Domain\User\Model\User;
  27. use Doctrine\ORM\EntityManagerInterface;
  28. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  29. use Symfony\Component\Security\Core\Security;
  30. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  31. use Symfony\Component\Security\Http\SecurityEvents;
  32. class LoginSubscriber implements EventSubscriberInterface
  33. {
  34.     /**
  35.      * @var EntityManagerInterface
  36.      */
  37.     private $entityManager;
  38.     /**
  39.      * @var LogRepository
  40.      */
  41.     private $logJournalRepository;
  42.     /**
  43.      * @var string
  44.      */
  45.     private $logJournalDuration;
  46.     /**
  47.      * @var Security
  48.      */
  49.     private $security;
  50.     public function __construct(
  51.         EntityManagerInterface $em,
  52.         LogRepository $logRepository,
  53.         Security $security,
  54.         string $logJournalDuration
  55.     ) {
  56.         $this->entityManager        $em;
  57.         $this->logJournalRepository $logRepository;
  58.         $this->security             $security;
  59.         $this->logJournalDuration   $logJournalDuration;
  60.     }
  61.     public static function getSubscribedEvents()
  62.     {
  63.         return [
  64.             SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
  65.         ];
  66.     }
  67.     public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
  68.     {
  69.         /** @var User $user */
  70.         $user $event->getAuthenticationToken()->getUser();
  71.         $user->setLastLogin(new \DateTimeImmutable());
  72.         $this->entityManager->persist($user);
  73.         if ($this->security->isGranted('ROLE_ADMIN')) {
  74.             $this->logJournalRepository->deleteAllAnteriorToDate(new \DateTime('-' $this->logJournalDuration));
  75.         }
  76.         $log = new LogJournal(
  77.             $user->getCollectivity(),
  78.             $user->getFullName(),
  79.             $user->getEmail(),
  80.             LogJournalActionDictionary::LOGIN,
  81.             LogJournalSubjectDictionary::USER_USER,
  82.             $user->getId()->toString(),
  83.             $user->getFullName()
  84.         );
  85.         $this->entityManager->persist($log);
  86.         $this->entityManager->flush();
  87.     }
  88. }