AdminSecurityController.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 Sonata\UserBundle\Controller;
  11. use FOS\UserBundle\Controller\SecurityController;
  12. use Symfony\Component\DependencyInjection\ContainerAware;
  13. use Symfony\Component\Security\Core\SecurityContext;
  14. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. class AdminSecurityController extends SecurityController
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function loginAction()
  22. {
  23. $request = $this->container->get('request');
  24. /* @var $request \Symfony\Component\HttpFoundation\Request */
  25. $session = $request->getSession();
  26. /* @var $session \Symfony\Component\HttpFoundation\Session */
  27. // get the error if any (works with forward and redirect -- see below)
  28. if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
  29. $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
  30. } elseif (null !== $session && $session->has(SecurityContext::AUTHENTICATION_ERROR)) {
  31. $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
  32. $session->remove(SecurityContext::AUTHENTICATION_ERROR);
  33. } else {
  34. $error = '';
  35. }
  36. if ($error) {
  37. // TODO: this is a potential security risk (see http://trac.symfony-project.org/ticket/9523)
  38. $error = $error->getMessage();
  39. }
  40. // last username entered by the user
  41. $lastUsername = (null === $session) ? '' : $session->get(SecurityContext::LAST_USERNAME);
  42. if ($this->container->get('security.context')->isGranted('ROLE_ADMIN')) {
  43. $refererUri = $request->server->get('HTTP_REFERER');
  44. return new RedirectResponse($refererUri && $refererUri != $request->getUri() ? $refererUri : $this->container->get('router')->generate('sonata_admin_dashboard'));
  45. }
  46. return $this->container->get('templating')->renderResponse('SonataUserBundle:Admin:Security/login.html.'.$this->container->getParameter('fos_user.template.engine'), array(
  47. 'last_username' => $lastUsername,
  48. 'error' => $error,
  49. 'base_template' => $this->container->get('sonata.admin.pool')->getTemplate('layout'),
  50. 'admin_pool' => $this->container->get('sonata.admin.pool')
  51. ));
  52. }
  53. }