src/Controller/SecurityController.php line 20

  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  7. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  8. #[Route("/admin")]
  9. class SecurityController extends AbstractController
  10. {
  11.     #[Route("/login"name"login"methods: ["GET","POST"])]
  12.     /**
  13.      * @param Request                       $request
  14.      * @param AuthenticationUtils           $authenticationUtils
  15.      * @param AuthorizationCheckerInterface $authorizationChecker
  16.      * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
  17.      */
  18.     public function login(Request $requestAuthenticationUtils $authenticationUtilsAuthorizationCheckerInterface $authorizationChecker)
  19.     {
  20.         // si déjà connecté on redirige sur la home
  21.         if ($request->getMethod() == 'GET' && $authorizationChecker->isGranted('ROLE_ADMIN')) {
  22.             return $this->redirectToRoute('sonata_admin_dashboard');
  23.         }
  24.         // c'est un peu magique... mais ça marche
  25.         // get the login error if there is one
  26.         $error $authenticationUtils->getLastAuthenticationError();
  27.         // last username entered by the user
  28.         $lastUsername $authenticationUtils->getLastUsername();
  29.         return $this->render('security/login.html.twig', [
  30.             'last_username' => $lastUsername,
  31.             'error'         => $error,
  32.         ]);
  33.     }
  34.     #[Route("/logout"name"logout")]
  35.     public function logout()
  36.     {
  37.         // configuré dans config/packages/security.yaml
  38.     }
  39. }