FormAuthenticationEntryPoint.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 string $loginPath The path to the login form
  28. * @param bool $useForward Whether to forward or redirect to the login form
  29. */
  30. public function __construct(HttpKernelInterface $kernel, HttpUtils $httpUtils, string $loginPath, bool $useForward = false)
  31. {
  32. $this->httpKernel = $kernel;
  33. $this->httpUtils = $httpUtils;
  34. $this->loginPath = $loginPath;
  35. $this->useForward = $useForward;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function start(Request $request, AuthenticationException $authException = null)
  41. {
  42. if ($this->useForward) {
  43. $subRequest = $this->httpUtils->createRequest($request, $this->loginPath);
  44. $response = $this->httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
  45. if (200 === $response->getStatusCode()) {
  46. $response->setStatusCode(401);
  47. }
  48. return $response;
  49. }
  50. return $this->httpUtils->createRedirectResponse($request, $this->loginPath);
  51. }
  52. }