<?php
namespace App\Controller;
use App\Form\UpdatePasswordType;
use App\Repository\UserRepository;
use App\Form\ResettingPasswordType;
use Symfony\Component\Mime\Address;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Util\TargetPathTrait;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
class LoginController extends BaseController
{
private MailerInterface $mailer;
use TargetPathTrait;
public function __construct(MailerInterface $mailer)
{
$this->mailer = $mailer;
}
#[Route('/login', name: 'login')]
public function index(AuthenticationUtils $authenticationUtils, Request $request): Response
{
if ($this->getUser()) {
if (!$this->getUser()->getCompleted()) {
return $this->redirectToRoute('app_profil');
}elseif ($this->isGranted('ROLE_ADMIN') || $this->isGranted('ROLE_RD')) {
return $this->redirectToRoute('admin_dashboard');
}else{
return $this->redirectToRoute('front');
}
}
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('login/index.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
]);
}
#[Route('/reset_password', name: 'reset_password')]
public function reset_password(TokenGeneratorInterface $tokenGenerator,UserRepository $userRepository,Request $request,AuthenticationUtils $authenticationUtils): Response
{
$form = $this->createForm(ResettingPasswordType::class);
$form->handleRequest($request);
if ($form->isSubmitted()) {
$user = $userRepository->findOneBy(['email' => $form->get('email')->getData()]);
if ($user) {
$user->setTokenReset($tokenGenerator->generateToken());
}else {
$this->addFlash('EmailIncorrect', 'Adresse email est incorrect !');
return $this->redirectToRoute('reset_password');
}
$userRepository->add($user);
// generate a signed url and email it to the user
$protocol = stripos($_SERVER['SERVER_PROTOCOL'],'https') === 0 ? 'https://' : 'http://';
$email = new TemplatedEmail();
$email->from(new Address($this->getParameter('EmailAdmin'), 'IGPPP'))
->to($user->getEmail())
->subject('RĂ©initialisation de mot de passe')
->htmlTemplate('email/email_reset_password.html.twig')
->context([
'signedUrl' => $protocol.$_SERVER["HTTP_HOST"]. "/reset/" . $user->getTokenReset()
]);
$this->mailer->send($email);
return $this->redirectToRoute('login');
}
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('login/reset_password.html.twig', [
'last_username' => $lastUsername,
'form' => $form->createView(),
]);
}
#[Route('/reset/{token}', name: 'change_password')]
public function change_password($token,UserPasswordHasherInterface $userPasswordHasher,UserRepository $userRepository,Request $request): Response
{
$form = $this->createForm(UpdatePasswordType::class);
$form->handleRequest($request);
$user = $userRepository->findOneBy(['TokenReset' => $token]);
if (!$user) {
$this->addFlash('EmailIncorrect', 'Token incorrect !');
return $this->redirectToRoute('login');
}
if ($form->isSubmitted()) {
// generate a signed url and email it to the user
//dd($form->get('password')->getData(), $form->get('confirmpassword')->getData());
if (strcmp($form->get('password')->getData(), $form->get('confirmpassword')->getData()) == 0) {
$user->setPassword(
$userPasswordHasher->hashPassword(
$user,
$form->get('password')->getData()
)
);
$userRepository->add($user);
}else {
$this->addFlash('PasswordError', 'Les mots de passe ne sont pas identiques !');
return $this->redirect('/reset/' . $token);
}
return $this->redirectToRoute('login');
}
return $this->render('login/update_password.html.twig', [
'form' => $form->createView(),
]);
}
#[Route('/logout', name: 'app_logout')]
public function logout(){
return $this->redirectToRoute('login');
}
}