<?php
namespace App\EventSubscriber;
//use App\Entity\ActivityLog;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\SecurityEvents;
use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
/**
* Stores the locale of the user in the session after the
* login. This can be used by the LocaleSubscriber afterwards.
*/
class LoginSuccessSubscriber implements EventSubscriberInterface
{
private $session;
private $em;
public function __construct(SessionInterface $session, EntityManagerInterface $em)
{
$this->session = $session;
$this->em = $em;
}
public static function getSubscribedEvents()
{
return [
/*SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',*/
LoginSuccessEvent::class => 'onLoginSuccess'
];
}
public function onLoginSuccess(LoginSuccessEvent $event)
{
$user = $event->getUser();
$user->setLastConnected(new \DateTime());
$this->em->flush();
}
/*public function onInteractiveLogin(InteractiveLoginEvent $event)
{
$user = $event->getAuthenticationToken()->getUser();
$this->session->set('_current_user', $user->getId());
if (null !== $user->getLocale()) {
$this->session->set('_locale', $user->getLocale());
}else{
$this->session->set('_locale', 'en');
}
$user->setLastPublicIpAddress($_SERVER['REMOTE_ADDR']);
if (isset($_COOKIE['privateIp'])) {
$user->setLastPrivateIpAddress($_COOKIE['privateIp']);
}
$this->em->persist($user);
$this->em->flush();
}*/
}