src/Controller/SecurityController.php line 29

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.         Location $location,
  15.         EntityManagerInterface $entityManager
  16.     )
  17.     {
  18.         $this->location $location;
  19.         $this->entityManager $entityManager;
  20.     }
  21.     /**
  22.      * @Route("/", name="app_login")
  23.      */
  24.     public function login(AuthenticationUtils $authenticationUtils): Response
  25.     {
  26.          if ($this->getUser()) {
  27.              return $this->redirectToRoute('app_security');
  28.          }
  29.         // get the login error if there is one
  30.         $error $authenticationUtils->getLastAuthenticationError();
  31.         // last username entered by the user
  32.         $lastUsername $authenticationUtils->getLastUsername();
  33.         return $this->render('login.html.twig', [
  34.             'last_username' => $lastUsername,
  35.             'error' => $error
  36.         ]);
  37.     }
  38.     /**
  39.      * @Route("/security", name="app_security")
  40.      */
  41.     public function securityAction()
  42.     {
  43.         $user $this->getUser();
  44.         if(!$user)
  45.             return $this->redirect($this->generateUrl('app_login'));
  46.         $em $this->entityManager;
  47.         $user->setLastLogin(new \DateTime());
  48.         $em->persist($user);
  49.         $em->flush();
  50.         if(
  51.             in_array('ROLE_SUPER_ADMIN',$user->getRoles()) or
  52.             in_array('ROLE_ADMIN',$user->getRoles())
  53.         )
  54.             return $this->redirect($this->generateUrl('profile'));
  55.         if(
  56.             in_array('ROLE_USER',$user->getRoles())
  57.         )
  58.             return $this->redirect($this->generateUrl('profile_message_create'));
  59.     }
  60.     /**
  61.      * @Route("/logout", name="app_logout")
  62.      */
  63.     public function logout(): void
  64.     {
  65.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  66.     }
  67.     /**
  68.      * @Route(
  69.      *     "/logout/no/valid/ip"
  70.      * )
  71.      */
  72.     public function logoutNoValidIp()
  73.     {
  74.         $location $this->location;
  75.         $user_data $location->getUserData();
  76.         $user_ip $user_data['ip'];
  77.         $message "you can't log in from this IP: $user_ip";
  78.         $this->addFlash('message'$message);
  79.         return $this->redirect($this->generateUrl('app_login'));
  80.     }
  81. }