重置密码symfony 4(用户未登录)

时间:2019-01-14 16:28:13

标签: symfony symfony4 forgot-password

当我尝试在非登录用户上重置密码时遇到问题。

实体用户:

class Users implements AdvancedUserInterface, \Serializable, TwoFactorInterface, TrustedDeviceInterface  {

[...]
}

重置密码操作:

public function forgetPassword(Request $request, EntityManagerInterface $em, UserPasswordEncoderInterface $encoder)
    {
        $changePasswordModel = new ChangePassword();
        $form = $this->createForm(ForgetPasswordType::class, $changePasswordModel);
        $form->handleRequest($request);


        $user = $em->getRepository("App:Users")->find(12);
        if (!$request->get('token') || !$user || $user->getTokenExpire() <= new \DateTime())
        {
            return $this->redirectToRoute("404");
        }

        if ($form->isSubmitted() && $form->isValid())
        {
            $plainPassword = $form->getData()->getNewPassword();
            $encoded = $encoder->encodePassword($user, $plainPassword);
            $user->setPassword($encoded);
            $em->persist($user);
            $em->flush();

        }

        return $this->render('security/forget_password.html.twig', array(
            'form'    => $form->createView(),
            'success' => $success
        ));
    }

但是我得到这个错误:

The User object must implement the UserInterface interface.

此错误的确切位置是: \ vendor \ symfony \ security \ Core \ Validator \ Constraints \ UserPasswordValidator.php:

[...]
 $user = $this->tokenStorage->getToken()->getUser();

        if (!$user instanceof UserInterface) {
            throw new ConstraintDefinitionException('The User object must 
            implement the UserInterface interface.');
        }
[...]

用户登录后可以正常工作,但是当用户使用重置密码时,这是因为他丢失了密码。因此,它应该在用户未登录时起作用。

感谢您的帮助!

亚历克斯

0 个答案:

没有答案