只允许在使用FOSUserBundle的同一登录上有一个连接 [英] allow only one connection on the same login with FOSUserBundle

查看:473
本文介绍了只允许在使用FOSUserBundle的同一登录上有一个连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Symfony2和FOSUserBundle创建了一个网站。



我在同一登录上拒绝多个连接(但是从不同的计算机) 。
我有两个解决方案:




  • 在验证时创建一个事件列表,但我没有成功。 (即使使用食谱)。

  • 覆盖login_check方法,但是如果我这样做,我的FOSUserBundle不会工作。



你有什么更好的选择吗?
或任何解决方案?

解决方案

只有最后一次更新来解决这一切。
您需要向用户实体添加其他字段。 sessionId(string)。
然后更新你的LoginListener类,如下:

  // YourSite\UserBundle\Listener\YourSiteLoginListener.php 

// ...
public function onSecurityInteractiveLogin(InteractiveLoginEvent $ event)
{
$ request = $ event-> getRequest
$ session = $ request-> getSession();
$ user = $ event-> getAuthenticationToken() - > getUser();
$ has_session = is_file('/ path_to_your_php_session_file /'。'sess _'。$ user-> getSessionId());

if($ user-> getLogged()& $ has_session){
throw new AuthenticationException('this user was already logged');
} else {
$ user-> setLogged(true);
$ user-> setSessionId($ session-> getId());
$ this-> userManager-> updateUser($ user);
}
}


I'm creating a website thanks to Symfony2 with FOSUserBundle.

I'm triyng to deny multiple connections on the same login (but from different computers for example). I've 2 solutions :

  • Create an event listner on authentification but I didn't manage to make it. (even with the cookbook).
  • override the login_check method but my FOSUserBundle doesn't work if I do it.

Do you have any better options? Or any solutions?

解决方案

Got it finaly. There is just one last update to make to solve it all. You need to add an other field to the User entity. sessionId (string). Then update your LoginListener class like that :

// YourSite\UserBundle\Listener\YourSiteLoginListener.php

//...
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
    $request = $event->getRequest();
    $session = $request->getSession();
    $user = $event->getAuthenticationToken()->getUser();
    $has_session = is_file ( '/path_to_your_php_session_file/'.'sess_'.$user->getSessionId() );

    if($user->getLogged() && $has_session){
        throw new AuthenticationException('this user is already logged');
    }else{
        $user->setLogged(true);
        $user->setSessionId($session->getId());
        $this->userManager->updateUser($user);
    }   
}

这篇关于只允许在使用FOSUserBundle的同一登录上有一个连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆