src/Controller/ForgotPasswordController.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\Type\ForgotPasswordType;
  5. use App\Service\EmailService;
  6. use App\Service\MessageService;
  7. use Exception;
  8. use Swift_Mailer;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  15. class ForgotPasswordController extends AbstractController
  16. {
  17.     /**
  18.      * @Route("/forgot-password", name="forgot_password")
  19.      * @param Request $request
  20.      * @param UserPasswordEncoderInterface $passwordEncoder
  21.      * @param MessageService $messageService
  22.      * @param EmailService $emailService
  23.      * @return RedirectResponse|Response
  24.      * @throws Exception
  25.      */
  26.     public function forgotPassword(Request $requestUserPasswordEncoderInterface $passwordEncoderMessageService $messageServiceEmailService $emailService)
  27.     {
  28.         $entityManager $this->getDoctrine()->getManager();
  29.         $form $this->createForm(ForgotPasswordType::class, new User());
  30.         $form->handleRequest($request);
  31.         if ($form->isSubmitted() && $form->isValid()) {
  32.             $user_tmp $form->getData();
  33.             // load user by email
  34.             $user $entityManager->getRepository(User::class)->findOneBy(array(
  35.                 'email' => $user_tmp->getEmail()
  36.             ));
  37.             if (!$user) {
  38.                 // if no user with the given email address was found exit here
  39.                 $messageService->setMessage('Something went wrong. Please try again.''danger');
  40.                 return $this->redirectToRoute('forgot_password');
  41.             } else {
  42.                 // generate random password
  43.                 $random_password $this->generateNewPassword();
  44.                 // save new password to user
  45.                 $user->setPassword($passwordEncoder->encodePassword($user$random_password));
  46.                 $user->setModified(new \DateTime());
  47.                 $entityManager->persist($user);
  48.                 $entityManager->flush();
  49.                 // send message with new password
  50.                 $emailService->setSubject('METRO Menu Engineering: Your new password');
  51.                 $emailService->setTo($user->getEmail());
  52.                 $emailService->setBody($this->renderView(EmailService::EMAIL_TEMPLATE_NEW_PASSWORD, [
  53.                     "password" => $random_password,
  54.                     "login_link" => $_SERVER["HTTP_ORIGIN"]. "/login"
  55.                 ]));
  56.                 $emailService->send();
  57.                 $messageService->setMessage('Your password was renewed. Please check your emails.');
  58.                 return $this->redirectToRoute('login');
  59.             }
  60.         }
  61.         return $this->render('forgot.password.html.twig', [
  62.             'form' => $form->createView(),
  63.         ]);
  64.     }
  65.     /**
  66.      * Generates and returns a random password
  67.      * @return false|string
  68.      */
  69.     private function generateNewPassword()
  70.     {
  71.         $letters "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  72.         $random_password "";
  73.         for ($i 0$i <= 12$i++) {
  74.             $random_position random_int(0strlen($letters) - 1);
  75.             $random_password .= substr($letters$random_position1);
  76.         }
  77.         return $random_password;
  78.     }
  79. }