Skip to content

Commit 2972115

Browse files
[Security][SecurityBundle] Allow passing attributes to passport via Security::login()
1 parent 287f2a2 commit 2972115

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Allow configuring the secret used to sign login links
8+
* Allow passing optional passport attributes to `Security::login()`
89

910
7.1
1011
---

Diff for: Security.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,15 @@ public function getFirewallConfig(Request $request): ?FirewallConfig
7474
}
7575

7676
/**
77-
* @param UserInterface $user The user to authenticate
78-
* @param string|null $authenticatorName The authenticator name (e.g. "form_login") or service id (e.g. SomeApiKeyAuthenticator::class) - required only if multiple authenticators are configured
79-
* @param string|null $firewallName The firewall name - required only if multiple firewalls are configured
80-
* @param BadgeInterface[] $badges Badges to add to the user's passport
77+
* @param UserInterface $user The user to authenticate
78+
* @param string|null $authenticatorName The authenticator name (e.g. "form_login") or service id (e.g. SomeApiKeyAuthenticator::class) - required only if multiple authenticators are configured
79+
* @param string|null $firewallName The firewall name - required only if multiple firewalls are configured
80+
* @param BadgeInterface[] $badges Badges to add to the user's passport
81+
* @param array<string, mixed> $attributes Attributes to add to the user's passport
8182
*
8283
* @return Response|null The authenticator success response if any
8384
*/
84-
public function login(UserInterface $user, ?string $authenticatorName = null, ?string $firewallName = null, array $badges = []): ?Response
85+
public function login(UserInterface $user, ?string $authenticatorName = null, ?string $firewallName = null, array $badges = [], array $attributes = []): ?Response
8586
{
8687
$request = $this->container->get('request_stack')->getCurrentRequest();
8788
if (null === $request) {
@@ -99,7 +100,7 @@ public function login(UserInterface $user, ?string $authenticatorName = null, ?s
99100
$userCheckerLocator = $this->container->get('security.user_checker_locator');
100101
$userCheckerLocator->get($firewallName)->checkPreAuth($user);
101102

102-
return $this->container->get('security.authenticator.managers_locator')->get($firewallName)->authenticateUser($user, $authenticator, $request, $badges);
103+
return $this->container->get('security.authenticator.managers_locator')->get($firewallName)->authenticateUser($user, $authenticator, $request, $badges, $attributes);
103104
}
104105

105106
/**

Diff for: Security/UserAuthenticator.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public function __construct(FirewallMap $firewallMap, ContainerInterface $userAu
3838
$this->requestStack = $requestStack;
3939
}
4040

41-
public function authenticateUser(UserInterface $user, AuthenticatorInterface $authenticator, Request $request, array $badges = []): ?Response
41+
public function authenticateUser(UserInterface $user, AuthenticatorInterface $authenticator, Request $request, array $badges = [], array $attributes = []): ?Response
4242
{
43-
return $this->getForFirewall()->authenticateUser($user, $authenticator, $request, $badges);
43+
return $this->getForFirewall()->authenticateUser($user, $authenticator, $request, $badges, $attributes);
4444
}
4545
}

Diff for: Tests/SecurityTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
3434
use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
3535
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
36+
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
3637
use Symfony\Component\Security\Http\Event\LogoutEvent;
3738
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
3839
use Symfony\Contracts\Service\ServiceProviderInterface;
@@ -135,6 +136,7 @@ public function testLogin()
135136
$userAuthenticator = $this->createMock(UserAuthenticatorInterface::class);
136137
$user = $this->createMock(UserInterface::class);
137138
$userChecker = $this->createMock(UserCheckerInterface::class);
139+
$badge = new UserBadge('foo');
138140

139141
$container = new Container();
140142
$container->set('request_stack', $requestStack);
@@ -143,7 +145,7 @@ public function testLogin()
143145
$container->set('security.user_checker_locator', $this->createContainer('main', $userChecker));
144146

145147
$firewallMap->expects($this->once())->method('getFirewallConfig')->willReturn($firewall);
146-
$userAuthenticator->expects($this->once())->method('authenticateUser')->with($user, $authenticator, $request);
148+
$userAuthenticator->expects($this->once())->method('authenticateUser')->with($user, $authenticator, $request, [$badge], ['foo' => 'bar']);
147149
$userChecker->expects($this->once())->method('checkPreAuth')->with($user);
148150

149151
$firewallAuthenticatorLocator = $this->createMock(ServiceProviderInterface::class);
@@ -161,7 +163,7 @@ public function testLogin()
161163

162164
$security = new Security($container, ['main' => $firewallAuthenticatorLocator]);
163165

164-
$security->login($user);
166+
$security->login($user, badges: [$badge], attributes: ['foo' => 'bar']);
165167
}
166168

167169
public function testLoginReturnsAuthenticatorResponse()

0 commit comments

Comments
 (0)