繁体   English   中英

覆盖FOSUserBundle(Symfony2)中的登录控制器

[英]Override the login Controller in FOSUserBundle (Symfony2)

我目前正在从WordPress迁移到Symfony2网站。

我在Symfony2上导入了所有WP用户,但是现在我正在寻找一种在用户尝试登录时进行其他检查的方法(通常检查用户是否从WP导入并检查其旧密码)。

在用户身份验证上添加一些检查的最佳方法是什么? (登录fosuserbundle)。

我只是尝试覆盖SecurityController,但由于似乎未在此处进行登录而无法正常工作。

谢谢你的帮助。

编辑 :我需要在登录过程中而不是之后添加我的支票。 登录期间,如果用户来自WordPress,我想检查他提供的密码是否与他的旧WordPress密码相同(该密码也存储在数据库中)。

我最终找到了解决方案,但不确定这是做这些事情的最佳方法。

登录失败时,我添加了一个侦听器,并检查它是否来自WordPress。

现在,我正在寻找一种解决方案来处理“记住我”复选框,因为用户是通过编程方式进行身份验证的。 这是代码:

public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
    $username = $request->request->get('_username');
    $password = $request->request->get('_password');

    $user = $this->doctrine->getRepository('AppBundle:User')->findOneByUsername($username);

    if ($user instanceof User && $user->getFromWordpress() == true) {

        //The class use by WordPress to check / encode passwords
        $hasher = new PasswordHash(8, TRUE);    

        //User provide the right password
        if ($hasher->CheckPassword($password, $user->getWordpressPassword())){

            //Programmatically authenticate the user
            $token = new UsernamePasswordToken($user, $user->getPassword(), "main", $user->getRoles());
            $this->tokenStorage->setToken($token);
            $event = new InteractiveLoginEvent($request, $token);
            $this->eventDispacher->dispatch("security.interactive_login", $event);

            //Set the password with the Symfony2 encoder
            $encoder = $this->encoderFactory->getEncoder($user);
            $password = $encoder->encodePassword($password, $user->getSalt());
            $user->setPassword($password);
            $user->setFromWordpress(false);
            $this->doctrine->getManager()->persist($user);
            $this->doctrine->getManager()->flush();

            //Finnaly send login ok response
            return $this->onAuthenticationSuccess($request, $token);
        }
    }   

    //Login failed code ...
    //.....
}

暂无
暂无

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

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