File: /var/www/dvpis2025/dvpis.kaunokolegija.lt/src/Controller/RemindPasswordController.php
<?php
namespace App\Controller;
use DateTime;
use Exception;
use App\Entity\User;
use App\Form\UserResetType;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\NonUniqueResultException;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\BodyRendererInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Throwable;
class RemindPasswordController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $manager,
private readonly UserRepository $userRepository,
private readonly UserPasswordHasherInterface $passwordHasher,
private readonly MailerInterface $mailer,
private readonly BodyRendererInterface $bodyRenderer,
) {
}
#[Route(path: '/resetpassword', name: 'resetpassword_index')]
public function resetAction(Request $request)
{
$user = new User();
$form = $this->createForm(UserResetType::class, $user);
$lastUserName = '';
return $this->render('remind/remind.html.twig', array(
'lastUserName' => $lastUserName,
'form' => $form->createView(),
));
}
/**
*
* @throws NonUniqueResultException
* @throws Exception
*/
#[Route(path: '/resetpassword/sendEmail', name: 'resetpassword_sendemail')]
public function sendEmailAction(Request $request)
{
if (!empty($userName = $request->get('_username'))) {
$user = $this->userRepository->loadUserByUsername($userName);
if (!empty($user)) {
$dateTime = time();
$hash = md5($user->getUserName() . "::" . $user->getEmail() . "::" . $dateTime);
$dateTimeValidTo = date("Y-m-d H:i:s", strtotime("+ 6 hours"));
$user->setChangePaswHash($hash);
$user->setChangePaswValidTo(new DateTime($dateTimeValidTo));
$this->manager->persist($user);
$this->manager->flush();
$this->sendUserEmail($user, $hash);
$this->addFlash(
'info',
'Jums buvo išsiųstas laiškas su slaptažodžio pakeitimo instrukcija! Patikrinkite pašto dėžutę.'
);
return $this->redirect($this->generateUrl('resetpassword_index'));
}
}
$this->addFlash(
'danger',
'Tokio naudotojo neradome!'
);
return $this->redirect($this->generateUrl('resetpassword_index'));
}
#[Route(path: '/resetpassword/resetFromEmail/{hash}', name: 'resetpassword_resetfromemail')]
public function resetFromEmailEmailAction($hash, Request $request)
{
if (!empty($hash)) {
$user = $this->userRepository->loadUserByHash($hash);
if (!empty($user)) {
if ($request->isMethod('POST')) {
$pasw1 = $request->get('_password1');
$pasw2 = $request->get('_password2');
if (empty($pasw1)) {
$this->addFlash(
'danger',
'Slaptažodis privalomas!'
);
return $this->render('remind/changeForm.html.twig', [
'hash' => $hash,
]);
}
if ($pasw1 != $pasw2) {
$this->addFlash(
'danger',
'Slaptažodžiai nesutampa!'
);
return $this->render('remind/changeForm.html.twig', [
'hash' => $hash,
]);
}
$user->setPassword($this->passwordHasher->hashPassword($user, $pasw1));
$user->setChangePaswValidTo(new DateTime());
$this->manager->flush();
$this->addFlash(
'info',
'Slaptažodis pakeistas'
);
return $this->redirect($this->generateUrl('app_login'));
}
return $this->render('remind/changeForm.html.twig', [
'hash' => $hash,
]);
}
}
$this->addFlash(
'danger',
'Slaptažodžio atnaujinimas negalimas. Bandykite dar kartą!'
);
return $this->redirect($this->generateUrl('resetpassword_index'));
}
private function sendUserEmail($user, $hash): void
{
$email =
(new TemplatedEmail())
->subject('Slaptažodžio keitimas')
->from('no-reply@kaunokolegija.lt')
->to($user->getEmail())
->htmlTemplate('remind/remindEmail.html.twig')
->context([
'name' => $user->getUserName(),
'hash' => $hash,
])
;
$this->bodyRenderer->render($email);
try {
$this->mailer->send($email);
} catch (Throwable $e) {
$this->addFlash($e->getCode(), $e->getMessage());
}
}
}