src/JuridicusBundle/Controller/ResettingPrueferUserController.php line 48

Open in your IDE?
  1. <?php
  2. namespace JF\JuridicusBundle\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  10. use JF\JuridicusBundle\Entity\PrueferEmail;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  13. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  14. use JF\JuridicusBundle\Entity\PrueferUser;
  15. use JF\JuridicusBundle\Entity\User;
  16. use JF\JuridicusBundle\Entity\MailTemplate;
  17. use Swift_Mailer;
  18. use Swift_Message;
  19. use Psr\Log\LoggerInterface;
  20. /**
  21. * Controller managing the resetting of the password
  22. *
  23. * @Route("/pruefer/reset_password")
  24. */
  25. class ResettingPrueferUserController extends AbstractController
  26. {
  27. private int $tokenTtl = 86400;
  28. public function __construct(
  29. private EntityManagerInterface $em,
  30. private UserPasswordHasherInterface $passwordHasher,
  31. private Swift_Mailer $mailer,
  32. private LoggerInterface $logger ) {
  33. }
  34. /**
  35. * Request reset user password: show form
  36. *
  37. * @Route("/request", name="pruefer_reset_password_request")
  38. * @Template("@JFJuridicusBundle/PrueferBereich/request.html.twig")
  39. */
  40. public function requestAction()
  41. {
  42. return array();
  43. }
  44. /**
  45. * Request reset user password: submit form and send email
  46. *
  47. * @Route("/send_email", name="pruefer_reset_password_send_email")
  48. * @Method({"POST"})
  49. * @Template("@JFJuridicusBundle/PrueferBereich/request.html.twig")
  50. */
  51. public function sendEmailAction(Request $request)
  52. {
  53. $username = $request->request->get('username');
  54. $user = $this->em->getRepository(User::class)->findOneBy(['username' => $username]);
  55. if (null === $user) {
  56. // Back To Form
  57. return array('invalid_username' => $username);
  58. }
  59. $prueferUser = $this->em->getRepository(PrueferUser::class)->findOneByEmail($username);
  60. if (null === $prueferUser) {
  61. // Back To Form
  62. return array('invalid_username' => $username);
  63. }
  64. if ($user->isPasswordRequestNonExpired($this->tokenTtl)) {
  65. return $this->render('@JFJuridicusBundle/PrueferBereich/password_already_requested.html.twig');
  66. }
  67. $token = $user->getConfirmationToken();
  68. if (null === $token) {
  69. $token = uniqid(md5(rand()));
  70. $user->setConfirmationToken($token);
  71. }
  72. $user->setPasswordRequestedAt(new \DateTimeImmutable());
  73. $this->em->persist($user);
  74. $this->em->flush();
  75. $link = $this->generateUrl('pruefer_reset_password_activate', array('token' => $token), UrlGeneratorInterface::ABSOLUTE_URL); //chagngedFrom true
  76. // finde neuestes Template -> TODO
  77. $template = $this->em->getRepository(MailTemplate::class)->findCurrentByTyp(PrueferEmail::PRUEFER_BEREICH_PASSWORT_RESET);
  78. $replacements = array(
  79. 'vorname' => $prueferUser->getVorname(),
  80. 'nachname' => $prueferUser->getNachname(),
  81. 'email' => $prueferUser->getEmail(),
  82. //'handynummer' => $prueferUser->getHandynummer(),
  83. 'link' => $link
  84. );
  85. $this->logger->info(sprintf('Link: %s ', $link));
  86. $mailerBcc = $this->getParameter('mailer_bcc');
  87. $mailUser = $this->getParameter('mailer_user');
  88. $message = (new Swift_Message($template->getBetreff()))
  89. ->setFrom(array($mailUser => 'Juridicus'))
  90. ->setReplyTo('info@juridicus.de')
  91. ->setTo($prueferUser->getEmail())
  92. ->setBcc('info@juridicus.de')
  93. ->setBody($template->replace($replacements), 'text/html')
  94. ;
  95. if ($this->mailer->send($message)) {
  96. // Versand speichern
  97. $this->email = new PrueferEmail();
  98. $this->email
  99. ->setTyp(PrueferEmail::PRUEFER_BEREICH_PASSWORT_RESET)
  100. ->setPrueferUser($prueferUser)
  101. ;
  102. $this->em->persist($this->email);
  103. $this->container->get('session')->set('pruefer_reset_password_email_success', $prueferUser->getEmail());
  104. return $this->redirect($this->generateUrl('pruefer_reset_password_email_success'));
  105. } else {
  106. $this->container->get('session')->set('pruefer_reset_password_email_success', $prueferUser->getEmail());
  107. return $this->redirect($this->generateUrl('pruefer_reset_password_email_error'));
  108. }
  109. }
  110. /**
  111. * Reset user password and send sms
  112. *
  113. * @Route("/activate/{token}", name="pruefer_reset_password_activate")
  114. * @param string $token
  115. * @return \Symfony\Component\HttpFoundation\Response
  116. * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  117. */
  118. public function resetAction($token)
  119. {
  120. //$sms_service = $this->container->get('jf_juridicus.sms');
  121. /* @var $session Symfony\Component\HttpFoundation\Session */
  122. $session = $this->container->get('session');
  123. $user = $this->em->getRepository(User::class)->findOneBy(['confirmationToken' => $token]);
  124. if (null === $user) {
  125. throw $this->createNotFoundException('Der Resetcode existiert nicht.');
  126. }
  127. $prueferUser = $this->em->getRepository(PrueferUser::class)->findOneByUser($user);
  128. if (null === $prueferUser) {
  129. throw $this->createNotFoundException('Der Benutzer zum Resetcode existiert nicht.');
  130. }
  131. if (!$user->isPasswordRequestNonExpired($this->tokenTtl)) {
  132. return $this->redirect($this->generateUrl('pruefer_reset_password_request'));
  133. }
  134. $password_plain = substr(uniqid(md5(rand())), 8, 8);
  135. $hashedPassword = $this->passwordHasher->hashPassword(
  136. $user,
  137. $password_plain
  138. );
  139. $user->setConfirmationToken(null);
  140. $user->setPlainPassword($password_plain);
  141. $user->setPassword($hashedPassword);
  142. $user->setPasswordRequestedAt(null);
  143. $user->setEnabled(true);
  144. $this->em->persist($user);
  145. $this->em->flush();
  146. $this->logger->error(sprintf('Password: %s ', $password_plain));
  147. $this->em = $this->getDoctrine()->getManager();
  148. // Senden des Passwords per SMS
  149. $template = $this->em->getRepository(MailTemplate::class)->findCurrentByTyp(PrueferEmail::PRUEFER_BEREICH_PASSWORT_RESET_PASSWORT);
  150. $replacements = array(
  151. 'vorname' => $prueferUser->getVorname(),
  152. 'nachname' => $prueferUser->getNachname(),
  153. 'email' => $prueferUser->getEmail(),
  154. //'handynummer' => $prueferUser->getHandynummer(),
  155. 'password' => $password_plain
  156. );
  157. /* NEW send E-Mail with PW */
  158. $mailerBcc = $this->getParameter('mailer_bcc');
  159. $mailUser = $this->getParameter('mailer_user');
  160. $message = (new Swift_Message($template->getBetreff()))
  161. ->setFrom(array($mailUser => 'Juridicus'))
  162. ->setReplyTo('info@juridicus.de')
  163. ->setTo($prueferUser->getEmail())
  164. ->setBcc('info@juridicus.de')
  165. ->setBody($template->replace($replacements), 'text/html')
  166. ;
  167. if ($this->mailer->send($message)) {
  168. // Versand speichern
  169. $this->email = new PrueferEmail();
  170. $this->email
  171. ->setTyp(PrueferEmail::PRUEFER_BEREICH_PASSWORT_RESET_PASSWORT)
  172. ->setPrueferUser($prueferUser)
  173. ;
  174. $this->em->persist($this->email);
  175. $this->em->flush();
  176. $session->set('pruefer_reset_password_sms_success', $prueferUser->getEmail());
  177. return $this->redirect($this->generateUrl('pruefer_reset_password_sms_success'));
  178. } else {
  179. $session->set('pruefer_reset_password_sms_error', $prueferUser->getEmail());
  180. return $this->redirect($this->generateUrl('pruefer_reset_password_sms_error'));
  181. }
  182. }
  183. /**
  184. * Tell the user to check his email provider
  185. *
  186. * @Route("/email/success", name="pruefer_reset_password_email_success")
  187. * @Template("@JFJuridicusBundle/PrueferBereich/resetting_email_success.html.twig")
  188. */
  189. public function emailSuccessAction()
  190. {
  191. $session = $this->container->get('session');
  192. $this->email = $session->get('pruefer_reset_password_email_success');
  193. $session->remove('pruefer_reset_password_email_success');
  194. if (empty($this->email)) {
  195. // the user does not come from the sendEmail action
  196. return $this->redirect($this->generateUrl('pruefer_reset_password_request'));
  197. }
  198. return array(
  199. 'email' => $this->email
  200. );
  201. }
  202. /**
  203. * Tell the user that email sending failed
  204. *
  205. * @Route("/email/error", name="pruefer_reset_password_email_error")
  206. * @Template("@JFJuridicusBundle/PrueferBereich/resetting_email_error.html.twig")
  207. */
  208. public function emailErrorAction()
  209. {
  210. $session = $this->container->get('session');
  211. $this->email = $session->get('pruefer_reset_password_email_error');
  212. $session->remove('pruefer_reset_password_email_error');
  213. if (empty($this->email)) {
  214. // the user does not come from the sendEmail action
  215. return $this->redirect($this->generateUrl('pruefer_reset_password_request'));
  216. }
  217. return array(
  218. 'email' => $this->email
  219. );
  220. }
  221. /**
  222. * Tell the user to check his sms
  223. *
  224. * @Route("/sms/success", name="pruefer_reset_password_sms_success")
  225. * @Template("@JFJuridicusBundle/PrueferBereich/resetting_sms_success.html.twig")
  226. */
  227. public function smsSuccessAction()
  228. {
  229. $session = $this->container->get('session');
  230. $this->email = $session->get('pruefer_reset_password_sms_success');
  231. $session->remove('pruefer_reset_password_sms_success');
  232. if (empty($this->email)) {
  233. // the user does not come from the sendEmail action
  234. return $this->redirect($this->generateUrl('pruefer_reset_password_request'));
  235. }
  236. return array(
  237. 'email' => $this->email
  238. );
  239. }
  240. /**
  241. * Tell the user that sms sending failed
  242. *
  243. * @Route("/sms/error", name="pruefer_reset_password_sms_error")
  244. * @Template("@JFJuridicusBundle/PrueferBereich/resetting_sms_error.html.twig")
  245. */
  246. public function smsErrorAction()
  247. {
  248. $session = $this->container->get('session');
  249. $this->email = $session->get('pruefer_reset_password_sms_error');
  250. $session->remove('pruefer_reset_password_sms_error');
  251. if (empty($this->email)) {
  252. // the user does not come from the sendSms action
  253. return $this->redirect($this->generateUrl('pruefer_reset_password_request'));
  254. }
  255. return array(
  256. 'email' => $this->email
  257. );
  258. }
  259. }