SecurityController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 Base\OAuthServerBundle\Controller;
  11. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  15. use Symfony\Component\Security\Core\Security;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  17. class SecurityController extends Controller
  18. {
  19. /**
  20. * @Route("/oauth/v2/auth/login", name="oauth_login")
  21. * @param Request $request
  22. *
  23. * @return Response
  24. */
  25. public function loginAction(Request $request)
  26. {
  27. /** @var $session \Symfony\Component\HttpFoundation\Session\Session */
  28. $session = $request->getSession();
  29. $authErrorKey = Security::AUTHENTICATION_ERROR;
  30. $lastUsernameKey = Security::LAST_USERNAME;
  31. // get the error if any (works with forward and redirect -- see below)
  32. if ($request->attributes->has($authErrorKey)) {
  33. $error = $request->attributes->get($authErrorKey);
  34. } elseif (null !== $session && $session->has($authErrorKey)) {
  35. $error = $session->get($authErrorKey);
  36. $session->remove($authErrorKey);
  37. } else {
  38. $error = null;
  39. }
  40. if (!$error instanceof AuthenticationException) {
  41. $error = null; // The value does not come from the security component.
  42. }
  43. // last username entered by the user
  44. $lastUsername = (null === $session) ? '' : $session->get($lastUsernameKey);
  45. $csrfToken = $this->has('security.csrf.token_manager')
  46. ? $this->get('security.csrf.token_manager')->getToken('authenticate')->getValue()
  47. : null;
  48. return $this->renderLogin(array(
  49. 'last_username' => $lastUsername,
  50. 'error' => $error,
  51. 'csrf_token' => $csrfToken,
  52. ));
  53. }
  54. /**
  55. * Renders the login template with the given parameters. Overwrite this function in
  56. * an extended controller to provide additional data for the login template.
  57. *
  58. * @param array $data
  59. *
  60. * @return Response
  61. */
  62. protected function renderLogin(array $data)
  63. {
  64. return $this->render('@BaseOAuthServerBundle/Security/login.html.twig', $data);
  65. }
  66. /**
  67. * @Route("/oauth/v2/auth/login_check", name="oauth_login_check")
  68. */
  69. public function checkAction()
  70. {
  71. throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');
  72. }
  73. /**
  74. * @Route("/oauth/v2/auth/logout", name="oauth_logout")
  75. */
  76. public function logoutAction()
  77. {
  78. throw new \RuntimeException('You must activate the logout in your security firewall configuration.');
  79. }
  80. }