Symfony2 FOSUSerBundle提升了多帐户用户

时间:2015-08-08 01:37:29

标签: security symfony fosuserbundle

我正在尝试创建多个帐户的猎人,以防止拥有IP的用户在我的应用程序中创建多个帐户。

我创建了一个FOSUserBunde的登录事件监听器,这是函数。

class FOSUserLoginListener implements EventSubscriberInterface
{
    private $em;
    private $userManager;
    private $router;
    private $requestStack;
    private $mailer;
    private $templating;

public function __construct(UserManager $usermanager, Router $router, RequestStack $requestStack, TwigEngine $templating, EntityManager $em, $mailer)
{
    $this->em                   = $em;
    $this->userManager          = $usermanager;
    $this->router               = $router;
    $this->requestStack         = $requestStack;
    $this->mailer               = $mailer;
    $this->templating           = $templating;
}

/**
 * {@inheritDoc}
 */
public static function getSubscribedEvents()
{
    return array(
        FOSUserEvents::SECURITY_IMPLICIT_LOGIN => 'onImplicitLogin',
        SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
    );
}
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
    $user = $event->getAuthenticationToken()->getUser();

    if ($user instanceof UserInterface) {
        $lastIp = $this->requestStack->getCurrentRequest()->getClientIp();
        if($this->multiAccountChecker($lastIp))
        {
            $this->sendAlertToBanEmail($user, $lastIp);
            $user->setLastIP($lastIp)
                 ->setLocked(true);
            $this->userManager->updateUser($user);                
            $this->userManager->reloadUser($user);
        }
        $user->setLastIP($lastIp);
        $this->userManager->updateUser($user);
    }
}

除了一点小故障外,它工作正常。当我检测到具有相同IP的多个帐户时,数据将存储在数据库中,但被记录的用户将不会使用“此帐户已被锁定”消息关闭会话。

有人可以给我一些线索,以确保在多个帐户的情况下用户被拒绝访问?

1 个答案:

答案 0 :(得分:0)

说明

$this->userManager->updateUser($user);                
$this->userManager->reloadUser($user);

只需将用户的数据保存到数据库中,然后刷新内存中的用户。

您应该为当前用户执行程序化注销;看到答案https://stackoverflow.com/a/6474975/5192753