2012-07-07 16:04:15 +00:00
|
|
|
<?php
|
|
|
|
namespace KekRozsak\SecurityBundle\Security;
|
|
|
|
|
|
|
|
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
|
|
|
use Symfony\Bridge\Doctrine\RegistryInterface;
|
2012-07-17 05:52:24 +00:00
|
|
|
use Symfony\Component\Security\Core\Event\AuthenticationEvent;
|
2012-08-16 17:06:00 +00:00
|
|
|
use JMS\DiExtraBundle\Annotation as DI;
|
2012-07-07 16:04:15 +00:00
|
|
|
|
2012-08-16 17:06:00 +00:00
|
|
|
/**
|
|
|
|
* @DI\Service
|
|
|
|
* @DI\Tag("kernel.event_listener", attributes={"event" = "security.authentication.success"})
|
|
|
|
*/
|
2012-07-07 16:04:15 +00:00
|
|
|
class AuthSuccess implements AuthenticationSuccessHandlerInterface
|
|
|
|
{
|
2012-08-16 17:06:00 +00:00
|
|
|
/**
|
|
|
|
* The Doctrine interface
|
|
|
|
*
|
|
|
|
* @var Symfony\Bridge\Doctrine\RegistryInterface $doctrine
|
|
|
|
*/
|
2012-08-16 13:52:41 +00:00
|
|
|
private $doctrine;
|
2012-07-07 16:04:15 +00:00
|
|
|
|
2012-08-16 17:06:00 +00:00
|
|
|
/**
|
|
|
|
* @DI\InjectParams
|
|
|
|
*
|
|
|
|
* @param \Symfony\Bridge\Doctrine\RegistryInterface $doctrine
|
|
|
|
*/
|
2012-08-16 13:52:41 +00:00
|
|
|
public function __construct(RegistryInterface $doctrine)
|
|
|
|
{
|
|
|
|
$this->doctrine = $doctrine;
|
|
|
|
}
|
2012-07-07 16:04:15 +00:00
|
|
|
|
2012-08-16 13:52:41 +00:00
|
|
|
public function onSecurityAuthenticationSuccess(AuthenticationEvent $event)
|
|
|
|
{
|
|
|
|
$user = $event->getAuthenticationToken()->getUser();
|
2013-02-26 19:40:19 +00:00
|
|
|
$em = $this->doctrine->getManager();
|
2012-08-16 13:52:41 +00:00
|
|
|
$user->setLastLoginAt(new \DateTime('now'));
|
|
|
|
$em->persist($user);
|
|
|
|
$em->flush();
|
|
|
|
}
|
2012-07-17 05:52:24 +00:00
|
|
|
|
2012-08-16 13:52:41 +00:00
|
|
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
|
|
|
|
{
|
|
|
|
$user = $token->getUser();
|
2013-02-26 19:40:19 +00:00
|
|
|
$em = $this->doctrine->getManager();
|
2012-08-16 13:52:41 +00:00
|
|
|
$user->setLastLoginAt(new \DateTime('now'));
|
|
|
|
$em->persist($user);
|
|
|
|
$em->flush();
|
|
|
|
}
|
2012-07-07 16:04:15 +00:00
|
|
|
}
|