src/Controller/ForgotPasswordController.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\HttpFoundation\Response;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Validator\Constraints\DateTime;
  10. use App\Utils\Constants;
  11. use App\Utils\Breadcrumbs;
  12. use App\Utils\Utils;
  13. use App\Entity\User;
  14. use App\Entity\BusinessArea;
  15. use App\Entity\Organisation;
  16. use Symfony\Component\Form\Extension\Core\Type\TextType;
  17. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  18. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  19. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  20. use Doctrine\ORM\EntityRepository;
  21. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  22. use Symfony\Component\Form\FormError;
  23. use App\Form\CompanyNameType;
  24. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  25. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  26. use App\Security\LoginFormAuthenticator;
  27. use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
  28. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  29. use Symfony\Component\Security\Core\Authentication\Token;
  30. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  31. use App\Service\CryptoService;
  32. class ForgotPasswordController extends AbstractController
  33. {
  34. /**
  35. * @Route("/forgot-password", name="forgot-password")
  36. */
  37. public function index(Request $request, UserPasswordEncoderInterface $encoder, LoginFormAuthenticator $authenticator, GuardAuthenticatorHandler $guardHandler, \Swift_Mailer $mailer, CryptoService $crypto)
  38. {
  39. $currentUser = new User();
  40. $form = $this->createFormBuilder($currentUser)->getForm();
  41. $form->add('email', EmailType::class, ['label' => '', 'data' => $currentUser->getEmail(), 'required' => true, 'attr' => array('class' => 'form-control') ]);
  42. $form->add('save', SubmitType::class, ['label' => 'Reset Password', 'attr' => array('class' => 'btn btn-success btn-md') ]);
  43. $em = $this->getDoctrine()->getManager();
  44. $form->handleRequest($request);
  45. if ($form->isSubmitted() and $form->isValid()) {
  46. $userRepo = $this->getDoctrine()->getRepository(User::class);
  47. $users = $userRepo->findBy(
  48. ['status' => Constants::ACTIVE],
  49. ['date_updated' => 'DESC']
  50. );
  51. $userFound = false;
  52. $thisUser = "";
  53. foreach($users as $user) {
  54. if($user->getEmail() == $currentUser->getEmail()) {
  55. $userFound = true;
  56. $thisUser = $user;
  57. break;
  58. }
  59. }
  60. if($userFound == false) {
  61. $this->addFlash('error','User with email '.$currentUser->getEmail().' does not exist or is not active in the system.');
  62. } else {
  63. if($thisUser != "") {
  64. $tempPassword = sha1(random_bytes(10));
  65. $encoded = $encoder->encodePassword($thisUser, $tempPassword);
  66. $thisUser->setPassword($encoded);
  67. $thisUser->setDateUpdated(\DateTime::createFromFormat('U', time()));
  68. $em->persist($thisUser);
  69. $em->flush();
  70. $this->addFlash('notice','Temporary Password reset for User '.$crypto->getCrypto()->Decrypt($thisUser->getEmail()));
  71. //Password reset email
  72. $result = Utils::sendEmail($mailer, $this, Constants::EMAIL_SUBJECT_PASSWORD_RESET, $tempPassword, $crypto->getCrypto()->Decrypt($thisUser->getEmail()), Constants::EMAIL_FROM_SYSTEM, Constants::EMAIL_TEMPLATE_FORGOT_PASSWORD_RESET);
  73. if($result == "1") {
  74. $this->addFlash('notice', 'Password reset email sent');
  75. } else {
  76. $this->addFlash('error','Error sending password reset email: '.$result);
  77. }
  78. } else {
  79. $this->addFlash('error','Error finding selected user, please contact info@medtechmor.ie');
  80. }
  81. return $this->redirectToRoute('forgot-password');
  82. }
  83. }
  84. return $this->render('security/forgot-password.html.twig', array(
  85. 'form' => $form->createView(),
  86. ));
  87. }
  88. }