Skip to content

Commit 3e3b964

Browse files
[Security][SecurityBundle] Allow passing attributes to passport via Security::login()
1 parent 7776b9c commit 3e3b964

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

Authentication/AuthenticatorManager.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,19 @@ public function __construct(
6262
}
6363

6464
/**
65-
* @param BadgeInterface[] $badges Optionally, pass some Passport badges to use for the manual login
65+
* @param BadgeInterface[] $badges Optionally, pass some Passport badges to use for the manual login
66+
* @param array<string, mixed> $attributes Optionally, pass some Passport attributes to use for the manual login
6667
*/
67-
public function authenticateUser(UserInterface $user, AuthenticatorInterface $authenticator, Request $request, array $badges = []): ?Response
68+
public function authenticateUser(UserInterface $user, AuthenticatorInterface $authenticator, Request $request, array $badges = [] /* , array $attributes = [] */): ?Response
6869
{
70+
$attributes = 4 < \func_num_args() ? func_get_arg(4) : [];
71+
6972
// create an authentication token for the User
7073
$passport = new SelfValidatingPassport(new UserBadge($user->getUserIdentifier(), fn () => $user), $badges);
74+
foreach ($attributes as $k => $v) {
75+
$passport->setAttribute($k, $v);
76+
}
77+
7178
$token = $authenticator->createToken($passport, $this->firewallName);
7279

7380
// announce the authentication token

Authentication/UserAuthenticatorInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ interface UserAuthenticatorInterface
2626
* Convenience method to programmatically login a user and return a
2727
* Response *if any* for success.
2828
*
29-
* @param BadgeInterface[] $badges Optionally, pass some Passport badges to use for the manual login
29+
* @param BadgeInterface[] $badges Optionally, pass some Passport badges to use for the manual login
30+
* @param array<string, mixed> $attributes Optionally, pass some Passport attributes to use for the manual login
3031
*/
31-
public function authenticateUser(UserInterface $user, AuthenticatorInterface $authenticator, Request $request, array $badges = []): ?Response;
32+
public function authenticateUser(UserInterface $user, AuthenticatorInterface $authenticator, Request $request, array $badges = [] /* , array $attributes = [] */): ?Response;
3233
}

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* Pass the current token to the `checkPostAuth()` method of user checkers
88
* Deprecate argument `$secret` of `RememberMeAuthenticator`
99
* Deprecate passing an empty string as `$userIdentifier` argument to `UserBadge` constructor
10+
* Allow passing passport attributes to the `UserAuthenticatorInterface::authenticateUser()` method
1011

1112
7.1
1213
---

Tests/Authentication/AuthenticatorManagerTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,24 @@ public function testAuthenticateRequestCanModifyTokenFromEvent()
232232
public function testAuthenticateUser()
233233
{
234234
$authenticator = $this->createAuthenticator();
235-
$authenticator->expects($this->any())->method('createToken')->willReturn($this->token);
236235
$authenticator->expects($this->any())->method('onAuthenticationSuccess')->willReturn($this->response);
237236

237+
$badge = new UserBadge('alex');
238+
239+
$authenticator
240+
->expects($this->any())
241+
->method('createToken')
242+
->willReturnCallback(function (Passport $passport) use ($badge) {
243+
$this->assertSame(['attr' => 'foo', 'attr2' => 'bar'], $passport->getAttributes());
244+
$this->assertSame([UserBadge::class => $badge], $passport->getBadges());
245+
246+
return $this->token;
247+
});
248+
238249
$this->tokenStorage->expects($this->once())->method('setToken')->with($this->token);
239250

240251
$manager = $this->createManager([$authenticator]);
241-
$manager->authenticateUser($this->user, $authenticator, $this->request);
252+
$manager->authenticateUser($this->user, $authenticator, $this->request, [$badge], ['attr' => 'foo', 'attr2' => 'bar']);
242253
}
243254

244255
public function testAuthenticateUserCanModifyTokenFromEvent()

0 commit comments

Comments
 (0)