DefaultAuthenticationSuccessHandler.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Security\Http\HttpUtils;
  14. /**
  15. * Class with the default authentication success handling logic.
  16. *
  17. * Can be optionally be extended from by the developer to alter the behaviour
  18. * while keeping the default behaviour.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  22. * @author Alexander <iam.asm89@gmail.com>
  23. */
  24. class DefaultAuthenticationSuccessHandler implements AuthenticationSuccessHandlerInterface
  25. {
  26. protected $httpUtils;
  27. protected $options;
  28. protected $providerKey;
  29. /**
  30. * Constructor.
  31. *
  32. * @param HttpUtils $httpUtils
  33. * @param array $options Options for processing a successful authentication attempt.
  34. */
  35. public function __construct(HttpUtils $httpUtils, array $options)
  36. {
  37. $this->httpUtils = $httpUtils;
  38. $this->options = array_merge(array(
  39. 'always_use_default_target_path' => false,
  40. 'default_target_path' => '/',
  41. 'login_path' => '/login',
  42. 'target_path_parameter' => '_target_path',
  43. 'use_referer' => false,
  44. ), $options);
  45. }
  46. /**
  47. * {@inheritDoc}
  48. */
  49. public function onAuthenticationSuccess(Request $request, TokenInterface $token)
  50. {
  51. return $this->httpUtils->createRedirectResponse($request, $this->determineTargetUrl($request));
  52. }
  53. /**
  54. * Get the provider key.
  55. *
  56. * @return string
  57. */
  58. public function getProviderKey()
  59. {
  60. return $this->providerKey;
  61. }
  62. /**
  63. * Set the provider key.
  64. *
  65. * @param string $providerKey
  66. */
  67. public function setProviderKey($providerKey)
  68. {
  69. $this->providerKey = $providerKey;
  70. }
  71. /**
  72. * Builds the target URL according to the defined options.
  73. *
  74. * @param Request $request
  75. *
  76. * @return string
  77. */
  78. protected function determineTargetUrl(Request $request)
  79. {
  80. if ($this->options['always_use_default_target_path']) {
  81. return $this->options['default_target_path'];
  82. }
  83. if ($targetUrl = $request->get($this->options['target_path_parameter'], null, true)) {
  84. return $targetUrl;
  85. }
  86. if (null !== $this->providerKey && $targetUrl = $request->getSession()->get('_security.'.$this->providerKey.'.target_path')) {
  87. $request->getSession()->remove('_security.'.$this->providerKey.'.target_path');
  88. return $targetUrl;
  89. }
  90. if ($this->options['use_referer'] && ($targetUrl = $request->headers->get('Referer')) && $targetUrl !== $this->httpUtils->generateUri($request, $this->options['login_path'])) {
  91. return $targetUrl;
  92. }
  93. return $this->options['default_target_path'];
  94. }
  95. }