<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\ChangePasswordType;
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;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class SecurityController extends AbstractController
{
#[Route(path: '/login', name: 'app_login')]
public function login(AuthenticationUtils $authenticationUtils): Response
{
if ($this->getUser()) {
if ($this->isGranted('ROLE_HOTESSE')) {
return $this->redirectToRoute('app_register');
} else {
return $this->redirectToRoute('admin_index');
}
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('registration/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(path: '/changePassword/{qrcode}/{id}/{email}', name: 'app_changePassword')]
public function changePassword($qrcode, $id, $email, EntityManagerInterface $entityManager, Request $request, UserPasswordHasherInterface $userPasswordHasher): Response
{
//Si le QRCODE et l'ID ne sont pas des entiers on redirige.
if ($qrcode) {
$user = $entityManager->getRepository(User::class)->findByQrCode($id);
if ($user->getQrCode() * 100 != $qrcode) {
throw new \LogicException('Il y a un problème. Veuillez réessayer plus tard.');
}
if ($user->getEmail() != $email) {
throw new \LogicException('Il y a un problème. Veuillez réessayer plus tard.');
}
if ($qrcode && $email && $id) {
$form = $this->createForm(ChangePasswordType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user->setPassword(
$userPasswordHasher->hashPassword(
$user,
$user->getPassword()
)
);
$entityManager->persist($user);
$entityManager->flush();
return $this->redirectToRoute('changePasswordOk');
}
}
} else {
throw new \LogicException('Il y a un problème. Veuillez réessayer plus tard.');
}
return $this->render('registration/changePassword.html.twig', [
'passwordForm' => $form->createView(),
'user' => $user,
]);
}
#[Route(path: '/changePassword/ok', name: 'changePasswordOk')]
public function changePasswordOk()
{
return $this->render('error/changePasswordOk.html.twig');
}
}