src/Controller/PassResetController.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  8. class PassResetController extends AbstractController {
  9.     
  10.     /**
  11.      * Zasób renderuje stronę rozpoczynającą procedurę resetu hasła
  12.      * @Route("/pass/reset/1", name="get_pass_reset_stage_1", methods={"GET"})
  13.      * @return Response
  14.      */
  15.     public function passReset1 (): Response {
  16.         return $this->render('pass_reset_stage1.html.twig');
  17.     }
  18.     
  19.     /**
  20.      * Zasób generuje maila otwierającego procedurę resetu hasło i wyświetla stosowną stronę z informacją o mailu lub strone z informacją o nieznanym adresie e-mail
  21.      * @Route("/pass/reset/1", name="post_pass_reset_stage_1", methods={"POST"})
  22.      * @param \Swift_Mailer $mailer
  23.      * @param \App\Service\Cezar $cezar
  24.      * @param \Symfony\Component\HttpFoundation\Request $zadanie
  25.      * @param \Doctrine\ORM\EntityManagerInterface $em
  26.      * @return Response
  27.      */
  28.     public function passreset2 (\Swift_Mailer $mailer, \App\Service\Cezar $cezar, \Symfony\Component\HttpFoundation\Request $zadanie, \Doctrine\ORM\EntityManagerInterface $em): Response {
  29.         $termin = new \DateTime (date ("Y-m-d H:i:s"time() + 86400 7));
  30.         $user $em->getRepository(\App\Entity\User::class)->findOneBy (["login" => $zadanie->get ("login""")]);
  31.         if ($user) {
  32.             $emailMessage = new \Swift_Message("InforPol NET - reset hasła.");
  33.             $emailMessage->setFrom ($em->getRepository(\App\Entity\Conf::class)->peek ("email-sender-return-address""info@ndc.pl"), $em->getRepository(\App\Entity\Conf::class)->peek ("email-sender-name""INFORPOL NET"))
  34.                 ->setTo ($user->getEmail (), $user->getNazwa ())                
  35.                 ->setBody (
  36.                     $this->renderView("email/reset_hasla.html.twig", ["user" => $user"termin" => $termin"key" => $cezar->match(serialize([ "user" => $user->getId (), "termin" => date ("Y-m-d H:i:s"$termin->getTimestamp()) ]))]),
  37.                     'text/html'
  38.                 );
  39.             $mailer->send($emailMessage);
  40.             $email $user->getEmail();
  41.             $cnt round (strlen ($email) * 0.3);
  42.             if ($cnt 1$cnt 1;
  43.             $email str_replace ("@"""substr ($email0$cnt)) . "**@****" str_replace ("@"""substr ($emailstrlen ($email) - $cnt));
  44.             return $this->render('pass_reset_stage2.html.twig', ["email" => $email"termin" => $termin]);
  45.         } else {
  46.             $email = [
  47.                 "biuro@domena.pl",
  48.                 "info@domena.pl",
  49.                 "poczta@domena.pl",
  50.                 "sklep@domena.pl",
  51.                 "biuro@domena.waw.pl",
  52.                 "info@domena.waw.pl",
  53.                 "poczta@domena.waw.pl",
  54.                 "sklep@domena.waw.pl",
  55.                 "biuro@domena.com.pl",
  56.                 "info@domena.com.pl",
  57.                 "poczta@domena.com.pl",
  58.                 "sklep@domena.com.pl",
  59.                 "biuro@domena.eu",
  60.                 "info@domena.eu",
  61.                 "poczta@domena.eu",
  62.                 "sklep@domena.eu"
  63.             ][rand(015)];
  64.             $cnt round (strlen ($email) * 0.3);
  65.             if ($cnt 1$cnt 1;
  66.             $email str_replace ("@"""substr ($email0$cnt)) . "**@****" str_replace ("@"""substr ($emailstrlen ($email) - $cnt));
  67.             return $this->render('pass_reset_no-mail.html.twig', ["email" => $email"termin" => $termin]);
  68.         }
  69.     }
  70.     
  71.     /**
  72.      * Funkcja weryfikuje link i wyświetla stronę z formularzem restu hasła, bądź informacje o nieaktywności łącza
  73.      * @Route("/pass/reset/2/{key}", name="get_pass_reset_stage_2", methods={"GET"})
  74.      * @param \App\Service\Cezar $cezar
  75.      * @param \Doctrine\ORM\EntityManagerInterface $em
  76.      * @param string $key
  77.      * @return Response
  78.      */
  79.     public function passreset3 (\App\Service\Cezar $cezar, \Doctrine\ORM\EntityManagerInterface $emstring $key): Response {
  80.         $error false;
  81.         try {
  82.             $key unserialize ($cezar->demath($key));
  83.         } catch (\Exception $e) {
  84.             $error true;
  85.         }
  86.         if ($error) {
  87.             return $this->render("pass_reset_error_link.html.twig");
  88.         }
  89.         if (strtotime ($key ['termin']) < time ()) {
  90.             return $this->render("pass_reset_link.out.off.date.html.twig");
  91.         } else {
  92.             $user $em->getRepository (\App\Entity\User::class)->find ($key ['user']);
  93.             if ($user === NULL) {
  94.                 return $this->render("pass_reset_link.out.off.date.html.twig");
  95.             } else {
  96.                 return $this->render ("pass_reset_reset.html.twig", [
  97.                     "user" => $user,
  98.                     "min" => intval ($em->getRepository (\App\Entity\Conf::class)->peek ("pass-min-len"0)),
  99.                     "max" => intval ($em->getRepository (\App\Entity\Conf::class)->peek ("pass-max-len"0)),
  100.                     "min_duzych" => intval ($em->getRepository (\App\Entity\Conf::class)->peek ("pass-min-upper"0)),
  101.                     "min_malych" => intval ($em->getRepository (\App\Entity\Conf::class)->peek ("pass-min-lower"0)),
  102.                     "min_cyfr" => intval ($em->getRepository (\App\Entity\Conf::class)->peek ("pass-min-digit"0)),
  103.                     "min_symboli" => intval ($em->getRepository (\App\Entity\Conf::class)->peek ("pass-min-symbols"0)),
  104.                     "symbole" => $em->getRepository (\App\Entity\Conf::class)->peek ("pass-symbols""") == "" "`~!@#$%^&*()_-=+[]{};'\\:\"|,./<>?" $em->getRepository (\App\Entity\Conf::class)->peek ("pass-min-symbols"""),
  105.                 ]);
  106.             }
  107.         }
  108.     }
  109.     
  110.     /**
  111.      * Funkcja weryfikuje link oraz formularz i wyświetla stronę z formularzem lub z informacją o zmianie hasła, bądź informacje o nieaktywności łącza lub błędach w nowym haśle
  112.      * @Route("/pass/reset/2/{key}", name="post_pass_reset_stage_2", methods={"POST"})
  113.      * @param \Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface $PHI
  114.      * @param \App\Service\PassValidator $pv
  115.      * @param \App\Service\Cezar $cezar
  116.      * @param \Symfony\Component\HttpFoundation\Request $zadanie
  117.      * @param \Doctrine\ORM\EntityManagerInterface $em
  118.      * @param string $key
  119.      * @return Response
  120.      */
  121.     public function passreset4 (\Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface $PHI, \App\Service\PassValidator $pv, \App\Service\Cezar $cezar, \Symfony\Component\HttpFoundation\Request $zadanie, \Doctrine\ORM\EntityManagerInterface $emstring $key): Response {
  122.         $error false;
  123.         try {
  124.             $key unserialize ($cezar->demath($key));
  125.         } catch (\Exception $e) {
  126.             $error true;
  127.         }
  128.         if ($error) {
  129.             return $this->render("pass_reset_error_link.html.twig");
  130.         }
  131.         if (strtotime ($key ['termin']) < time ()) {
  132.             return $this->render("pass_reset_link.out.off.date.html.twig");
  133.         } else {
  134.             $user $em->getRepository (\App\Entity\User::class)->find ($key ['user']);
  135.             if ($user === NULL) {
  136.                 return $this->render("pass_reset_link.out.off.date.html.twig");
  137.             } else {
  138.                 $errors = [];
  139.                 if ($zadanie->get ("pass1""") == "") {
  140.                     $errors[] = [ "message" => "Hasło nie może być puste." ];
  141.                 } else {
  142.                     if ($zadanie->get ("pass1""") != $zadanie->get ("pass2""")) {
  143.                         $errors[] = [ "message" => "Podane hasła są różne." ];
  144.                     } else {
  145.                         $test $pv->isValid($em$zadanie->get ("pass1"));
  146.                         if ($test === true) {
  147.                             
  148.                         } else {
  149.                             $errors[] = [ "message" => nl2br ($test) ];
  150.                         }
  151.                     }
  152.                 }                
  153.                 if (count ($errors) > 0) {
  154.                     return $this->render ("pass_reset_reset.html.twig", [
  155.                         "user" => $user,
  156.                         "min" => intval ($em->getRepository (\App\Entity\Conf::class)->peek ("pass-min-len"0)),
  157.                         "max" => intval ($em->getRepository (\App\Entity\Conf::class)->peek ("pass-max-len"0)),
  158.                         "min_duzych" => intval ($em->getRepository (\App\Entity\Conf::class)->peek ("pass-min-upper"0)),
  159.                         "min_malych" => intval ($em->getRepository (\App\Entity\Conf::class)->peek ("pass-min-lower"0)),
  160.                         "min_cyfr" => intval ($em->getRepository (\App\Entity\Conf::class)->peek ("pass-min-digit"0)),
  161.                         "min_symboli" => intval ($em->getRepository (\App\Entity\Conf::class)->peek ("pass-min-symbols"0)),
  162.                         "symbole" => $em->getRepository (\App\Entity\Conf::class)->peek ("pass-symbols""") == "" "`~!@#$%^&*()_-=+[]{};'\\:\"|,./<>?" $em->getRepository (\App\Entity\Conf::class)->peek ("pass-min-symbols"""),
  163.                         "alerts" => $errors,
  164.                         "pass" => $zadanie->get("pass1")
  165.                     ]);
  166.                 } else {                    
  167.                     $user->setPassword($PHI->hashPassword ($user$zadanie->get ("pass1")));
  168.                     $em->persist($user);
  169.                     $em->flush();
  170.                     return $this->render("pass_reset_setting-ok.html.twig", [ "user" => $user ]);
  171.                 }
  172.             }
  173.         }
  174.     }
  175.     
  176. }