<?php
namespace App\Controller;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
class SecurityController extends AbstractController
{
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
/**
* @Route("/logout", name="app_logout", methods={"GET"})
*/
public function logout()
{
// controller can be blank: it will never be executed!
throw new \Exception('Don\'t forget to activate logout in security.yaml');
}
/**
* @Route("/mot-de-passe-oublie", name="app_forgotten_password")
*/
public function forgottenPassword(Request $request, UserPasswordEncoderInterface $encoder, MailerInterface $mailer, TokenGeneratorInterface $tokenGenerator): Response
{
if ($request->isMethod('POST')) {
$email = $request->request->get('email');
$entityManager = $this->getDoctrine()->getManager();
$user = $entityManager->getRepository(User::class)->findOneByEmail($email);
/* @var $user User */
if ($user === null) {
$this->addFlash('danger', 'Email Inconnu');
return $this->redirectToRoute('app_forgotten_password');
}
$token = $tokenGenerator->generateToken();
try{
$user->setResetToken($token);
$entityManager->flush();
} catch (\Exception $e) {
$this->addFlash('warning', $e->getMessage());
return $this->redirectToRoute('app_login');
}
$url = $this->generateUrl('app_reset_password', array('token' => $token), UrlGeneratorInterface::ABSOLUTE_URL);
$html = $this->renderView(
'emails/forgotten_password.html.twig',
[
'user' => $user,
'url' => $url
]
);
$message = (new Email())
->from(new Address($this->getParameter('admin_email'), 'prejeance industrial'))
->to($user->getEmail())
->subject('Mot de passe oublié')
->html($html);
$mailer->send($message);
$this->addFlash('notice', 'Mail envoyé');
return $this->redirectToRoute('app_login');
}
return $this->render('security/forgotten_password.html.twig');
}
/**
* @Route("/changer-mot-de-passe/{token}", name="app_reset_password")
*/
public function resetPassword(Request $request, string $token, UserPasswordEncoderInterface $passwordEncoder)
{
if ($request->isMethod('POST')) {
$entityManager = $this->getDoctrine()->getManager();
$user = $entityManager->getRepository(User::class)->findOneByResetToken($token);
/* @var $user User */
if ($user === null) {
$this->addFlash('danger', 'Token Inconnu');
return $this->redirectToRoute('app_reset_password',['token' => $token]);
}
$user->setResetToken(null);
$user->setPassword($passwordEncoder->encodePassword($user, $request->request->get('password')));
$entityManager->flush();
$this->addFlash('notice', 'Mot de passe mis à jour');
return $this->redirectToRoute('app_login');
}else {
return $this->render('security/reset_password.html.twig', ['token' => $token]);
}
}
}