次のリスナーを作成しました。
<?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;
use Symfony\Component\Security\Core\Event\AuthenticationEvent;
use JMS\DiExtraBundle\Annotation as DI;
/**
* @DI\Service
* @DI\Tag("kernel.event_listener", attributes={"event" = "security.authentication.success"})
*/
class AuthSuccess implements AuthenticationSuccessHandlerInterface
{
/**
* The Doctrine interface
*
* @var Symfony\Bridge\Doctrine\RegistryInterface $doctrine
*
* @DI\Inject
*/
private $doctrine;
public function onSecurityAuthenticationSuccess(AuthenticationEvent $event)
{
$user = $event->getAuthenticationToken()->getUser();
$em = $this->doctrine->getEntityManager();
$user->setLastLoginAt(new \DateTime('now'));
$em->persist($user);
$em->flush();
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
$user = $token->getUser();
$em = $this->doctrine->getEntityManager();
$user->setLastLoginAt(new \DateTime('now'));
$em->persist($user);
$em->flush();
}
}
しかし、それは呼び出されません。で注入したら
<service id="kek_rozsak_security.auth.success" class="KekRozsak\SecurityBundle\Security\AuthSuccess">
<argument type="service" id="doctrine" />
<tag name="kernel.event_listener" event="security.authentication.success" />
</service>
そしてコンストラクターを追加します。
public function __construct(RegistryInterface $doctrine)
{
$this->doctrine = $doctrine;
}
それは魅力のように実行されます。何か不足していますか?