src/Controller/SecurityController.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  8. class SecurityController extends AbstractController
  9. {
  10.     /**
  11.      * @Route(
  12.      *     "/",
  13.      *     name="app_login"
  14.      * )
  15.      */
  16.     public function login(AuthenticationUtils $authenticationUtils): Response
  17.     {
  18.         if ($this->getUser()) {
  19.              return $this->redirectToRoute('app_security');
  20.          }
  21.         // get the login error if there is one
  22.         $error $authenticationUtils->getLastAuthenticationError();
  23.         // last username entered by the user
  24.         $lastUsername $authenticationUtils->getLastUsername();
  25.         return $this->render('login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  26.     }
  27.     /**
  28.      * @Route(
  29.      *     "/security",
  30.      *     name="app_security"
  31.      * )
  32.      */
  33.     public function securityAction()
  34.     {
  35.         $user $this->getUser();
  36.         if(!$user)
  37.             return $this->redirect($this->generateUrl('app_login'));
  38.         return $this->redirect($this->generateUrl('profile'));
  39.     }
  40.     /**
  41.      * @Route(
  42.      *     "/logout",
  43.      *     name="app_logout"
  44.      * )
  45.      */
  46.     public function logout(): void
  47.     {
  48.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  49.     }
  50.     /**
  51.      * @Route(
  52.      *     "/logout/no/valid/ip"
  53.      * )
  54.      */
  55.     public function logoutNoValidIp()
  56.     {
  57.         $message "you can't log in from this IP";
  58.         $this->addFlash('message'$message);
  59.         return $this->redirect($this->generateUrl('app_login'));
  60.     }
  61. }