<?php
namespace App\Controller;
use App\Entity\HistoriqueUtilisateur;
use App\Repository\HistoriqueUtilisateurRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class LoginController extends AbstractController
{
/**
* @Route("/login", name="login_index")
*/
public function index(AuthenticationUtils $authenticationUtils): Response
{
if($this->getUser()) return $this->redirectToRoute('sonata_admin_dashboard');
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('login/index.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
]);
}
/**
* @Route("/post-login", name="login_post", methods={"GET"})
*/
public function postLogin(
HistoriqueUtilisateurRepository $repo,
EntityManagerInterface $em
): Response {
$user = $this->getUser();
if (!$user) {
return $this->redirectToRoute('login_index');
}
$repo->log($user, HistoriqueUtilisateur::ACTION_LOGIN, "Connexion à l'espace utilisateur.");
$em->flush();
return $this->redirectToRoute('sonata_admin_dashboard');
}
/**
* @Route("/logout", name="login_logout", methods={"GET"})
*/
public function logout(): void
{
// controller can be blank: it will never be called!
throw new \Exception('Don\'t forget to activate logout in security.yaml');
}
}