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.         
  77.         $link $this->generateUrl('reset_password_activate', array('token' => $token), UrlGeneratorInterface::ABSOLUTE_URL); //changedFrom true 
  78.         // finde neuestes Template -> TODO
  79.         $template $this->em->getRepository(MailTemplate::class)->findCurrentByTyp(Email::RESETTING);
  80.         $replacements = array(
  81.             'vorname' => $kunde->getVorname(),
  82.             'nachname' => $kunde->getNachname(),
  83.             'email' => $kunde->getEmail(),
  84.             'handynummer' => $kunde->getHandynummer(),
  85.             'link' => $link
  86.         );
  87.         
  88.         $this->logger->error(sprintf('Linka: %s '$link));
  89.         
  90.         $mailerBcc $this->getParameter('mailer_bcc');
  91.         $mailUser     $this->getParameter('mailer_user');
  92.        
  93.         $message = (new Swift_Message($template->getBetreff()))
  94.                 ->setFrom(array($mailUser => 'Juridicus'))
  95.                 ->setReplyTo('info@juridicus.de')
  96.                 ->setTo($kunde->getEmail())
  97.                 ->setBody($template->replace($replacements), 'text/html')
  98.         ;
  99.         if (isset($mailerBcc)) {
  100.             $message->setBcc(array($mailerBcc));
  101.         }
  102.         $failedRecipients = [];
  103.         $result $this->mailer->send($message$failedRecipients);
  104.         $this->logger->error('Mail debug', [
  105.             'to' => $kunde->getEmail(),
  106.             'from' => $mailUser,
  107.             'subject' => $template->getBetreff(),
  108.             'result' => $result,
  109.             'failedRecipients' => $failedRecipients,
  110.         ]);
  111.         if ( $result) {
  112.             // Versand speichern
  113.             $email = new Email();
  114.             $email
  115.                     ->setTyp(Email::RESETTING)
  116.                     ->setKunde($kunde)
  117.             ;
  118.             $this->em->persist($email);
  119.             $this->container->get('session')->set('reset_password_email_success'$kunde->getEmail());
  120.             return $this->redirect($this->generateUrl('reset_password_email_success'));
  121.         } else {
  122.             $this->container->get('session')->set('reset_password_email_success'$kunde->getEmail());
  123.             return $this->redirect($this->generateUrl('reset_password_email_error'));
  124.         }
  125.     }
  126.     /**
  127.      * Reset user password and send sms
  128.      *
  129.      * @Route("/activate/{token}", name="reset_password_activate")
  130.      * @param string $token
  131.      * @return \Symfony\Component\HttpFoundation\Response
  132.      * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  133.      */
  134.     public function resetAction($token)
  135.     {
  136.        
  137.         /* @var $session Symfony\Component\HttpFoundation\Session */
  138.         $session $this->container->get('session');
  139.         $user $this->em->getRepository(User::class)->findOneBy(['confirmationToken' => $token]);
  140.         if (null === $user) {
  141.             throw $this->createNotFoundException('Der Resetcode existiert nicht.');
  142.         }
  143.         $kunde $user->getKunde();
  144.         if (null === $kunde) {
  145.             throw $this->createNotFoundException('Der Benutzer zum Resetcode existiert nicht.');
  146.         }
  147.         if (!$user->isPasswordRequestNonExpired($this->tokenTtl)) {
  148.             return $this->redirect($this->generateUrl('password_reset_request'));
  149.         }
  150.         $password_plain substr(uniqid(md5(rand())), 88);
  151.         $hashedPassword $this->passwordHasher->hashPassword(
  152.             $user,
  153.             $password_plain
  154.         );
  155.         
  156.         $this->logger->error(sprintf('Password: %s '$password_plain));
  157.         $user->setConfirmationToken(null);
  158.         $user->setPlainPassword($password_plain);
  159.         $user->setPassword($hashedPassword);
  160.         $user->setPasswordRequestedAt(null);
  161.         $user->setEnabled(true);
  162.         $this->em->persist($user);
  163.         $this->em->flush();
  164.         // Senden des Passwords per SMS
  165.         $template $this->em->getRepository(MailTemplate::class)->findCurrentByTyp(Email::SMS_RESETTING);
  166.         $replacements = array(
  167.             'vorname' => $kunde->getVorname(),
  168.             'nachname' => $kunde->getNachname(),
  169.             'email' => $kunde->getEmail(),
  170.             'handynummer' => $kunde->getHandynummer(),
  171.             'password' => $password_plain
  172.         );
  173.         if ($this->smsService->send(
  174.                         html_entity_decode(strip_tags($template->replace($replacements))), $kunde->getHandynummer())
  175.         )
  176.         {
  177.             $kunde->setSmsSendAt(new \DateTime());
  178.             $this->em->persist($kunde);
  179.             $this->em->flush();
  180.             $session->set('reset_password_sms_success'$kunde->getEmail());
  181.             return $this->redirect($this->generateUrl('reset_password_sms_success'));
  182.         } else {
  183.             $session->set('reset_password_sms_error'$kunde->getEmail());
  184.             return $this->redirect($this->generateUrl('reset_password_sms_error'));
  185.         }
  186.     }
  187.     /**
  188.      * Tell the user to check his email provider
  189.      *
  190.      * @Route("/email/success", name="reset_password_email_success")
  191.      * @Template("@JFJuridicusBundle/Resetting/email_success.html.twig")
  192.      */
  193.     public function emailSuccessAction()
  194.     {
  195.         $session $this->container->get('session');
  196.         $email $session->get('reset_password_email_success');
  197.         $session->remove('reset_password_email_success');
  198.         if (empty($email)) {
  199.             // the user does not come from the sendEmail action
  200.             return $this->redirect($this->generateUrl('reset_password_request'));
  201.         }
  202.         return array(
  203.             'kunde' => $this->em->getRepository(User::class)->findOneBy(['email' => $email])->getKunde()
  204.         );
  205.     }
  206.     /**
  207.      * Tell the user that email sending failed
  208.      *
  209.      * @Route("/email/error", name="reset_password_email_error")
  210.      * @Template("@JFJuridicusBundle/Resetting/email_error.html.twig")
  211.      */
  212.     public function emailErrorAction()
  213.     {
  214.         $session $this->container->get('session');
  215.         $email $session->get('reset_password_email_error');
  216.         $session->remove('reset_password_email_error');
  217.         if (empty($email)) {
  218.             // the user does not come from the sendEmail action
  219.             return $this->redirect($this->generateUrl('reset_password_request'));
  220.         }
  221.         return array(
  222.             'kunde' => $this->em->getRepository(User::class)->findOneBy(['email' => $email])->getKunde()
  223.         );
  224.     }
  225.     /**
  226.      * Tell the user to check his sms
  227.      *
  228.      * @Route("/sms/success", name="reset_password_sms_success")
  229.      * @Template("@JFJuridicusBundle/Resetting/sms_success.html.twig")
  230.      */
  231.     public function smsSuccessAction()
  232.     {
  233.         $session $this->container->get('session');
  234.         $email $session->get('reset_password_sms_success');
  235.         $session->remove('reset_password_sms_success');
  236.         if (empty($email)) {
  237.             // the user does not come from the sendEmail action
  238.             return $this->redirect($this->generateUrl('reset_password_request'));
  239.         }
  240.         return array(
  241.             'kunde' => $this->em->getRepository(User::class)->findOneBy(['email' => $email])->getKunde()
  242.         );
  243.     }
  244.     /**
  245.      * Tell the user that sms sending failed
  246.      *
  247.      * @Route("/sms/error", name="reset_password_sms_error")
  248.      * @Template("@JFJuridicusBundle/Resetting/sms_error.html.twig")
  249.      */
  250.     public function smsErrorAction()
  251.     {
  252.         $session $this->container->get('session');
  253.         $email $session->get('reset_password_sms_error');
  254.         $session->remove('reset_password_sms_error');
  255.         if (empty($email)) {
  256.             // the user does not come from the sendSms action
  257.             return $this->redirect($this->generateUrl('reset_password_request'));
  258.         }
  259.         return array(
  260.             'kunde' => $this->em->getRepository(User::class)->findOneBy(['email' => $email])->getKunde()
  261.         );
  262.     }
  263. }