AdminSecurityController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\Model\UserInterface;
  12. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  17. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  18. use Symfony\Component\Security\Core\Security;
  19. use Symfony\Component\Security\Core\SecurityContextInterface;
  20. class AdminSecurityController extends Controller
  21. {
  22. /**
  23. * @param Request $request
  24. *
  25. * @return Response|RedirectResponse
  26. */
  27. public function loginAction(Request $request = null)
  28. {
  29. $user = $this->getUser();
  30. if ($user instanceof UserInterface) {
  31. $this->get('session')->getFlashBag()->set('sonata_user_error', 'sonata_user_already_authenticated');
  32. $url = $this->generateUrl('sonata_admin_dashboard');
  33. return $this->redirect($url);
  34. }
  35. $session = $request->getSession();
  36. // NEXT_MAJOR: Symfony <2.6 BC. To be removed.
  37. $authenticationErrorKey = class_exists('Symfony\Component\Security\Core\Security')
  38. ? Security::AUTHENTICATION_ERROR : SecurityContextInterface::AUTHENTICATION_ERROR;
  39. // get the error if any (works with forward and redirect -- see below)
  40. if ($request->attributes->has($authenticationErrorKey)) {
  41. $error = $request->attributes->get($authenticationErrorKey);
  42. } elseif (null !== $session && $session->has($authenticationErrorKey)) {
  43. $error = $session->get($authenticationErrorKey);
  44. $session->remove($authenticationErrorKey);
  45. } else {
  46. $error = null;
  47. }
  48. if (!$error instanceof AuthenticationException) {
  49. $error = null; // The value does not come from the security component.
  50. }
  51. // NEXT_MAJOR: Symfony <2.6 BC. To be removed.
  52. $lastUsernameKey = class_exists('Symfony\Component\Security\Core\Security')
  53. ? Security::LAST_USERNAME : SecurityContextInterface::LAST_USERNAME;
  54. // last username entered by the user
  55. $lastUsername = (null === $session) ? '' : $session->get($lastUsernameKey);
  56. // NEXT_MAJOR: Symfony <2.4 BC. To be removed.
  57. if ($this->has('security.csrf.token_manager')) {
  58. $csrfToken = $this->get('security.csrf.token_manager')->getToken('authenticate')->getValue();
  59. } else {
  60. $csrfToken = $this->has('form.csrf_provider')
  61. ? $this->get('form.csrf_provider')->generateCsrfToken('authenticate')
  62. : null;
  63. }
  64. // NEXT_MAJOR: remove when dropping Symfony <2.8 support
  65. $authorizationCheckerService = $this->has('security.authorization_checker')
  66. ? $this->get('security.authorization_checker') : $this->get('security.context');
  67. if ($authorizationCheckerService->isGranted('ROLE_ADMIN')) {
  68. $refererUri = $request->server->get('HTTP_REFERER');
  69. return $this->redirect($refererUri && $refererUri != $request->getUri() ? $refererUri : $this->generateUrl('sonata_admin_dashboard'));
  70. }
  71. // NEXT_MAJOR: Deprecated in 2.3, to be removed in 4.0
  72. try {
  73. $resetRoute = $this->generateUrl('sonata_user_admin_resetting_request');
  74. } catch (RouteNotFoundException $e) {
  75. @trigger_error('Using the route fos_user_resetting_request for admin password resetting is deprecated since version 2.3 and will be removed in 3.0. Use sonata_user_admin_resetting_request instead.', E_USER_DEPRECATED);
  76. $resetRoute = $this->generateUrl('fos_user_resetting_request');
  77. }
  78. return $this->render('SonataUserBundle:Admin:Security/login.html.'.$this->container->getParameter('fos_user.template.engine'), array(
  79. 'last_username' => $lastUsername,
  80. 'error' => $error,
  81. 'csrf_token' => $csrfToken,
  82. 'base_template' => $this->get('sonata.admin.pool')->getTemplate('layout'),
  83. 'admin_pool' => $this->get('sonata.admin.pool'),
  84. 'reset_route' => $resetRoute, // NEXT_MAJOR: Deprecated in 2.3, to be removed in 4.0
  85. ));
  86. }
  87. public function checkAction()
  88. {
  89. throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');
  90. }
  91. public function logoutAction()
  92. {
  93. throw new \RuntimeException('You must activate the logout in your security firewall configuration.');
  94. }
  95. /**
  96. * Renders the login template with the given parameters. Overwrite this function in
  97. * an extended controller to provide additional data for the login template.
  98. *
  99. * @param array $data
  100. *
  101. * @return Response
  102. */
  103. protected function renderLogin(array $data)
  104. {
  105. $template = sprintf('FOSUserBundle:Security:login.html.%s', $this->container->getParameter('fos_user.template.engine'));
  106. return $this->render($template, $data);
  107. }
  108. }