8000 [Security] Keep roles when serializing tokens · symfony/symfony@b1b0fd7 · GitHub
[go: up one dir, main page]

Skip to content

Commit b1b0fd7

Browse files
[Security] Keep roles when serializing tokens
1 parent e9eb3e7 commit b1b0fd7

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

src/Symfony/Component/Security/Core/Authentication/Token/AbstractToken.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,12 @@ abstract class AbstractToken implements TokenInterface, \Serializable
3232
*/
3333
public function __construct(array $roles = [])
3434
{
35-
$this->roleNames = [];
36-
37-
foreach ($roles as $role) {
38-
$this->roleNames[] = (string) $role;
39-
}
35+
$this->roleNames = $roles;
4036
}
4137

4238
public function getRoleNames(): array
4339
{
44-
return $this->roleNames ??= self::__construct($this->user->getRoles()) ?? $this->roleNames;
40+
return $this->roleNames ??= $this->user?->getRoles() ?? [];
4541
}
4642

4743
public function getUserIdentifier(): string
@@ -92,7 +88,7 @@ public function __serialize(): array
9288
{
9389
$data = [$this->user, true, null, $this->attributes];
9490

95-
if (!$this->user instanceof EquatableInterface) {
91+
if (($this->user?->getRoles() ?? []) !== $this->getRoleNames()) {
9692
$data[] = $this->roleNames;
9793
}
9894

@@ -161,7 +157,7 @@ public function __toString(): string
161157
$class = substr($class, strrpos($class, '\\') + 1);
162158

163159
$roles = [];
164-
foreach ($this->roleNames as $role) {
160+
foreach ($this->getRoleNames() as $role) {
165161
$roles[] = $role;
166162
}
167163

0 commit comments

Comments
 (0)
0