RetryAuthenticationEntryPoint.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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\EntryPoint;
  11. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  12. use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. /**
  16. * RetryAuthenticationEntryPoint redirects URL based on the configured scheme.
  17. *
  18. * This entry point is not intended to work with HTTP post requests.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. */
  22. class RetryAuthenticationEntryPoint implements AuthenticationEntryPointInterface
  23. {
  24. private $httpPort;
  25. private $httpsPort;
  26. public function __construct($httpPort = 80, $httpsPort = 443)
  27. {
  28. $this->httpPort = $httpPort;
  29. $this->httpsPort = $httpsPort;
  30. }
  31. public function start(Request $request, AuthenticationException $authException = null)
  32. {
  33. $scheme = $request->isSecure() ? 'http' : 'https';
  34. if ('http' === $scheme && 80 != $this->httpPort) {
  35. $port = ':'.$this->httpPort;
  36. } elseif ('https' === $scheme && 443 != $this->httpsPort) {
  37. $port = ':'.$this->httpsPort;
  38. } else {
  39. $port = '';
  40. }
  41. $qs = $request->getQueryString();
  42. if (null !== $qs) {
  43. $qs = '?'.$qs;
  44. }
  45. $url = $scheme.'://'.$request->getHost().$port.$request->getBaseUrl().$request->getPathInfo().$qs;
  46. return new RedirectResponse($url, 301);
  47. }
  48. }