doutao4938 2012-08-16 15:30
浏览 41

Symfony 2.1 - 使用JMS \ DiExtraBundle创建身份验证侦听器

I have created the following listener:

<?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();
    }
}

But it doesn't get called. If I inject it with

    <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>

And add the constructor:

public function __construct(RegistryInterface $doctrine)
{
    $this->doctrine = $doctrine;
}

It runs like charm. Am I missing something?

  • 写回答

1条回答 默认 最新

  • dongyunwei8596 2012-08-16 15:46
    关注

    Answering my own (kinda noob) question...

    I simply forgot to add

    jms_di_extra:
        locations:
            all_bundles: false
            bundles: [ KekRozsakFrontBundle, KekRozsakSecurityBundle ]
            directories: [ "%kernel.root_dir%/../src" ]
    

    to my config.yml

    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决