我正在使用 Symfony4 开发一个简单的重置密码系统。
我不知道为什么isPasswordValid()总是返回false?
我正在使用 Bcrypt 对密码进行哈希处理 这是 Security.yaml 的一些代码:
security:
encoders:
App\Entity\User:
algorithm: bcrypt
我不知道为什么isPasswordValid()总是返回 false。 我手动尝试过:
$pass="000000000";
dump($encoder->isPasswordValid($user, $pass));
die();
它转储错误..
这是我在控制器上编写的函数:
/**
* @Route("/password", name="change_pass", methods={"GET","POST"})
* @IsGranted("ROLE_USER")
*/
public function edit(Request $request,UserPasswordEncoderInterface $encoder): Response
{
$user = $this->getUser();
$form = $this->createForm(ResetPassType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$oldPassword = $request->request->get('reset_pass')['oldPassword'];
$newPassword = $user->getPassword();
if ($encoder->isPasswordValid($user, $oldPassword)) {
$hash = $encoder->encodePassword($user,$newPassword);
$user->setPassword($hash);
$this->getDoctrine()->getManager()->flush();
$this->addFlash('success', 'Your password is succesfully changed');
}else {
$this->addFlash('fail', 'old password is wrong');
}
}
$this->getDoctrine()->getManager()->refresh($user);
return $this->render('consultant/changepass.html.twig', [
'form' => $form->createView(),
]);
}
这是 ResetPassType 的形式:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('oldPassword', PasswordType::class, [
'mapped' => false,])
->add('password',PasswordType::class)
;
}
当年话下
绝地无双