简体   繁体   English

Symonfy 3-使用REST API重置FOSUserBundle密码

[英]Symonfy 3 - FOSUserBundle reset password with REST API

I am developing a REST API with Symfony 3 . 我正在使用Symfony 3开发REST API。 Now I want to send an user a link to reset his password. 现在,我想向用户发送一个链接以重置其密码。 This should be done by sending a request to something like: 这可以通过将请求发送至以下内容来完成:

    "/resetpassword/{userEmail}"

How can I create a link for the user to reset the password? 如何为用户创建一个链接以重置密码? I allready now how to send an email by my own, but not how to generate this link or how to handle the link. 我现在已经准备好如何通过自己的方式发送电子邮件,而不是如何生成此链接或如何处理该链接。

FOSUserBundle already has all the needed functionality. FOSUserBundle已经具有所有必需的功能。

1) find the user from requested email 1)从请求的电子邮件中找到用户
2) check that current user hasn't already requested new password 2)检查当前用户尚未请求新密码
3) generate confirmation token for user 3)为用户生成确认令牌
4) use FOSUserBundle mailer implementation for sending resetting email message 4)使用FOSUserBundle邮件程序实现来发送重置电子邮件
5) update user 5)更新用户
6) return response 6)返回响应

You could do something like this: 您可以执行以下操作:

    /**
     * @Route("/resetpassword/{userEmail}", name="user_password_reset-request")
     * @Method("GET")
     */
    public function resetPasswordRequestAction(Request $request)
    {
        $email = $request->query->get('userEmail');
        $user = $this->get('fos_user.user_manager')->findUserByEmail($email);
        if (null === $user) {
            throw $this->createNotFoundException();
        }

        if ($user->isPasswordRequestNonExpired($this->container->getParameter('fos_user.resetting.token_ttl'))) {
            throw new BadRequestHttpException('Password request alerady requested');
        }

        if (null === $user->getConfirmationToken()) {
            /** @var $tokenGenerator \FOS\UserBundle\Util\TokenGeneratorInterface */
            $tokenGenerator = $this->get('fos_user.util.token_generator');
            $user->setConfirmationToken($tokenGenerator->generateToken());
        }

        $this->get('fos_user.mailer')->sendResettingEmailMessage($user);
        $user->setPasswordRequestedAt(new \DateTime());
        $this->get('fos_user.user_manager')->updateUser($user);

        return new Response(Response::HTTP_OK);
    }

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

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