Skip to content

Commit

Permalink
feature #48044 [SecurityBundle] Set request stateless when firewall i…
Browse files Browse the repository at this point in the history
…s 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
  • Loading branch information
fabpot committed Dec 18, 2022
2 parents c6c300c + ce458c6 commit cb6f2c5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Bundle/SecurityBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

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

6.2
---
Expand Down
9 changes: 8 additions & 1 deletion src/Symfony/Bundle/SecurityBundle/Security/FirewallMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,14 @@ private function getFirewallContext(Request $request): ?FirewallContext
if (null === $requestMatcher || $requestMatcher->matches($request)) {
$request->attributes->set('_firewall_context', $contextId);

return $this->container->get($contextId);
/** @var FirewallContext $context */
$context = $this->container->get($contextId);

if ($context->getConfig()?->isStateless()) {
$request->attributes->set('_stateless', true);
}

return $context;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function testGetListenersWithInvalidParameter()
$this->assertEquals([[], null, null], $firewallMap->getListeners($request));
$this->assertNull($firewallMap->getFirewallConfig($request));
$this->assertFalse($request->attributes->has(self::ATTRIBUTE_FIREWALL_CONTEXT));
$this->assertFalse($request->attributes->has('_stateless'));
}

public function testGetListeners()
Expand All @@ -62,8 +63,8 @@ public function testGetListeners()

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

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

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

0 comments on commit cb6f2c5

Please sign in to comment.