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