|
15 | 15 | use Symfony\Component\HttpKernel\Event\FinishRequestEvent; |
16 | 16 | use Symfony\Component\HttpKernel\Event\RequestEvent; |
17 | 17 | use Symfony\Component\HttpKernel\KernelEvents; |
| 18 | +use Symfony\Component\Security\Http\Firewall\FirewallListenerInterface; |
18 | 19 | use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
19 | 20 |
|
20 | 21 | /** |
@@ -51,13 +52,38 @@ public function onKernelRequest(RequestEvent $event) |
51 | 52 |
|
52 | 53 | $authenticationListeners = $listeners[0]; |
53 | 54 | $exceptionListener = $listeners[1]; |
| 55 | + $logoutListener = $listeners[2]; |
54 | 56 |
|
55 | 57 | if (null !== $exceptionListener) { |
56 | 58 | $this->exceptionListeners[$event->getRequest()] = $exceptionListener; |
57 | 59 | $exceptionListener->register($this->dispatcher); |
58 | 60 | } |
59 | 61 |
|
60 | | - $this->callListeners($event, $authenticationListeners); |
| 62 | + // Authentication listeners are pre-sorted by SortFirewallListenersPass |
| 63 | + $authenticationListeners = function () use ($authenticationListeners, $logoutListener) { |
| 64 | + if (null !== $logoutListener) { |
| 65 | + $logoutListenerPriority = $this->getListenerPriority($logoutListener); |
| 66 | + } |
| 67 | + |
| 68 | + foreach ($authenticationListeners as $listener) { |
| 69 | + $listenerPriority = $this->getListenerPriority($listener); |
| 70 | + |
| 71 | + // Yielding the LogoutListener at the correct position |
| 72 | + if (null !== $logoutListener && $listenerPriority < $logoutListenerPriority) { |
| 73 | + yield $logoutListener; |
| 74 | + $logoutListener = null; |
| 75 | + } |
| 76 | + |
| 77 | + yield $listener; |
| 78 | + } |
| 79 | + |
| 80 | + // When LogoutListener has the lowest priority of all listeners |
| 81 | + if (null !== $logoutListener) { |
| 82 | + yield $logoutListener; |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + $this->callListeners($event, $authenticationListeners()); |
61 | 87 | } |
62 | 88 |
|
63 | 89 | public function onKernelFinishRequest(FinishRequestEvent $event) |
@@ -91,4 +117,9 @@ protected function callListeners(RequestEvent $event, iterable $listeners) |
91 | 117 | } |
92 | 118 | } |
93 | 119 | } |
| 120 | + |
| 121 | + private function getListenerPriority(object $logoutListener): int |
| 122 | + { |
| 123 | + return $logoutListener instanceof FirewallListenerInterface ? $logoutListener->getPriority() : 0; |
| 124 | + } |
94 | 125 | } |
0 commit comments