src/Controller/SecurityController.php line 44
<?phpnamespace App\Controller;use App\Entity\User;use App\Service\MailerService;use Doctrine\ORM\EntityManagerInterface;use Symfony\Bundle\SecurityBundle\Security;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;use Symfony\Component\Routing\Annotation\Route;use Symfony\Component\HttpFoundation\RedirectResponse;use Symfony\Component\Routing\Generator\UrlGeneratorInterface;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;class SecurityController extends AbstractController{/*** @var EntityManagerInterface*/private $em;/*** @var MailerService*/private $mailerService;/*** @var UserPasswordHasherInterface*/private $passwordEncoder;public function __construct(private Security $security, EntityManagerInterface $em, MailerService $mailerService, UserPasswordHasherInterface $passwordEncoder){$this->security = $security;$this->em = $em;$this->mailerService = $mailerService;$this->passwordEncoder = $passwordEncoder;}#[Route(path: '/login', name: 'app_login')]public function login(AuthenticationUtils $authenticationUtils): Response{if ($this->security->isGranted('ROLE_ADMIN')) {return $this->redirectToRoute('app_dashboard');} else if ($this->security->isGranted('ROLE_MANAGER')) {return $this->redirectToRoute('app_dashboard');} else if ($this->security->isGranted('ROLE_COLLAB')) {return $this->redirectToRoute('app_dashboard');}// 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(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: '/oubli-mot-de-passe', name: 'app_forgotten_password')]public function forgottenPassword(Request $request): Response{if ($request->isMethod('POST')) {$email = $request->request->get('email');/** @var User $user */$user = $this->em->getRepository(User::class)->findOneByEmail($email);if ($user === null) {return $this->redirectToRoute('app_forgotten_password', ['error' => 'Email invalide']);}$token = md5(uniqid());$user->setToken($token);$this->em->flush();$params = ['user' => $user,'url' => $this->generateUrl('app_reset_password', array('token' => $token), UrlGeneratorInterface::ABSOLUTE_URL)];$this->mailerService->sendResetPassword($params);return $this->redirectToRoute('app_login');}return $this->render('security/forgot-password.html.twig');}#[Route(path: '/reinitialiser-mot-de-passe/{token}', name: 'app_reset_password')]public function resetPassword(string $token, Request $request){if ($request->isMethod('POST')) {/* @var User $user */$user = $this->em->getRepository(User::class)->findOneBy(['token' => $token]);if ($user === null) {return $this->redirectToRoute('app_login', ['error' => 'Token invalide']);}if (($request->request->get('password') != $request->request->get('passwordbis')) || (strlen($request->request->get('passwordbis')) < 9)) {return $this->redirectToRoute('app_reset_password', ['token' => $token]);}$user->setToken(null);$hashedPassword = $this->passwordEncoder->hashPassword($user, $request->request->get('password'));$user->setPassword($hashedPassword);$this->em->persist($user);$this->em->flush();return $this->redirectToRoute('app_login');} else {return $this->render('security/reset-password.html.twig', ['token' => $token]);}}}