vendor/symfony/security-http/Firewall.php line 50

  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;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
  14. use Symfony\Component\HttpKernel\Event\RequestEvent;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. use Symfony\Component\Security\Http\Firewall\ExceptionListener;
  17. use Symfony\Component\Security\Http\Firewall\FirewallListenerInterface;
  18. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  19. /**
  20.  * Firewall uses a FirewallMap to register security listeners for the given
  21.  * request.
  22.  *
  23.  * It allows for different security strategies within the same application
  24.  * (a Basic authentication for the /api, and a web based authentication for
  25.  * everything else for instance).
  26.  *
  27.  * @author Fabien Potencier <fabien@symfony.com>
  28.  */
  29. class Firewall implements EventSubscriberInterface
  30. {
  31.     private FirewallMapInterface $map;
  32.     private EventDispatcherInterface $dispatcher;
  33.     /**
  34.      * @var \SplObjectStorage<Request, ExceptionListener>
  35.      */
  36.     private \SplObjectStorage $exceptionListeners;
  37.     public function __construct(FirewallMapInterface $mapEventDispatcherInterface $dispatcher)
  38.     {
  39.         $this->map $map;
  40.         $this->dispatcher $dispatcher;
  41.         $this->exceptionListeners = new \SplObjectStorage();
  42.     }
  43.     /**
  44.      * @return void
  45.      */
  46.     public function onKernelRequest(RequestEvent $event)
  47.     {
  48.         if (!$event->isMainRequest()) {
  49.             return;
  50.         }
  51.         // register listeners for this firewall
  52.         $listeners $this->map->getListeners($event->getRequest());
  53.         $authenticationListeners $listeners[0];
  54.         $exceptionListener $listeners[1];
  55.         $logoutListener $listeners[2];
  56.         if (null !== $exceptionListener) {
  57.             $this->exceptionListeners[$event->getRequest()] = $exceptionListener;
  58.             $exceptionListener->register($this->dispatcher);
  59.         }
  60.         // Authentication listeners are pre-sorted by SortFirewallListenersPass
  61.         $authenticationListeners = function () use ($authenticationListeners$logoutListener) {
  62.             if (null !== $logoutListener) {
  63.                 $logoutListenerPriority $this->getListenerPriority($logoutListener);
  64.             }
  65.             foreach ($authenticationListeners as $listener) {
  66.                 $listenerPriority $this->getListenerPriority($listener);
  67.                 // Yielding the LogoutListener at the correct position
  68.                 if (null !== $logoutListener && $listenerPriority $logoutListenerPriority) {
  69.                     yield $logoutListener;
  70.                     $logoutListener null;
  71.                 }
  72.                 yield $listener;
  73.             }
  74.             // When LogoutListener has the lowest priority of all listeners
  75.             if (null !== $logoutListener) {
  76.                 yield $logoutListener;
  77.             }
  78.         };
  79.         $this->callListeners($event$authenticationListeners());
  80.     }
  81.     /**
  82.      * @return void
  83.      */
  84.     public function onKernelFinishRequest(FinishRequestEvent $event)
  85.     {
  86.         $request $event->getRequest();
  87.         if (isset($this->exceptionListeners[$request])) {
  88.             $this->exceptionListeners[$request]->unregister($this->dispatcher);
  89.             unset($this->exceptionListeners[$request]);
  90.         }
  91.     }
  92.     /**
  93.      * @return array
  94.      */
  95.     public static function getSubscribedEvents()
  96.     {
  97.         return [
  98.             KernelEvents::REQUEST => ['onKernelRequest'8],
  99.             KernelEvents::FINISH_REQUEST => 'onKernelFinishRequest',
  100.         ];
  101.     }
  102.     /**
  103.      * @return void
  104.      */
  105.     protected function callListeners(RequestEvent $eventiterable $listeners)
  106.     {
  107.         foreach ($listeners as $listener) {
  108.             $listener($event);
  109.             if ($event->hasResponse()) {
  110.                 break;
  111.             }
  112.         }
  113.     }
  114.     private function getListenerPriority(object $logoutListener): int
  115.     {
  116.         return $logoutListener instanceof FirewallListenerInterface $logoutListener->getPriority() : 0;
  117.     }
  118. }