<?php
namespace App\Controller;
use App\Constants\CentreServicesParametresConstants;
use App\Entity\CentreServicesParametres;
use App\Entity\OperateursCentres;
use App\Manager\Mail\SendMailManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Manager\Admincentre\Template\TemplateListManager;
use App\Service\Metier\CentreServicesParametresSM;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class SecurityController extends AbstractController
{
private CentreServicesParametresSM $centreServicesParametresSM;
public function __construct(
CentreServicesParametresSM $centreServicesParametresSM
){
$this->centreServicesParametresSM = $centreServicesParametresSM;
}
/**
* @Route("/", name="app_redirect_to_login")
*/
public function redirectToLogin(AuthenticationUtils $authenticationUtils): Response
{
return $this->redirectToRoute('app_login');
}
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
// if ($this->getUser()) {
// $this->redirectToRoute('target_path');
// }
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
/**
* @Route("/logout", name="app_logout")
*/
public function logout(UrlGeneratorInterface $urlGenerator)
{
throw new \Exception('This method can be blank - it will be intercepted by the logout key on your firewall');
}
/**
* @Route("/mot-de-passe-oublie", name="app_forgotten_password")
*/
public function forgottenPassword(Request $request, TokenGeneratorInterface $tokenGenerator, ManagerRegistry $doctrine): Response
{
if ($request->isMethod('POST')) {
$email = $request->request->get('email');
$entityManager = $doctrine->getManager();
$user = $entityManager->getRepository(OperateursCentres::class)->findOneBy(['email' => $email]);
if ($user === null || $user->getEtatArchivage()) {
$this->addFlash('danger', 'Impossible de réinitisaliser votre mot de passe. Veuillez réessayer ou contacter l\'administrateur.');
return $this->redirectToRoute('app_forgotten_password');
}
$token = $tokenGenerator->generateToken();
$user->setResetToken($token);
$entityManager->flush();
$url = $this->generateUrl('app_reset_password', array('token' => $token), UrlGeneratorInterface::ABSOLUTE_URL);
$template = TemplateListManager::getTemplateByCodeAndIdCentre($entityManager, TemplateListManager::RESET_MDP_INCALL, $user->getCentre()->getIdCentre());
$params['idTemplate'] = $template->getIdSendiblue();
$params['data'] = array(
'token'=> $url
);
$apiKey = $this->centreServicesParametresSM->getBrevoApiKey();
SendMailManager::sendinBlueEmailInCallWithParam($user->getEmail(), $apiKey, $params);
return $this->render('security/mail-sent.html.twig');
}
return $this->render('security/forgotten_password.html.twig');
}
/**
* @Route("/reset_password/{token}", name="app_reset_password")
*/
public function resetPassword(Request $request, string $token, UserPasswordHasherInterface $passwordHasher, ManagerRegistry $doctrine)
{
if ($request->isMethod('POST')) {
$entityManager = $doctrine->getManager();
$user = $entityManager->getRepository(OperateursCentres::class)->findOneBy(['resetToken' => $token]);
if ($user === null) {
$this->addFlash('danger', 'Token Inconnu');
return $this->redirectToRoute('app_reset_password', ['token' => $token]);
}
$user->setResetToken(null);
$user->setPassword($passwordHasher->hashPassword($user, $request->request->get('password')));
$entityManager->flush();
return $this->render('security/success_changed_password.html.twig', ['token' => $token]);
} else {
return $this->render('security/reset_password.html.twig', ['token' => $token]);
}
}
}