AdminSecurityController.php 2.1 KB

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