<?php
namespace App\Controller;
use App\Entity\TokenUser;
use App\Entity\User;
use App\Form\ConfirmRecuperatePasswordType;
use App\Form\LoginType;
use App\Form\RecuperatePasswordType;
use App\Services\EmailSender;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Contracts\Translation\TranslatorInterface;
class SecurityController extends AbstractController
{
/**
* @param AuthenticationUtils $authenticationUtils
* @return Response
* @Route("/login", name="login")
*/
public function index(AuthenticationUtils $authenticationUtils): Response
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
$form = $this->createForm(LoginType::class, null, [
'lastUsername' => $lastUsername,
]);
return $this->render('security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
'form' => $form->createView()
]);
}
/**
* @Route("/logout", name="app_logout")
*/
public function logout(): void
{
}
/**
* @Route("/recuperate-password", name="forgot")
*
* @param Request $request
*
* @return RedirectResponse|Response
*/
public function recuperatePassword(Request $request, EntityManagerInterface $em, TranslatorInterface $translator, EmailSender $mailer)
{
$form = $this->createForm(RecuperatePasswordType::class);
$form->handleRequest($request);
$success = $request->get('success');
if ($form->isSubmitted() and $form->isValid()) {
//get user by email
$user = $em->getRepository(User::class)->findOneBy(['email' => $form->getData()['email']]);
if(!$user){
$this->addFlash(
'success',
$translator->trans('security.no_user')
);
return $this->render('security/recuperate_password.html.twig', [
'form' => $form->createView(),
'title' => $translator->trans('security.recuperate_password'),
// 'success' => true
]);
}
//create Token
$token = new TokenUser();
$token->setType(TokenUser::TOKEN_RECUPERATE_PASSWORD);
$user->addToken($token);
$em->persist($token);
$em->flush();
$data = [
'user' => $user,
'path' => $this->generateUrl('confirm_recuperate_password', [
'token' => $token->getToken(),
], UrlGeneratorInterface::ABSOLUTE_URL),
];
//send email
$mailer->sendEmail(
'mails/recuperate_password.html.twig',
$translator->trans('general.app_name'),
$user->getEmail(),
null,
null,
$data
);
//Add flash success
$this->addFlash(
'success',
$translator->trans('security.recuperate_password_success')
);
}
return $this->render('security/recuperate_password.html.twig', [
'form' => $form->createView(),
'title' => $translator->trans('security.recuperate_password'),
// 'success' => true
]);
}
/**
* @Route("/confirm/recuperate-password/{token}/{_locale}", name="confirm_recuperate_password")
*
* @param Request $request
* @param $token
*
*/
public function confirmRecuperatePassword(
Request $request,
$token,
UserPasswordHasherInterface $encoder,
EntityManagerInterface $em
) {
$tokenUser = $em->getRepository(TokenUser::class)->findOneBy([
'token' => $token,
]);
$now = new DateTime();
if (!$tokenUser->getEnabled() or
$now >= $tokenUser->getExpiredAt() or
TokenUser::TOKEN_RECUPERATE_PASSWORD !== $tokenUser->getType()) {
return $this->redirectToRoute('forgot');
}
$form = $this->createForm(ConfirmRecuperatePasswordType::class);
$form->handleRequest($request);
if ($form->isSubmitted() and $form->isValid()) {
//Disabled token
$tokenUser->setEnabled(false);
//get user from token
$user = $tokenUser->getUser();
$user->setPassword($encoder->hashPassword($user, $form->getData()['password']));
$em->persist($tokenUser);
$em->persist($user);
$em->flush();
return $this->render('security/confirm_recuperate_password.html.twig', [
'form' => $form->createView(),
'success' => true
]);
}
return $this->render('security/confirm_recuperate_password.html.twig', [
'form' => $form->createView()
]);
}
}