简体   繁体   English

将日志添加到用户登录/注销Symfony2

[英]Add log to user login/logout Symfony2

I have a simple SecurityBundle following the symfony.com documentation I've created: 按照我创建的symfony.com文档,我有一个简单的SecurityBundle:

  • UserRepository which implements UserProviderInterface 实现UserProviderInterface UserRepository
  • UserEntity which implements UserInterface and EquatableInterface 实现UserInterfaceEquatableInterface
  • SecurityController which has a loginAction 具有loginAction
  • setup routing with login, login_check, logout routes 使用登录,login_check和注销路由设置路由
  • setup my firewall settings 设置我的防火墙设置

public function loginAction()
{
    $request = $this->getRequest();
    $session = $request->getSession();

    if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
        $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
    } else {
        $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
        $session->remove(SecurityContext::AUTHENTICATION_ERROR);
    }

    return $this->render('MySecurityBundle:Security:login.html.twig', array(
        'last_username' => $session->get(SecurityContext::LAST_USERNAME),
        'error' => $error,
    ));
}

Everything is OK, but i need add a logger for every action by user, example: USER:username, DO:erase file XXXXX, AT:2013-08-08.15-03-43 the problem is i dont have control in the logout, login action because the firewall catch these and i dont know how to modify 一切正常,但我需要为用户执行的每个操作添加一个记录器,例如: USER:username, DO:erase file XXXXX, AT:2013-08-08.15-03-43问题是我无法控制注销,登录操作,因为防火墙捕获了这些,我不知道如何修改

Login event can be catched by security.interactive_login event. 可以通过security.interactive_login事件捕获登录事件。 Read here . 在这里阅读

For logout needs to specify your own success_handler in logout firewall. 对于注销,需要在注销防火墙中指定您自己的success_handler Read here . 在这里阅读 The same can be done for a login. 可以对登录执行相同的操作。

firewalls:
    main:
        form_login:
            failure_handler: mybundle.security.handler.failure
            success_handler:mybundle.security.handler.success
        logout:
            success_handler: mybundle.security.handler.logout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM