我目前正在从WordPress迁移到Symfony2网站。
我在Symfony2上导入了我的所有WP用户,但现在我正在寻找一种方法,在用户尝试登录时进行额外检查(通常检查用户是否是从WP导入的,并检查他的旧密码)。
在用户身份验证上添加一些检查的最佳方法是什么(登录fosuserbundle)。
我只是试图覆盖SecurityController,但它不起作用,因为这里似乎没有登录。
谢谢你的帮助。
编辑:我需要在登录过程中添加我的检查,而不是在登录之后。在登录过程中,如果用户来自WordPress,我想检查他提供的密码是否与他的旧WordPress密码相同(也存储在DB中)。
我终于找到了一个解决方案,但不确定这是最好的方法。
当登录失败时,我添加了一个监听器,并检查它是否是来自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 ...
//.....
}