Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit891e58e

Browse files
committed
[Security] Deprecate callable firewall listeners
1 parentf8e605c commit891e58e

File tree

7 files changed

+116
-21
lines changed

7 files changed

+116
-21
lines changed

‎UPGRADE-7.4.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
UPGRADE FROM 7.3 to 7.4
2+
=======================
3+
4+
Symfony 7.4 is a minor release. According to the Symfony release process, there should be no significant
5+
backward compatibility breaks. Minor backward compatibility breaks are prefixed in this document with
6+
`[BC BREAK]`, make sure your code is compatible with these entries before upgrading.
7+
Read more about this in the[Symfony documentation](https://symfony.com/doc/7.4/setup/upgrade_minor.html).
8+
9+
If you're upgrading from a version below 7.3, follow the[7.3 upgrade guide](UPGRADE-7.3.md) first.
10+
11+
Security
12+
--------
13+
14+
* Deprecate callable firewall listeners, extend`AbstractListener` instead

‎src/Symfony/Bundle/SecurityBundle/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.4
5+
---
6+
7+
* Deprecate callable firewall listeners, extend`AbstractListener` instead
8+
49
7.3
510
---
611

‎src/Symfony/Bundle/SecurityBundle/Security/LazyFirewallContext.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespaceSymfony\Bundle\SecurityBundle\Security;
1313

14+
useSymfony\Component\HttpFoundation\Request;
1415
useSymfony\Component\HttpKernel\Event\RequestEvent;
1516
useSymfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
1617
useSymfony\Component\Security\Http\Event\LazyResponseEvent;
@@ -23,7 +24,7 @@
2324
*
2425
* @author Nicolas Grekas <p@tchwork.com>
2526
*/
26-
class LazyFirewallContextextends FirewallContext
27+
class LazyFirewallContextextends FirewallContextimplements FirewallListenerInterface
2728
{
2829
publicfunction__construct(
2930
iterable$listeners,
@@ -40,7 +41,22 @@ public function getListeners(): iterable
4041
return [$this];
4142
}
4243

43-
publicfunction__invoke(RequestEvent$event):void
44+
publicfunctionsupports(Request$request): ?bool
45+
{
46+
foreach (parent::getListeners()as$listener) {
47+
if (!$listenerinstanceof FirewallListenerInterface) {
48+
returntrue;
49+
}
50+
51+
if (false !==$listener->supports($request)) {
52+
returntrue;
53+
}
54+
}
55+
56+
returnfalse;
57+
}
58+
59+
publicfunctionauthenticate(RequestEvent$event):void
4460
{
4561
$listeners = [];
4662
$request =$event->getRequest();
@@ -75,4 +91,14 @@ public function __invoke(RequestEvent $event): void
7591
}
7692
});
7793
}
94+
95+
publicstaticfunctiongetPriority():int
96+
{
97+
return0;
98+
}
99+
100+
publicfunction__invoke(RequestEvent$event):void
101+
{
102+
$this->authenticate($event);
103+
}
78104
}

‎src/Symfony/Bundle/SecurityBundle/Tests/DataCollector/SecurityDataCollectorTest.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
useSymfony\Component\Security\Core\Authorization\Voter\VoterInterface;
3333
useSymfony\Component\Security\Core\Role\RoleHierarchy;
3434
useSymfony\Component\Security\Core\User\InMemoryUser;
35+
useSymfony\Component\Security\Http\Firewall\FirewallListenerInterface;
3536
useSymfony\Component\Security\Http\FirewallMapInterface;
3637
useSymfony\Component\Security\Http\Logout\LogoutUrlGenerator;
3738
useSymfony\Component\VarDumper\Caster\ClassStub;
@@ -193,8 +194,24 @@ public function testGetListeners()
193194
$request =newRequest();
194195
$event =newRequestEvent($this->createMock(HttpKernelInterface::class),$request, HttpKernelInterface::MAIN_REQUEST);
195196
$event->setResponse($response =newResponse());
196-
$listener =function ($e)use ($event, &$listenerCalled) {
197-
$listenerCalled +=$e ===$event;
197+
$listener =newclassimplements FirewallListenerInterface
198+
{
199+
publicint$callCount =0;
200+
201+
publicfunctionsupports(Request$request): ?bool
202+
{
203+
returntrue;
204+
}
205+
206+
publicfunctionauthenticate(RequestEvent$event):void
207+
{
208+
++$this->callCount;
209+
}
210+
211+
publicstaticfunctiongetPriority():int
212+
{
213+
return0;
214+
}
198215
};
199216
$firewallMap =$this
200217
->getMockBuilder(FirewallMap::class)
@@ -217,9 +234,9 @@ public function testGetListeners()
217234
$collector =newSecurityDataCollector(null,null,null,null,$firewallMap,$firewall,true);
218235
$collector->collect($request,$response);
219236

220-
$this->assertNotEmpty($collected =$collector->getListeners()[0]);
237+
$this->assertCount(1,$collector->getListeners());
221238
$collector->lateCollect();
222-
$this->assertSame(1,$listenerCalled);
239+
$this->assertSame(1,$listener->callCount);
223240
}
224241

225242
publicfunctiontestCollectCollectsDecisionLogWhenStrategyIsAffirmative()

‎src/Symfony/Bundle/SecurityBundle/Tests/Debug/TraceableFirewallListenerTest.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
useSymfony\Component\Security\Http\Authenticator\Passport\Passport;
3131
useSymfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
3232
useSymfony\Component\Security\Http\Firewall\AuthenticatorManagerListener;
33+
useSymfony\Component\Security\Http\Firewall\FirewallListenerInterface;
3334
useSymfony\Component\Security\Http\Logout\LogoutUrlGenerator;
3435

3536
/**
@@ -41,9 +42,25 @@ public function testOnKernelRequestRecordsListeners()
4142
{
4243
$request =newRequest();
4344
$event =newRequestEvent($this->createMock(HttpKernelInterface::class),$request, HttpKernelInterface::MAIN_REQUEST);
44-
$event->setResponse($response =newResponse());
45-
$listener =function ($e)use ($event, &$listenerCalled) {
46-
$listenerCalled +=$e ===$event;
45+
$event->setResponse(newResponse());
46+
$listener =newclassimplements FirewallListenerInterface
47+
{
48+
publicint$callCount =0;
49+
50+
publicfunctionsupports(Request$request): ?bool
51+
{
52+
returntrue;
53+
}
54+
55+
publicfunctionauthenticate(RequestEvent$event):void
56+
{
57+
++$this->callCount;
58+
}
59+
60+
publicstaticfunctiongetPriority():int
61+
{
62+
return0;
63+
}
4764
};
4865
$firewallMap =$this->createMock(FirewallMap::class);
4966
$firewallMap

‎src/Symfony/Component/Security/Http/Firewall.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
useSymfony\Component\HttpKernel\Event\FinishRequestEvent;
1717
useSymfony\Component\HttpKernel\Event\RequestEvent;
1818
useSymfony\Component\HttpKernel\KernelEvents;
19+
useSymfony\Component\Security\Http\Firewall\AbstractListener;
1920
useSymfony\Component\Security\Http\Firewall\ExceptionListener;
2021
useSymfony\Component\Security\Http\Firewall\FirewallListenerInterface;
2122
useSymfony\Contracts\EventDispatcher\EventDispatcherInterface;
@@ -122,6 +123,10 @@ public static function getSubscribedEvents()
122123
protectedfunctioncallListeners(RequestEvent$event,iterable$listeners)
123124
{
124125
foreach ($listenersas$listener) {
126+
if (!$listenerinstanceof FirewallListenerInterface) {
127+
trigger_deprecation('symfony/security-http','7.4','Using a callable as firewall listener is deprecated, extend "%s" instead.', AbstractListener::class);
128+
}
129+
125130
$listener($event);
126131

127132
if ($event->hasResponse()) {
@@ -130,8 +135,8 @@ protected function callListeners(RequestEvent $event, iterable $listeners)
130135
}
131136
}
132137

133-
privatefunctiongetListenerPriority(object$logoutListener):int
138+
privatefunctiongetListenerPriority(object$listener):int
134139
{
135-
return$logoutListenerinstanceof FirewallListenerInterface ?$logoutListener->getPriority() :0;
140+
return$listenerinstanceof FirewallListenerInterface ?$listener->getPriority() :0;
136141
}
137142
}

‎src/Symfony/Component/Security/Http/Tests/FirewallTest.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
useSymfony\Component\HttpKernel\Event\RequestEvent;
1919
useSymfony\Component\HttpKernel\HttpKernelInterface;
2020
useSymfony\Component\Security\Http\Firewall;
21+
useSymfony\Component\Security\Http\Firewall\AbstractListener;
2122
useSymfony\Component\Security\Http\Firewall\ExceptionListener;
2223
useSymfony\Component\Security\Http\FirewallMapInterface;
2324

@@ -52,21 +53,31 @@ public function testOnKernelRequestRegistersExceptionListener()
5253

5354
publicfunctiontestOnKernelRequestStopsWhenThereIsAResponse()
5455
{
55-
$called = [];
56-
57-
$first =function ()use (&$called) {
58-
$called[] =1;
59-
};
60-
61-
$second =function ()use (&$called) {
62-
$called[] =2;
56+
$listener =newclassextends AbstractListener
57+
{
58+
publicint$callCount =0;
59+
60+
publicfunctionsupports(Request$request): ?bool
61+
{
62+
returntrue;
63+
}
64+
65+
publicfunctionauthenticate(RequestEvent$event):void
66+
{
67+
++$this->callCount;
68+
}
69+
70+
publicstaticfunctiongetPriority():int
71+
{
72+
return0;
73+
}
6374
};
6475

6576
$map =$this->createMock(FirewallMapInterface::class);
6677
$map
6778
->expects($this->once())
6879
->method('getListeners')
69-
->willReturn([[$first,$second],null,null])
80+
->willReturn([[$listener,$listener],null,null])
7081
;
7182

7283
$event =newRequestEvent($this->createMock(HttpKernelInterface::class),newRequest(), HttpKernelInterface::MAIN_REQUEST);
@@ -75,7 +86,7 @@ public function testOnKernelRequestStopsWhenThereIsAResponse()
7586
$firewall =newFirewall($map,$this->createMock(EventDispatcherInterface::class));
7687
$firewall->onKernelRequest($event);
7788

78-
$this->assertSame([1],$called);
89+
$this->assertSame(1,$listener->callCount);
7990
}
8091

8192
publicfunctiontestOnKernelRequestWithSubRequest()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp