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