Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cb6f2c5

Browse files
committed
feature #48044 [SecurityBundle] Set request stateless when firewall is stateless (alamirault)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [SecurityBundle] Set request stateless when firewall is stateless | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #40372 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Automatically add `_stateless` attribute to the request when firewall is stateless Commits ------- ce458c6 [SecurityBundle] Set request stateless when firewall is stateless
2 parents c6c300c + ce458c6 commit cb6f2c5

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

src/Symfony/Bundle/SecurityBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Deprecate enabling bundle and not configuring it
8+
* Add `_stateless` attribute to the request when firewall is stateless
89

910
6.2
1011
---

src/Symfony/Bundle/SecurityBundle/Security/FirewallMap.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,14 @@ private function getFirewallContext(Request $request): ?FirewallContext
7272
if (null === $requestMatcher || $requestMatcher->matches($request)) {
7373
$request->attributes->set('_firewall_context', $contextId);
7474

75-
return $this->container->get($contextId);
75+
/** @var FirewallContext $context */
76+
$context = $this->container->get($contextId);
77+
78+
if ($context->getConfig()?->isStateless()) {
79+
$request->attributes->set('_stateless', true);
80+
}
81+
82+
return $context;
7683
}
7784
}
7885

src/Symfony/Bundle/SecurityBundle/Tests/Security/FirewallMapTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function testGetListenersWithInvalidParameter()
5454
$this->assertEquals([[], null, null], $firewallMap->getListeners($request));
5555
$this->assertNull($firewallMap->getFirewallConfig($request));
5656
$this->assertFalse($request->attributes->has(self::ATTRIBUTE_FIREWALL_CONTEXT));
57+
$this->assertFalse($request->attributes->has('_stateless'));
5758
}
5859

5960
public function testGetListeners()
@@ -62,8 +63,8 @@ public function testGetListeners()
6263

6364
$firewallContext = $this->createMock(FirewallContext::class);
6465

65-
$firewallConfig = new FirewallConfig('main', 'user_checker');
66-
$firewallContext->expects($this->once())->method('getConfig')->willReturn($firewallConfig);
66+
$firewallConfig = new FirewallConfig('main', 'user_checker', null, true, true);
67+
$firewallContext->expects($this->exactly(2))->method('getConfig')->willReturn($firewallConfig);
6768

6869
$listener = function () {};
6970
$firewallContext->expects($this->once())->method('getListeners')->willReturn([$listener]);
@@ -88,5 +89,6 @@ public function testGetListeners()
8889
$this->assertEquals([[$listener], $exceptionListener, $logoutListener], $firewallMap->getListeners($request));
8990
$this->assertEquals($firewallConfig, $firewallMap->getFirewallConfig($request));
9091
$this->assertEquals('security.firewall.map.context.foo', $request->attributes->get(self::ATTRIBUTE_FIREWALL_CONTEXT));
92+
$this->assertTrue($request->attributes->get('_stateless'));
9193
}
9294
}

0 commit comments

Comments
 (0)