FormAuthenticationEntryPoint.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\HttpKernelInterface;
  13. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  14. use Symfony\Component\Security\Http\HttpUtils;
  15. /**
  16. * FormAuthenticationEntryPoint starts an authentication via a login form.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class FormAuthenticationEntryPoint implements AuthenticationEntryPointInterface
  21. {
  22. private $loginPath;
  23. private $useForward;
  24. private $httpKernel;
  25. private $httpUtils;
  26. /**
  27. * @param HttpKernelInterface $kernel
  28. * @param HttpUtils $httpUtils An HttpUtils instance
  29. * @param string $loginPath The path to the login form
  30. * @param bool $useForward Whether to forward or redirect to the login form
  31. */
  32. public function __construct(HttpKernelInterface $kernel, HttpUtils $httpUtils, $loginPath, $useForward = false)
  33. {
  34. $this->httpKernel = $kernel;
  35. $this->httpUtils = $httpUtils;
  36. $this->loginPath = $loginPath;
  37. $this->useForward = (bool) $useForward;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function start(Request $request, AuthenticationException $authException = null)
  43. {
  44. if ($this->useForward) {
  45. $subRequest = $this->httpUtils->createRequest($request, $this->loginPath);
  46. $response = $this->httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
  47. if (200 === $response->getStatusCode()) {
  48. $response->headers->set('X-Status-Code', 401);
  49. }
  50. return $response;
  51. }
  52. return $this->httpUtils->createRedirectResponse($request, $this->loginPath);
  53. }
  54. }