SecurityController.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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\Component\DependencyInjection\ContainerAware;
  12. use Symfony\Component\Security\Core\SecurityContext;
  13. class SecurityController extends ContainerAware
  14. {
  15. public function loginAction()
  16. {
  17. $request = $this->container->get('request');
  18. /* @var $request \Symfony\Component\HttpFoundation\Request */
  19. $session = $request->getSession();
  20. /* @var $session \Symfony\Component\HttpFoundation\Session\Session */
  21. // get the error if any (works with forward and redirect -- see below)
  22. if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
  23. $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
  24. } elseif (null !== $session && $session->has(SecurityContext::AUTHENTICATION_ERROR)) {
  25. $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
  26. $session->remove(SecurityContext::AUTHENTICATION_ERROR);
  27. } else {
  28. $error = '';
  29. }
  30. if ($error) {
  31. // TODO: this is a potential security risk (see http://trac.symfony-project.org/ticket/9523)
  32. $error = $error->getMessage();
  33. }
  34. // last username entered by the user
  35. $lastUsername = (null === $session) ? '' : $session->get(SecurityContext::LAST_USERNAME);
  36. $csrfToken = $this->container->has('form.csrf_provider') ? $this->container->get('form.csrf_provider')->generateCsrfToken('authenticate') : null;
  37. return $this->renderLogin(array(
  38. 'last_username' => $lastUsername,
  39. 'error' => $error,
  40. 'csrf_token' => $csrfToken,
  41. ));
  42. }
  43. /**
  44. * Renders the login template with the given parameters. Overwrite this function in
  45. * an extended controller to provide additional data for the login template.
  46. *
  47. * @param array $data
  48. *
  49. * @return \Symfony\Component\HttpFoundation\Response
  50. */
  51. protected function renderLogin(array $data)
  52. {
  53. $template = sprintf('FOSUserBundle:Security:login.html.%s', $this->container->getParameter('fos_user.template.engine'));
  54. return $this->container->get('templating')->renderResponse($template, $data);
  55. }
  56. public function checkAction()
  57. {
  58. throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');
  59. }
  60. public function logoutAction()
  61. {
  62. throw new \RuntimeException('You must activate the logout in your security firewall configuration.');
  63. }
  64. }