src/JuridicusBundle/Controller/ResettingController.php line 46

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