<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
class PassResetController extends AbstractController {
/**
* Zasób renderuje stronę rozpoczynającą procedurę resetu hasła
* @Route("/pass/reset/1", name="get_pass_reset_stage_1", methods={"GET"})
* @return Response
*/
public function passReset1 (): Response {
return $this->render('pass_reset_stage1.html.twig');
}
/**
* 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
* @Route("/pass/reset/1", name="post_pass_reset_stage_1", methods={"POST"})
* @param \Swift_Mailer $mailer
* @param \App\Service\Cezar $cezar
* @param \Symfony\Component\HttpFoundation\Request $zadanie
* @param \Doctrine\ORM\EntityManagerInterface $em
* @return Response
*/
public function passreset2 (\Swift_Mailer $mailer, \App\Service\Cezar $cezar, \Symfony\Component\HttpFoundation\Request $zadanie, \Doctrine\ORM\EntityManagerInterface $em): Response {
$termin = new \DateTime (date ("Y-m-d H:i:s", time() + 86400 * 7));
$user = $em->getRepository(\App\Entity\User::class)->findOneBy (["login" => $zadanie->get ("login", "")]);
if ($user) {
$emailMessage = new \Swift_Message("InforPol NET - reset hasła.");
$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"))
->setTo ($user->getEmail (), $user->getNazwa ())
->setBody (
$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()) ]))]),
'text/html'
);
$mailer->send($emailMessage);
$email = $user->getEmail();
$cnt = round (strlen ($email) * 0.3);
if ($cnt < 1) $cnt = 1;
$email = str_replace ("@", "", substr ($email, 0, $cnt)) . "**@****" . str_replace ("@", "", substr ($email, strlen ($email) - 1 - $cnt));
return $this->render('pass_reset_stage2.html.twig', ["email" => $email, "termin" => $termin]);
} else {
$email = [
"biuro@domena.pl",
"info@domena.pl",
"poczta@domena.pl",
"sklep@domena.pl",
"biuro@domena.waw.pl",
"info@domena.waw.pl",
"poczta@domena.waw.pl",
"sklep@domena.waw.pl",
"biuro@domena.com.pl",
"info@domena.com.pl",
"poczta@domena.com.pl",
"sklep@domena.com.pl",
"biuro@domena.eu",
"info@domena.eu",
"poczta@domena.eu",
"sklep@domena.eu"
][rand(0, 15)];
$cnt = round (strlen ($email) * 0.3);
if ($cnt < 1) $cnt = 1;
$email = str_replace ("@", "", substr ($email, 0, $cnt)) . "**@****" . str_replace ("@", "", substr ($email, strlen ($email) - 1 - $cnt));
return $this->render('pass_reset_no-mail.html.twig', ["email" => $email, "termin" => $termin]);
}
}
/**
* Funkcja weryfikuje link i wyświetla stronę z formularzem restu hasła, bądź informacje o nieaktywności łącza
* @Route("/pass/reset/2/{key}", name="get_pass_reset_stage_2", methods={"GET"})
* @param \App\Service\Cezar $cezar
* @param \Doctrine\ORM\EntityManagerInterface $em
* @param string $key
* @return Response
*/
public function passreset3 (\App\Service\Cezar $cezar, \Doctrine\ORM\EntityManagerInterface $em, string $key): Response {
$error = false;
try {
$key = unserialize ($cezar->demath($key));
} catch (\Exception $e) {
$error = true;
}
if ($error) {
return $this->render("pass_reset_error_link.html.twig");
}
if (strtotime ($key ['termin']) < time ()) {
return $this->render("pass_reset_link.out.off.date.html.twig");
} else {
$user = $em->getRepository (\App\Entity\User::class)->find ($key ['user']);
if ($user === NULL) {
return $this->render("pass_reset_link.out.off.date.html.twig");
} else {
return $this->render ("pass_reset_reset.html.twig", [
"user" => $user,
"min" => intval ($em->getRepository (\App\Entity\Conf::class)->peek ("pass-min-len", 0)),
"max" => intval ($em->getRepository (\App\Entity\Conf::class)->peek ("pass-max-len", 0)),
"min_duzych" => intval ($em->getRepository (\App\Entity\Conf::class)->peek ("pass-min-upper", 0)),
"min_malych" => intval ($em->getRepository (\App\Entity\Conf::class)->peek ("pass-min-lower", 0)),
"min_cyfr" => intval ($em->getRepository (\App\Entity\Conf::class)->peek ("pass-min-digit", 0)),
"min_symboli" => intval ($em->getRepository (\App\Entity\Conf::class)->peek ("pass-min-symbols", 0)),
"symbole" => $em->getRepository (\App\Entity\Conf::class)->peek ("pass-symbols", "") == "" ? "`~!@#$%^&*()_-=+[]{};'\\:\"|,./<>?" : $em->getRepository (\App\Entity\Conf::class)->peek ("pass-min-symbols", ""),
]);
}
}
}
/**
* 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
* @Route("/pass/reset/2/{key}", name="post_pass_reset_stage_2", methods={"POST"})
* @param \Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface $PHI
* @param \App\Service\PassValidator $pv
* @param \App\Service\Cezar $cezar
* @param \Symfony\Component\HttpFoundation\Request $zadanie
* @param \Doctrine\ORM\EntityManagerInterface $em
* @param string $key
* @return Response
*/
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 $em, string $key): Response {
$error = false;
try {
$key = unserialize ($cezar->demath($key));
} catch (\Exception $e) {
$error = true;
}
if ($error) {
return $this->render("pass_reset_error_link.html.twig");
}
if (strtotime ($key ['termin']) < time ()) {
return $this->render("pass_reset_link.out.off.date.html.twig");
} else {
$user = $em->getRepository (\App\Entity\User::class)->find ($key ['user']);
if ($user === NULL) {
return $this->render("pass_reset_link.out.off.date.html.twig");
} else {
$errors = [];
if ($zadanie->get ("pass1", "") == "") {
$errors[] = [ "message" => "Hasło nie może być puste." ];
} else {
if ($zadanie->get ("pass1", "") != $zadanie->get ("pass2", "")) {
$errors[] = [ "message" => "Podane hasła są różne." ];
} else {
$test = $pv->isValid($em, $zadanie->get ("pass1"));
if ($test === true) {
} else {
$errors[] = [ "message" => nl2br ($test) ];
}
}
}
if (count ($errors) > 0) {
return $this->render ("pass_reset_reset.html.twig", [
"user" => $user,
"min" => intval ($em->getRepository (\App\Entity\Conf::class)->peek ("pass-min-len", 0)),
"max" => intval ($em->getRepository (\App\Entity\Conf::class)->peek ("pass-max-len", 0)),
"min_duzych" => intval ($em->getRepository (\App\Entity\Conf::class)->peek ("pass-min-upper", 0)),
"min_malych" => intval ($em->getRepository (\App\Entity\Conf::class)->peek ("pass-min-lower", 0)),
"min_cyfr" => intval ($em->getRepository (\App\Entity\Conf::class)->peek ("pass-min-digit", 0)),
"min_symboli" => intval ($em->getRepository (\App\Entity\Conf::class)->peek ("pass-min-symbols", 0)),
"symbole" => $em->getRepository (\App\Entity\Conf::class)->peek ("pass-symbols", "") == "" ? "`~!@#$%^&*()_-=+[]{};'\\:\"|,./<>?" : $em->getRepository (\App\Entity\Conf::class)->peek ("pass-min-symbols", ""),
"alerts" => $errors,
"pass" => $zadanie->get("pass1")
]);
} else {
$user->setPassword($PHI->hashPassword ($user, $zadanie->get ("pass1")));
$em->persist($user);
$em->flush();
return $this->render("pass_reset_setting-ok.html.twig", [ "user" => $user ]);
}
}
}
}
}