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