<?php
namespace App\Controller;
use DateTime;
use App\Entity\User;
use App\Form\ForgetPassType;
use App\Form\UpdatePassType;
use App\Service\EmailService;
use App\Repository\UserRepository;
use App\Service\UpdatePasswordService;
use App\Repository\UpdatePasswordRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
#[Route(path: '/login', name: 'app_login')]
public function login(AuthenticationUtils $authenticationUtils): Response
{
// if ($this->getUser()) {
// return $this->redirectToRoute('target_path');
// }
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
#[Route(path: '/logout', name: 'app_logout')]
public function logout(): void
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
/**
* Route permettant de faire une demande de mot de passe oublié. Envoi un email à l'adresse inscrit par l'utilisateur
*
* @param Request $request instance de l'objet Request
* @param UserRepository $userRepository instance du repository lié à l'entité User
* @param UpdatePasswordService $updatePasswordService instance du service lié à l'entité UpdatePassword
* @param EmailService $emailService instance du service lié aux emails
* @return Response
*/
#[Route(path:"/login/forget", name:"pass_forget")]
public function forgetPass(
Request $request,
UserRepository $userRepository,
UpdatePasswordService $updatePasswordService,
EmailService $emailService
): Response {
$form = $this->createForm(ForgetPassType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$userfinded = $userRepository->findOneBy(["email" => $form->get("email")->getData() ]);
if(!$userfinded) {
return $this->render('security/pass_forget.html.twig', [
'form' => $form->createView(),
'error' => "L'adresse mail " . $form->get("email")->getData() . " ne correspond à aucun utilisateur",
'submited' => true
]);
}
$token = $updatePasswordService->askChangePassword($userfinded);
$emailService->sendPasswordMail($token, $form->get("email")->getData());
}
return $this->render('security/pass_forget.html.twig', [
'form' => $form->createView(),
'error' => null,
'submited' => $form->isSubmitted() && $form->isValid()
]);
}
/**
* Route permettant à l'utilisateur d'entrer un nouveau mot de passe
*
* @param string $token Token généré lors de la demande de mot de passe oublié
* @param Request $request instance de l'objet Request
* @param UpdatePasswordRepository $updatePasswordRepository instance du repository lié à l'entité UpdatePassword
* @param UpdatePasswordService $updatePasswordService instance du service lié à l'entité UpdatePassword
* @return Response
*/
#[Route(path:"/pass-change/{token}", name:"pass_change")]
public function passChange(
string $token,
Request $request,
UpdatePasswordRepository $updatePasswordRepository,
UpdatePasswordService $updatePasswordService,
EntityManagerInterface $em,
): Response {
$success = false;
$form = $this->createForm(UpdatePassType::class);
$ask = $updatePasswordRepository->findOneBy(["token" => $token]);
$isExpired =($ask) ? $ask->getExpiration() < new DateTime() : true;
if (!$isExpired) {
$info = $updatePasswordService->onPassFormSend($form, $request, $ask);
}
if (!$isExpired && $info === true) {
$info = null;
$success = true;
}
return $this->render('security/pass_change.html.twig', [
'form' => $form->createView(),
'isExpired' => $isExpired,
'error' => $info ?? null,
'success' => $success ?? null
]);
}
#[Route(path:'/regenerate/token/{id}', name:"regenerate_pass_token")]
public function regeneratePassToken(User $user, UpdatePasswordService $updatePasswordService, EmailService $emailService)
{
if($this->isGranted('ROLE_ADMIN', $user)) {
$token = $updatePasswordService->askChangePassword($user);
$emailService->sendAccountCreated($token, $user->getEmail());
}
return $this->redirect($this->generateUrl('app_user_crud_index'));
}
}