src/Controller/SecurityController.php line 47

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSUserBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace App\Controller;
  11. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Security\Core\Security;
  14. use Symfony\Component\Security\Core\SecurityContextInterface;
  15. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\HttpFoundation\JsonResponse;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  20. class SecurityController extends AbstractController
  21. {
  22.     /**
  23.      *
  24.      * @Route("/login", name="login")
  25.      */
  26.     public function loginAction(Request $request)
  27.     {
  28.         /** @var $session \Symfony\Component\HttpFoundation\Session\Session */
  29.         $session $request->getSession();
  30.         if (class_exists('\Symfony\Component\Security\Core\Security')) {
  31.             $authErrorKey Security::AUTHENTICATION_ERROR;
  32.             $lastUsernameKey Security::LAST_USERNAME;
  33.         } else {
  34.             // BC for SF < 2.6
  35.             $authErrorKey SecurityContextInterface::AUTHENTICATION_ERROR;
  36.             $lastUsernameKey SecurityContextInterface::LAST_USERNAME;
  37.         }
  38.         // get the error if any (works with forward and redirect -- see below)
  39.         if ($request->attributes->has($authErrorKey)) {
  40.             $error $request->attributes->get($authErrorKey);
  41.         } elseif (null !== $session && $session->has($authErrorKey)) {
  42.             $error $session->get($authErrorKey);
  43.             $session->remove($authErrorKey);
  44.         } else {
  45.             $error null;
  46.         }
  47.         if (!$error instanceof AuthenticationException) {
  48.             $error null// The value does not come from the security component.
  49.         }
  50.         // last username entered by the user
  51.         $lastUsername = (null === $session) ? '' $session->get($lastUsernameKey);
  52.         if ($this->has('security.csrf.token_manager')) {
  53.             $csrfToken $this->get('security.csrf.token_manager')->getToken('authenticate')->getValue();
  54.         } else {
  55.             // BC for SF < 2.4
  56.             $csrfToken $this->has('form.csrf_provider')
  57.                 ? $this->get('form.csrf_provider')->generateCsrfToken('authenticate')
  58.                 : null;
  59.         }
  60.         return $this->renderLogin(array(
  61.             'last_username' => $lastUsername,
  62.             'error' => $error,
  63.             'csrf_token' => $csrfToken,
  64.         ));
  65.     }
  66.     /**
  67.      * @Route("/api/login_check", name="api_login")
  68.      * @return JsonResponse
  69.      */
  70.     public function api_login(): JsonResponse
  71.     {
  72.         $user $this->getUser();
  73.         return new Response([
  74.             'email' => $user->getEmail(),
  75.             'roles' => $user->getRoles(),
  76.         ]);
  77.     }
  78.     /**
  79.      * @Route("/login_check", name="login_check")
  80.      * @throws \RuntimeException
  81.      */
  82.     public function checkAction()
  83.     {
  84.         throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');
  85.     }
  86.     /**
  87.      * @Route("/logout", name="logout")
  88.      * @throws \RuntimeException
  89.      */
  90.     public function logoutAction()
  91.     {
  92.         throw new \RuntimeException('You must activate the logout in your security firewall configuration.');
  93.     }
  94.     /**
  95.      * Renders the login template with the given parameters. Overwrite this function in
  96.      * an extended controller to provide additional data for the login template.
  97.      *
  98.      * @param array $data
  99.      *
  100.      * @return \Symfony\Component\HttpFoundation\Response
  101.      */
  102.     protected function renderLogin(array $data)
  103.     {
  104.         return $this->render('Security/login.html.twig'$data);
  105.     }
  106. }