src/Controller/SecurityController.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Log;
  4. use App\Entity\User;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  10. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  11. class SecurityController extends AbstractController
  12. {
  13.     /**
  14.      * @Route("/user/login", name="app_login")
  15.      */
  16.     public function login(AuthenticationUtils $authenticationUtils): Response
  17.     {
  18.         // if ($this->getUser()) {
  19.         //     return $this->redirectToRoute('target_path');
  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('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  26.     }
  27.     /**
  28.      * @Route("/user/login/seed", name="app_login_seed")
  29.      */
  30.     public function login_seed(Request $request): Response
  31.     {
  32.         $this->get('security.token_storage')->setToken(null);
  33.         $request->getSession()->invalidate();
  34.         $user $this->getDoctrine()->getManager()->getRepository(User::class)->findOneBy(array('email' => "seed"));
  35.         if (!$user) {
  36.             return $this->redirect($this->generateUrl('app_login'));
  37.         }
  38.         $this->getDoctrine()->getRepository(Log::class)->addLog($user'CONNECT');
  39.         $token = new UsernamePasswordToken($usernull'main'$user->getRoles());
  40.         $this->get('security.token_storage')->setToken($token);
  41.         $this->get('session')->set('_security_main'serialize($token));
  42.         return $this->redirect($this->generateUrl('home'));
  43.     }
  44.     /**
  45.      * @Route("/user/logout", name="app_logout")
  46.      */
  47.     public function logout()
  48.     {
  49.         throw new \Exception('This method can be blank - it will be intercepted by the logout key on your firewall');
  50.     }
  51. }