DefaultAuthenticationSuccessHandler.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\Security\Http\Authentication;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  13. use Symfony\Component\Security\Http\HttpUtils;
  14. use Symfony\Component\Security\Http\ParameterBagUtils;
  15. /**
  16. * Class with the default authentication success handling logic.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  20. * @author Alexander <iam.asm89@gmail.com>
  21. */
  22. class DefaultAuthenticationSuccessHandler implements AuthenticationSuccessHandlerInterface
  23. {
  24. protected $httpUtils;
  25. protected $options;
  26. protected $providerKey;
  27. protected $defaultOptions = array(
  28. 'always_use_default_target_path' => false,
  29. 'default_target_path' => '/',
  30. 'login_path' => '/login',
  31. 'target_path_parameter' => '_target_path',
  32. 'use_referer' => false,
  33. );
  34. /**
  35. * @param HttpUtils $httpUtils
  36. * @param array $options Options for processing a successful authentication attempt
  37. */
  38. public function __construct(HttpUtils $httpUtils, array $options = array())
  39. {
  40. $this->httpUtils = $httpUtils;
  41. $this->setOptions($options);
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function onAuthenticationSuccess(Request $request, TokenInterface $token)
  47. {
  48. return $this->httpUtils->createRedirectResponse($request, $this->determineTargetUrl($request));
  49. }
  50. /**
  51. * Gets the options.
  52. *
  53. * @return array An array of options
  54. */
  55. public function getOptions()
  56. {
  57. return $this->options;
  58. }
  59. public function setOptions(array $options)
  60. {
  61. $this->options = array_merge($this->defaultOptions, $options);
  62. }
  63. /**
  64. * Get the provider key.
  65. *
  66. * @return string
  67. */
  68. public function getProviderKey()
  69. {
  70. return $this->providerKey;
  71. }
  72. /**
  73. * Set the provider key.
  74. *
  75. * @param string $providerKey
  76. */
  77. public function setProviderKey($providerKey)
  78. {
  79. $this->providerKey = $providerKey;
  80. }
  81. /**
  82. * Builds the target URL according to the defined options.
  83. *
  84. * @return string
  85. */
  86. protected function determineTargetUrl(Request $request)
  87. {
  88. if ($this->options['always_use_default_target_path']) {
  89. return $this->options['default_target_path'];
  90. }
  91. if ($targetUrl = ParameterBagUtils::getRequestParameterValue($request, $this->options['target_path_parameter'])) {
  92. return $targetUrl;
  93. }
  94. if (null !== $this->providerKey && $targetUrl = $request->getSession()->get('_security.'.$this->providerKey.'.target_path')) {
  95. $request->getSession()->remove('_security.'.$this->providerKey.'.target_path');
  96. return $targetUrl;
  97. }
  98. if ($this->options['use_referer'] && $targetUrl = $request->headers->get('Referer')) {
  99. if (false !== $pos = strpos($targetUrl, '?')) {
  100. $targetUrl = substr($targetUrl, 0, $pos);
  101. }
  102. if ($targetUrl && $targetUrl !== $this->httpUtils->generateUri($request, $this->options['login_path'])) {
  103. return $targetUrl;
  104. }
  105. }
  106. return $this->options['default_target_path'];
  107. }
  108. }