src/Controller/LoginController.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\HistoriqueUtilisateur;
  4. use App\Repository\HistoriqueUtilisateurRepository;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  10. class LoginController extends AbstractController
  11. {
  12.     /**
  13.     * @Route("/login", name="login_index")
  14.     */
  15.     public function index(AuthenticationUtils $authenticationUtils): Response
  16.     {
  17.         if($this->getUser()) return $this->redirectToRoute('sonata_admin_dashboard');
  18.         // get the login error if there is one
  19.         $error $authenticationUtils->getLastAuthenticationError();
  20.         // last username entered by the user
  21.         $lastUsername $authenticationUtils->getLastUsername();
  22.         return $this->render('login/index.html.twig', [
  23.             'last_username' => $lastUsername,
  24.             'error'         => $error,
  25.         ]);
  26.     }
  27.     /**
  28.      * @Route("/post-login", name="login_post", methods={"GET"})
  29.      */
  30.     public function postLogin(
  31.         HistoriqueUtilisateurRepository $repo,
  32.         EntityManagerInterface $em
  33.     ): Response {
  34.         $user $this->getUser();
  35.         if (!$user) {
  36.             return $this->redirectToRoute('login_index');
  37.         }
  38.         $repo->log($userHistoriqueUtilisateur::ACTION_LOGIN"Connexion à l'espace utilisateur.");
  39.         $em->flush();
  40.         return $this->redirectToRoute('sonata_admin_dashboard');
  41.     }
  42.     /**
  43.     * @Route("/logout", name="login_logout", methods={"GET"})
  44.     */
  45.     public function logout(): void
  46.     {
  47.         // controller can be blank: it will never be called!
  48.         throw new \Exception('Don\'t forget to activate logout in security.yaml');
  49.     }
  50. }