简体   繁体   中英

Override the login Controller in FOSUserBundle (Symfony2)

I'm currently migrating from a WordPress to a Symfony2 website.

I imported all my WP user on Symfony2 but now I'm looking for a way to make additional checks when the user tries to log in (typically check if the user was imported from WP and check his old password).

What's the best way to add some checks on the User authentication ? (login_check on fosuserbundle).

I simply try to override the SecurityController, but it doesn't work as the login doesn't seem to be made here.

Thanks for your help.

Edit : I need to add my check during the login process, not after. During the login, if the user comes from WordPress, I want to check if the password he provides is the same as his old WordPress password (that is stored in the DB too).

I finaly found a solution, but not sure it's the best way to do the stuff.

I added a listener when the login failed and check if it's a user from WordPress.

Now I'm looking for a solution to handle the "remember me" checkbox because the user is a authenticate programmatically. Here is the code :

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 ...
    //.....
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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