src/Controller/SecurityController.php line 84

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\HttpFoundation\Response;
  14. use Symfony\Component\HttpFoundation\Session\Session;
  15. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  16. use Symfony\Component\Security\Core\Security;
  17. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  18. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use Symfony\Component\HttpFoundation\JsonResponse;
  21. /**
  22.  * Controller managing security.
  23.  *
  24.  * @author Thibault Duplessis <thibault.duplessis@gmail.com>
  25.  * @author Christophe Coevoet <stof@notk.org>
  26.  */
  27. class SecurityController extends Controller {
  28.     private $tokenManager;
  29.     public function __construct(CsrfTokenManagerInterface $tokenManager null) {
  30.         $this->tokenManager $tokenManager;
  31.     }
  32.     /**
  33.      * @param Request $request
  34.      *
  35.      * @return Response
  36.      */
  37.     public function loginAction(Request $request) {
  38.         $securityContext $this->container->get('security.authorization_checker');
  39.         if ($securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  40.          
  41.             return $this->redirect($this->generateUrl('tableau-de-bord'));
  42.         }
  43.         /** @var $session Session */
  44.         $session $request->getSession();
  45.         $authErrorKey Security::AUTHENTICATION_ERROR;
  46.         $lastUsernameKey Security::LAST_USERNAME;
  47.         // get the error if any (works with forward and redirect -- see below)
  48.         if ($request->attributes->has($authErrorKey)) {
  49.             $error $request->attributes->get($authErrorKey);
  50.         } elseif (null !== $session && $session->has($authErrorKey)) {
  51.             $error $session->get($authErrorKey);
  52.             $session->remove($authErrorKey);
  53.         } else {
  54.             $error null;
  55.             
  56.         }
  57.         if (!$error instanceof AuthenticationException) {
  58.             $error null// The value does not come from the security component.
  59.         }
  60.         // last username entered by the user
  61.         $lastUsername = (null === $session) ? '' $session->get($lastUsernameKey);
  62.         $csrfToken $this->tokenManager $this->tokenManager->getToken('authenticate')->getValue() : null;
  63.         return $this->renderLogin(array(
  64.                     'last_username' => $lastUsername,
  65.                     'error' => $error,
  66.                     'csrf_token' => $csrfToken,
  67.         ));
  68.     }
  69.     public function checkAction() {
  70.         throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');
  71.     }
  72.     public function logoutAction() {
  73.         throw new \RuntimeException('You must activate the logout in your security firewall configuration.');
  74.     }
  75.     /**
  76.      * Renders the login template with the given parameters. Overwrite this function in
  77.      * an extended controller to provide additional data for the login template.
  78.      *
  79.      * @param array $data
  80.      *
  81.      * @return Response
  82.      */
  83.     protected function renderLogin(array $data) {
  84.         return $this->render('Security/login.html.twig'$data);
  85.     }
  86. }