<?php
namespace App\EventSubscriber;
use App\Entity\Log;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\AuthenticationEvents;
use Symfony\Component\Security\Core\Event\AuthenticationEvent;
use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\Event\LogoutEvent;
use Symfony\Component\Security\Http\SecurityEvents;
class AuthenticatorSubscriber implements EventSubscriberInterface
{
private $manager;
private $request;
/**
* AuthenticatorSubscriber constructor.
*/
public function __construct(EntityManagerInterface $manager, RequestStack $request)
{
$this->manager = $manager;
$this->request = $request;
}
/** @return array<string> */
public static function getSubscribedEvents()
{
return [
AuthenticationEvents::AUTHENTICATION_FAILURE => 'onSecurityAuthenticationFailure',
AuthenticationEvents::AUTHENTICATION_SUCCESS => 'onSecurityAuthenticationSuccess',
SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
'Symfony\Component\Security\Http\Event\LogoutEvent' => 'onSecurityLogout'
];
}
public function onSecurityAuthenticationFailure(AuthenticationFailureEvent $event)
{
$securityToken = $event->getAuthenticationToken();
$securityTokenException = $event->getAuthenticationException();
//dd($event);
$messageKey = $securityTokenException->getMessageKey();
['username' => $username] = $securityToken->getCredentials();
[
'user_ip' => $routeIp,
] = $this->getRouteNameAndUserIp();
$log = new Log();
$log->setLevel(1);
$log->setTitle("Login error");
$log->setDescription("L'utilisateur avec le login ".$username." a essayé de se connecter. La raison : ".$messageKey);
$log->setOthers("Adresse ip : ".$routeIp);
$this->manager->persist($log);
$this->manager->flush();
}
public function onSecurityAuthenticationSuccess(AuthenticationSuccessEvent $event)
{
[
'route_name' => $routeName,
'user_ip' => $routeIp,
] = $this->getRouteNameAndUserIp();
if (!empty($event->getAuthenticationToken()->getRoleNames())) {
//dd($event->getAuthenticationToken()->getRoleNames());
//dd($routeName);
if ($routeName == 'app_login') {
$user = $event->getAuthenticationToken()->getUser();
//dd($user);
// Create log
$log = new Log();
$log->setCreatedBy($user);
$log->setLevel(1);
$log->setTitle("Connexion utilisateur");
$log->setDescription("L'utilisateur ".$user->getUsername()." vient de se connecter");
$log->setOthers("IP : ".$routeIp);
$this->manager->persist($log);
$this->manager->flush();
}
}
}
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
//
}
public function onSecurityLogout(LogoutEvent $event)
{
//dd($event);
$response = $event->getResponse();
$securityToken = $event->getToken();
if (!$response || !$securityToken) {
return;
}
['user_ip' => $routeIp] = $this->getRouteNameAndUserIp();
$user = $securityToken->getUser();
$log = new Log();
$log->setCreatedBy($user);
$log->setLevel(1);
$log->setTitle("Deconnexion utilisateur");
$log->setDescription("L'utilisateur ".$user->getUsername()." vient de se déconnecter");
$log->setOthers("IP : ".$routeIp);
$this->manager->persist($log);
$this->manager->flush();
}
/** @return array{user_ip: string|null, route_name: mixed} */
private function getRouteNameAndUserIp()
{
$request = $this->request->getCurrentRequest();
if (!$request) {
return [
'user_ip' => 'Inconnue',
'route_name' => 'Inconnue'
];
}
return [
'user_ip' => $request->getClientIp() ? $request->getClientIp() : 'Inconnue',
'route_name' => $request->attributes->get('_route')
];
}
private function getUsername(TokenInterface $securityToken) {
$user = $securityToken->getUser();
return $user->getUsername();
}
}