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

Skip to content

Commit fc2777f

Browse files
committed
feature #51638 [FrameworkBundle] [Test] add token attributes in KernelBrowser::loginUser() (Valmonzo)
This PR was merged into the 6.4 branch. Discussion ---------- [FrameworkBundle] [Test] add token attributes in `KernelBrowser::loginUser()` | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | yes | Deprecations? | yes | Tickets | ~ | License | MIT | Doc PR | TODO We have an advanced use case where we define custom attributes in tokens from success handlers. Then, we use them for access controls. Currently we cannot define those attributes when testing and using the `loginUser()` method, and so we needed to copy paste the whole logic to do it. What do you think about this feature? I did not add tests because the method does not have any yet, should we add some? It's my first contribution, thanks to my brother `@HeahDude` for his help 🙏🏽 Commits ------- 1d38977 [FrameworkBundle][Test]: add token attributes in `KernelBrowser::loginUser()`
2 parents 7c833ee + 1d38977 commit fc2777f

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ CHANGELOG
2424
* Deprecate not setting the `framework.validation.email_validation_mode` config option; it will default to `html5` in 7.0
2525
* Deprecate `framework.validation.enable_annotations`, use `framework.validation.enable_attributes` instead
2626
* Deprecate `framework.serializer.enable_annotations`, use `framework.serializer.enable_attributes` instead
27+
* Add `array $tokenAttributes = []` optional parameter to `KernelBrowser::loginUser()`
2728

2829
6.3
2930
---

src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,15 @@ public function enableReboot()
107107
}
108108

109109
/**
110-
* @param UserInterface $user
110+
* @param UserInterface $user
111+
* @param array<string, mixed> $tokenAttributes
111112
*
112113
* @return $this
113114
*/
114-
public function loginUser(object $user, string $firewallContext = 'main'): static
115+
public function loginUser(object $user, string $firewallContext = 'main'/* , array $tokenAttributes = [] */): static
115116
{
117+
$tokenAttributes = 2 < \func_num_args() ? func_get_arg(2) : [];
118+
116119
if (!interface_exists(UserInterface::class)) {
117120
throw new \LogicException(sprintf('"%s" requires symfony/security-core to be installed. Try running "composer require symfony/security-core".', __METHOD__));
118121
}
@@ -122,6 +125,7 @@ public function loginUser(object $user, string $firewallContext = 'main'): stati
122125
}
123126

124127
$token = new TestBrowserToken($user->getRoles(), $user, $firewallContext);
128+
$token->setAttributes($tokenAttributes);
125129
// required for compatibility with Symfony 5.4
126130
if (method_exists($token, 'isAuthenticated')) {
127131
$token->setAuthenticated(true, false);

0 commit comments

Comments
 (0)