简体   繁体   English

安全性和基于Symfony 2的项目登录

[英]Security and login on a Symfony 2 based project

I'm developing a web application based on the Symfony 2 PHP framework. 我正在开发基于Symfony 2 PHP框架的Web应用程序。

It has a login page for the users registered. 它具有用于注册用户的登录页面。 I want to execute some custom logic for every user logging into the system. 我想为每个登录系统的用户执行一些自定义逻辑。

Basicaly, I want to log whenever any user logs into the system, but I don't want to do it on the main page's controller, because it would log every time the user reloads the main page. 基本上,我想在任何用户登录到系统时都进行登录,但是我不想在主页的控制器上进行登录,因为它会在用户每次重新加载主页时进行登录。

I also want to implement a function that gets called when the user logs into the system so I can decide wether the access is granted or not for any user (based on a full set of information stored on the user's database). 我还想实现一个在用户登录系统时调用的函数,以便我可以决定是否授予任何用户访问权限(基于存储在用户数据库中的全套信息)。

How can I achieve this? 我该如何实现?

For the first part of your question, I had something similar (eg store the last date & time of a user's login). 对于您的问题的第一部分,我有类似的内容(例如,存储用户登录的最后日期和时间)。 I went down the route of a service which was fired upon an event. 我沿着事件发生的服务路线走了。 In your services config (XML example here): 在您的服务配置中(此处为XML示例):

<services>

    <service id="my.login.listener" class="My\OwnBundle\Event\LoginEventListener">
      <tag name="kernel.event_listener" event="security.interactive_login" />
    </service>

</services>

and then create the above mentioned class in the appropriate place in your bundle: 然后在包中的适当位置创建上述类:

namespace My\OwnBundle\Event;

use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use My\OwnBundle\User\User as MyUser;

class LoginEventListener
{
    /**
     * Catches the login of a user and does something with it
     *
     * @param \Symfony\Component\Security\Http\Event\InteractiveLoginEvent $event
     * @return void
     */
    public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
    {
        $token = $event->getAuthenticationToken();
        if ($token && $token->getUser() instanceof MyUser)
        {
            // You can do something here eg
            // record the date & time of the user's login
        }
    }
}

I would imagine that you could extend this to the second part of your question, however I've not done this :-) 我想您可以将其扩展到问题的第二部分,但是我还没有这样做:-)

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

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