src/Controller/AccountRecoveryController.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Datetime;
  4. use App\Entity\User;
  5. use App\Entity\UserRecovery;
  6. use App\Form\Type\ForgotPasswordType;
  7. use App\Form\Type\ChangePasswordType;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  13. use Symfony\Component\Mailer\MailerInterface;
  14. use Symfony\Component\Mime\Email;
  15. class AccountRecoveryController extends AbstractController
  16. {
  17.     protected $recoveryHash 'SadkÅÄÖajsh213asdokz';
  18.     /**
  19.      * @Route("/forgot-password", name="app_forgot_password")
  20.      */
  21.     public function forgot(Request $requestMailerInterface $mailer) {
  22.         $form $this->createForm(ForgotPasswordType::class);
  23.         $form->handleRequest($request);
  24.         if ($form->isSubmitted() && $form->isValid()) {
  25.             $data $form->getData();
  26.             $em $this->getDoctrine()->getManager();
  27.             $user $em->getRepository('App\Entity\User')->findOneByEmail($data['email']);
  28.             if($user) {
  29.                 $recovery = new UserRecovery();
  30.                 $recovery->setUser($user);
  31.                 $recovery->setRecovery(md5($this->recoveryHash $user->getId()));
  32.                 $recovery->setCreated(new Datetime());
  33.                 $em->persist($recovery);
  34.                 $em->flush();
  35.                 /* $message = (new \Swift_Message('Återställ lösenord'))
  36.                     ->setFrom('info@sebohushalmstad.se', 'Sebohushalmstad.se')
  37.                     ->setTo($user->getEmail())
  38.                     ->setBody(
  39.                         // templates/emails/account-recovery.html.twig
  40.                         $this->renderView('emails/account-recovery.html.twig', [
  41.                             'user' => $user,
  42.                             'recovery' => $recovery
  43.                         ]),
  44.                         'text/html'
  45.                     ); */
  46.                 $message = (new Email())
  47.                     ->from('info@sebohushalmstad.se')
  48.                     ->to($user->getEmail())
  49.                     ->subject('Återställ lösenord')
  50.                     ->text('Återställ lösenord')
  51.                     ->html(
  52.                         // templates/emails/account-recovery.html.twig
  53.                         $this->renderView('emails/account-recovery.html.twig', [
  54.                             'user' => $user,
  55.                             'recovery' => $recovery
  56.                         ]),
  57.                     );
  58.                 $mailer->send($message);
  59.                 $this->addFlash('success''En länk för att återställa ditt lösenord har skickats till din e-postadress');
  60.             } else {
  61.                 // User not found
  62.             }
  63.             return $this->redirectToRoute('app_forgot_password');
  64.         }
  65.         return $this->render('forgot-password.html.twig', [
  66.             'form' => $form->createView()
  67.         ]);
  68.     }
  69.     /**
  70.      * @Route("/account-recovery/{hash}", name="app_account_recovery")
  71.      */
  72.     public function recovery($hashRequest $requestUserPasswordEncoderInterface $passwordEncoder): Response
  73.     {
  74.         $em $this->getDoctrine()->getManager();
  75.         $userRecovery $em->getRepository('App\Entity\UserRecovery')->findOneByRecovery($hash, [
  76.             'created' => 'DESC'
  77.         ]);
  78.         $user $userRecovery->getUser();
  79.         $form $this->createForm(ChangePasswordType::class, $user);
  80.         $form->handleRequest($request);
  81.         if ($form->isSubmitted() && $form->isValid()) {
  82.             // encode the plain password
  83.             $user->setPassword(
  84.                 $passwordEncoder->encodePassword(
  85.                     $user,
  86.                     $form->get('plainPassword')->getData()
  87.                 )
  88.             );
  89.             $em->flush();
  90.             $this->addFlash('success''Ditt lösenord är nu uppdaterat');
  91.             return $this->redirectToRoute('app_login');
  92.         }
  93.         return $this->render('account-recovery.html.twig', [
  94.             'userRecovery' => $userRecovery,
  95.             'form' => $form->createView()
  96.         ]);
  97.     }
  98. }