<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\CreatePasswordType;
use App\Form\PasswordForgotType;
use App\Repository\UserRepository;
use App\Service\MailerService;
use Doctrine\ORM\NoResultException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
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;
class SecurityController extends AbstractController
{
/**
* @Route("/", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
if ($this->getUser() && $this->isGranted('ROLE_SUPERADMIN_RECYCLINK')) {
return $this->redirectToRoute('super_admin_recyclink');
}
if ($this->getUser() && $this->isGranted('ROLE_USER') || $this->getUser() && $this->isGranted('ROLE_ADMIN')) {
return $this->redirectToRoute('client_index');
}
// 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")
*/
public function logout()
{
throw new \LogicException(
'This method can be blank - it will be intercepted by the logout key on your firewall.'
);
}
/**
* @Route("/create-password/{slug}", name="app_create_password")
*/
public function initializePassword(User $user, Request $request, UserPasswordHasherInterface $hasher)
{
if (! $user->getCreatePassword() === false) {
throw new UnauthorizedHttpException("Vous n'êtes pas autoriser à venir ici.");
}
$form = $this->createForm(CreatePasswordType::class, $user);
$form->handleRequest($request);
if (! $form->isSubmitted() || ! $form->isValid()) {
return $this->render('security/create-password.html.twig', [
'form' => $form->createView(),
]);
}
$user->setPassword($hasher->hashPassword($user, $form->get('password')->getData()));
$this->getDoctrine()->getManager()->flush();
$this->addFlash('create_password', "Votre mot de passe à bien été créé.");
return $this->redirectToRoute('app_login');
}
#[Route("/reset-password", name: "app_reset_password_request")]
public function reinitializePasswordRequest(
UserRepository $repository,
Request $request,
MailerService $mailerService,
UrlGeneratorInterface $urlGenerator
) {
$form = $this->createForm(PasswordForgotType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $repository->findOneBy([
'email' => $form['email']->getData(),
]);
$this->addFlash(
'email',
"Nous avons bien enregistrer votre demande, si votre email est enregistré vous recevrais un mail."
);
if ($user instanceof User) {
$uuid = $user->getResetToken() === null ?
uniqid($user->getSlug()) :
$user->getResetToken();
$path = $urlGenerator->generate(
'app_reset_password_request',
['uuid', $uuid],
UrlGeneratorInterface::ABSOLUTE_URL
);
$user->setResetToken($uuid);
$mailerService->sendEmailToResetPassword($path, $user->getEmail());
$this->getDoctrine()->getManager()->flush();
}
return $this->redirectToRoute('app_login');
}
return $this->renderForm('security/reset-password-request.html.twig', [
'form' => $form,
]);
}
#[Route("/reset-password/{uuid}", name: "app_reset_password")]
public function reinitializePassword(
UserRepository $repository,
Request $request,
UserPasswordHasherInterface $hasher
) {
$user = $repository->findOneBy([
'resetToken' => $request->get('uuid'),
]);
if (! $user instanceof User) {
throw new NoResultException();
}
$form = $this->createForm(CreatePasswordType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user->setPassword($hasher->hashPassword($user, $form->get('password')->getData()));
$user->setResetToken(null);
$this->getDoctrine()->getManager()->flush();
$this->addFlash('success', "Password mis à jour.");
return $this->redirectToRoute('app_login');
}
return $this->renderForm('security/reset-password.html.twig', [
'form' => $form,
]);
}
}