SecurityFOSUser1Controller.php 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\UserBundle\Controller;
  11. use FOS\UserBundle\Controller\SecurityController;
  12. use Sonata\UserBundle\Model\UserInterface;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  15. use Symfony\Component\Security\Core\Security;
  16. use Symfony\Component\Security\Core\SecurityContextInterface;
  17. /**
  18. * @author Hugo Briand <briand@ekino.com>
  19. */
  20. class SecurityFOSUser1Controller extends SecurityController
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function loginAction()
  26. {
  27. // NEXT_MAJOR: remove when dropping Symfony <2.8 support
  28. $tokenStorageService = $this->container->has('security.token_storage')
  29. ? $this->container->get('security.token_storage') : $this->container->get('security.context');
  30. $token = $tokenStorageService->getToken();
  31. if ($token && $token->getUser() instanceof UserInterface) {
  32. $this->container->get('session')->getFlashBag()->set('sonata_user_error', 'sonata_user_already_authenticated');
  33. $url = $this->container->get('router')->generate('sonata_user_profile_show');
  34. return new RedirectResponse($url);
  35. }
  36. /*
  37. Implementation of parent::loginAction
  38. Needed for fixing the "Bad Credentials" translation problem
  39. The code is a mix of the 2.0.0 version
  40. and the 1.3.6 version of FOSUserBundle
  41. */
  42. // NEXT_MAJOR: Inject $request in the method signature instead.
  43. if ($this->container->has('request_stack')) {
  44. $request = $this->container->get('request_stack')->getCurrentRequest();
  45. } else {
  46. $request = $this->container->get('request');
  47. }
  48. /** @var $session \Symfony\Component\HttpFoundation\Session\Session */
  49. $session = $request->getSession();
  50. // NEXT_MAJOR: Symfony <2.6 BC. To be removed.
  51. $authenticationErrorKey = class_exists('Symfony\Component\Security\Core\Security')
  52. ? Security::AUTHENTICATION_ERROR : SecurityContextInterface::AUTHENTICATION_ERROR;
  53. // get the error if any (works with forward and redirect -- see below)
  54. if ($request->attributes->has($authenticationErrorKey)) {
  55. $error = $request->attributes->get($authenticationErrorKey);
  56. } elseif (null !== $session && $session->has($authenticationErrorKey)) {
  57. $error = $session->get($authenticationErrorKey);
  58. $session->remove($authenticationErrorKey);
  59. } else {
  60. $error = null;
  61. }
  62. if (!$error instanceof AuthenticationException) {
  63. $error = null; // The value does not come from the security component.
  64. }
  65. // NEXT_MAJOR: Symfony <2.6 BC. To be removed.
  66. $lastUserNameKey = class_exists('Symfony\Component\Security\Core\Security')
  67. ? Security::LAST_USERNAME : SecurityContextInterface::LAST_USERNAME;
  68. // NEXT_MAJOR: Symfony <2.4 BC. To be removed.
  69. if ($this->container->has('security.csrf.token_manager')) {
  70. $csrfToken = $this->container->get('security.csrf.token_manager')->getToken('authenticate')->getValue();
  71. } else {
  72. $csrfToken = $this->container->has('form.csrf_provider')
  73. ? $this->container->get('form.csrf_provider')->generateCsrfToken('authenticate')
  74. : null;
  75. }
  76. return $this->renderLogin(array(
  77. 'last_username' => (null === $session) ? '' : $session->get($lastUserNameKey),
  78. 'error' => $error,
  79. 'csrf_token' => $csrfToken,
  80. ));
  81. }
  82. }