Symfony 3通过用户名/电子邮件重置密码

时间:2018-11-04 15:24:06

标签: symfony token reset-password

我正在尝试根据电子邮件的值创建一个密码重置表单,但是如果我输入电子邮件并单击“发送”,我的页面只会刷新,而不是将电子邮件发送到在表单字段中输入的地址,因此用户检索密码更改链接。 我希望将一封带有链接的电子邮件发送到上述地址,以允许用户更改密码。

控制器

 public function requestPassAction(Request $request)
{

       $tmpUser = new User();

        $em = $this->getDoctrine()->getManager();
   $form = $this->createForm('Doctix\UserBundle\Form\ForgottenUserType', $tmpUser);
    $form->handleRequest($request);


    if ($form->isSubmitted() && $form->isValid()) { 



        $user = $em->getRepository('DoctixUserBundle:User')->findOneBy(['username'=>$tmpUser->getUsername()]);
    if($user)
        {
          if ($user->getToken() === null  ) {
                /** @var $tokenGenerator TokenGeneratorInterface */
                $token = md5(uniqid($user->getUsername(), true)); //some unique token (you can create a nicer token generator in standalone class with a service)
                $user->setToken($token);
                $user->setPasswordRequestedAt(new \DateTime());
                $em->persist($user);
                $em->flush();

                $this->addFlash('Info', 'Si l utilisateur est trouvé, vous allez recevoir un mail avec des instructions.');
    $mailer = $this->get('mailer');
                $message = (new \Swift_Message('Renouvellement du mot de passe'))
                        ->setFrom("medmamtest@gmail.com")
                        ->setTo($user->getUsername())
                        ->setBody(
                                $this->renderView(
                // app/Resources/views/Emails/registration.html.twig
                                    'Resetting/request.html.twig',
                                    array('user' => $user)
            ),
            'text/html'
        );

                $mailer->send($message);
                $request->getSession()->getFlashBag()->add('success', "Un mail va vous être envoyé afin que vous puissiez renouveller votre mot de passe. Le lien que vous recevrez sera valide 24h.");

                return $this->redirectToRoute("login");
           } 
       }

       else {
              return $this->redirectToRoute("request_resetting") ;
       } 
    }
    return $this->render('Resetting/request.html.twig', array(
        'form' => $form->createView()
    ));

}

查看

   {% extends "DoctixFrontBundle::layout.html.twig" %}
  {% block body %}

<div class="bg_color_2">
    <div class="container margin_60_35">

            <div class="row">

            <div id="login">


  <h1>Mot de passe oublié ?</h1>
  <p>Réinitialiser votre mot de passe en renseignant votre mail.</p>

{{form_start(form)}}
 {{form_widget(form.username)}}
 {{ form_errors(form.username) }}
{{form_end(form)}}


        </div>
        </div>

 </div>
 </div>

 {% endblock %}

Swiftmailer配置

  # Swiftmailer Configuration
swiftmailer:
   transport: "%mailer_transport%"
   auth_mode: "%mailer_auth_mode%"
   host:      "%mailer_host%"
  encryption: "%mailer_encryption%"
  port:      "%mailer_port%"
  username:  "%mailer_user%"
password:  "%mailer_password%"
spool:     { type: memory }

谢谢!

0 个答案:

没有答案