LogoutUrlGenerator.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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\Logout;
  11. use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderAdapter;
  12. use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  15. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  16. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  17. /**
  18. * Provides generator functions for the logout URL.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. * @author Jeremy Mikola <jmikola@gmail.com>
  22. */
  23. class LogoutUrlGenerator
  24. {
  25. private $requestStack;
  26. private $router;
  27. private $tokenStorage;
  28. private $listeners = array();
  29. public function __construct(RequestStack $requestStack = null, UrlGeneratorInterface $router = null, TokenStorageInterface $tokenStorage = null)
  30. {
  31. $this->requestStack = $requestStack;
  32. $this->router = $router;
  33. $this->tokenStorage = $tokenStorage;
  34. }
  35. /**
  36. * Registers a firewall's LogoutListener, allowing its URL to be generated.
  37. *
  38. * @param string $key The firewall key
  39. * @param string $logoutPath The path that starts the logout process
  40. * @param string $csrfTokenId The ID of the CSRF token
  41. * @param string $csrfParameter The CSRF token parameter name
  42. * @param CsrfTokenManagerInterface $csrfTokenManager A CsrfTokenManagerInterface instance
  43. */
  44. public function registerListener($key, $logoutPath, $csrfTokenId, $csrfParameter, $csrfTokenManager = null)
  45. {
  46. if ($csrfTokenManager instanceof CsrfProviderInterface) {
  47. $csrfTokenManager = new CsrfProviderAdapter($csrfTokenManager);
  48. } elseif (null !== $csrfTokenManager && !$csrfTokenManager instanceof CsrfTokenManagerInterface) {
  49. throw new \InvalidArgumentException('The CSRF token manager should be an instance of CsrfProviderInterface or CsrfTokenManagerInterface.');
  50. }
  51. $this->listeners[$key] = array($logoutPath, $csrfTokenId, $csrfParameter, $csrfTokenManager);
  52. }
  53. /**
  54. * Generates the absolute logout path for the firewall.
  55. *
  56. * @param string|null $key The firewall key or null to use the current firewall key
  57. *
  58. * @return string The logout path
  59. */
  60. public function getLogoutPath($key = null)
  61. {
  62. return $this->generateLogoutUrl($key, UrlGeneratorInterface::ABSOLUTE_PATH);
  63. }
  64. /**
  65. * Generates the absolute logout URL for the firewall.
  66. *
  67. * @param string|null $key The firewall key or null to use the current firewall key
  68. *
  69. * @return string The logout URL
  70. */
  71. public function getLogoutUrl($key = null)
  72. {
  73. return $this->generateLogoutUrl($key, UrlGeneratorInterface::ABSOLUTE_URL);
  74. }
  75. /**
  76. * Generates the logout URL for the firewall.
  77. *
  78. * @param string|null $key The firewall key or null to use the current firewall key
  79. * @param int $referenceType The type of reference (one of the constants in UrlGeneratorInterface)
  80. *
  81. * @return string The logout URL
  82. *
  83. * @throws \InvalidArgumentException if no LogoutListener is registered for the key or the key could not be found automatically
  84. */
  85. private function generateLogoutUrl($key, $referenceType)
  86. {
  87. // Fetch the current provider key from token, if possible
  88. if (null === $key && null !== $this->tokenStorage) {
  89. $token = $this->tokenStorage->getToken();
  90. if (null !== $token && method_exists($token, 'getProviderKey')) {
  91. $key = $token->getProviderKey();
  92. }
  93. }
  94. if (null === $key) {
  95. throw new \InvalidArgumentException('Unable to find the current firewall LogoutListener, please provide the provider key manually.');
  96. }
  97. if (!array_key_exists($key, $this->listeners)) {
  98. throw new \InvalidArgumentException(sprintf('No LogoutListener found for firewall key "%s".', $key));
  99. }
  100. list($logoutPath, $csrfTokenId, $csrfParameter, $csrfTokenManager) = $this->listeners[$key];
  101. if (null === $logoutPath) {
  102. throw new \LogicException('Unable to generate the logout URL without a path.');
  103. }
  104. $parameters = null !== $csrfTokenManager ? array($csrfParameter => (string) $csrfTokenManager->getToken($csrfTokenId)) : array();
  105. if ('/' === $logoutPath[0]) {
  106. if (!$this->requestStack) {
  107. throw new \LogicException('Unable to generate the logout URL without a RequestStack.');
  108. }
  109. $request = $this->requestStack->getCurrentRequest();
  110. $url = UrlGeneratorInterface::ABSOLUTE_URL === $referenceType ? $request->getUriForPath($logoutPath) : $request->getBaseUrl().$logoutPath;
  111. if (!empty($parameters)) {
  112. $url .= '?'.http_build_query($parameters, '', '&');
  113. }
  114. } else {
  115. if (!$this->router) {
  116. throw new \LogicException('Unable to generate the logout URL without a Router.');
  117. }
  118. $url = $this->router->generate($logoutPath, $parameters, $referenceType);
  119. }
  120. return $url;
  121. }
  122. }