vendor/friendsofsymfony/user-bundle/Controller/SecurityController.php line 28

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