src/Controller/SecurityController.php line 31

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