AdminSecurityController.php 2.5 KB

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