vendor/symfony/security-bundle/EventListener/FirewallListener.php line 39

  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\Bundle\SecurityBundle\EventListener;
  11. use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
  12. use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
  13. use Symfony\Component\HttpKernel\Event\RequestEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. use Symfony\Component\Security\Http\Firewall;
  16. use Symfony\Component\Security\Http\FirewallMapInterface;
  17. use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
  18. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  19. /**
  20. * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
  21. */
  22. class FirewallListener extends Firewall
  23. {
  24. private FirewallMapInterface $map;
  25. private LogoutUrlGenerator $logoutUrlGenerator;
  26. public function __construct(FirewallMapInterface $map, EventDispatcherInterface $dispatcher, LogoutUrlGenerator $logoutUrlGenerator)
  27. {
  28. $this->map = $map;
  29. $this->logoutUrlGenerator = $logoutUrlGenerator;
  30. parent::__construct($map, $dispatcher);
  31. }
  32. /**
  33. * @return void
  34. */
  35. public function configureLogoutUrlGenerator(RequestEvent $event)
  36. {
  37. if (!$event->isMainRequest()) {
  38. return;
  39. }
  40. if ($this->map instanceof FirewallMap && $config = $this->map->getFirewallConfig($event->getRequest())) {
  41. $this->logoutUrlGenerator->setCurrentFirewall($config->getName(), $config->getContext());
  42. }
  43. }
  44. /**
  45. * @return void
  46. */
  47. public function onKernelFinishRequest(FinishRequestEvent $event)
  48. {
  49. if ($event->isMainRequest()) {
  50. $this->logoutUrlGenerator->setCurrentFirewall(null);
  51. }
  52. parent::onKernelFinishRequest($event);
  53. }
  54. public static function getSubscribedEvents(): array
  55. {
  56. return [
  57. KernelEvents::REQUEST => [
  58. ['configureLogoutUrlGenerator', 8],
  59. ['onKernelRequest', 8],
  60. ],
  61. KernelEvents::FINISH_REQUEST => 'onKernelFinishRequest',
  62. ];
  63. }
  64. }