vendor/friendsofsymfony/user-bundle/src/Controller/SecurityController.php line 38

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\AbstractController;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  14. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  15. /**
  16. * Controller managing security.
  17. *
  18. * @author Thibault Duplessis <thibault.duplessis@gmail.com>
  19. * @author Christophe Coevoet <stof@notk.org>
  20. *
  21. * @final
  22. */
  23. class SecurityController extends AbstractController
  24. {
  25. private $authenticationUtils;
  26. private $tokenManager;
  27. public function __construct(AuthenticationUtils $authenticationUtils, ?CsrfTokenManagerInterface $tokenManager = null)
  28. {
  29. $this->authenticationUtils = $authenticationUtils;
  30. $this->tokenManager = $tokenManager;
  31. }
  32. public function loginAction(): Response
  33. {
  34. $error = $this->authenticationUtils->getLastAuthenticationError();
  35. $lastUsername = $this->authenticationUtils->getLastUsername();
  36. $csrfToken = $this->tokenManager
  37. ? $this->tokenManager->getToken('authenticate')->getValue()
  38. : null;
  39. return $this->renderLogin([
  40. 'last_username' => $lastUsername,
  41. 'error' => $error,
  42. 'csrf_token' => $csrfToken,
  43. ]);
  44. }
  45. /**
  46. * @return never
  47. */
  48. public function checkAction()
  49. {
  50. throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');
  51. }
  52. /**
  53. * @return never
  54. */
  55. public function logoutAction()
  56. {
  57. throw new \RuntimeException('You must activate the logout in your security firewall configuration.');
  58. }
  59. /**
  60. * Renders the login template with the given parameters. Overwrite this function in
  61. * an extended controller to provide additional data for the login template.
  62. *
  63. * @param array<string, mixed> $data
  64. */
  65. protected function renderLogin(array $data): Response
  66. {
  67. return $this->render('@FOSUser/Security/login.html.twig', $data);
  68. }
  69. }