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

Skip to content

Commit 8092ffd

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

File tree

1 file changed

+4
-19
lines changed

1 file changed

+4
-19
lines changed

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

Lines changed: 4 additions & 19 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
@@ -90,13 +86,7 @@ public function eraseCredentials(): void
9086
*/
9187
public function __serialize(): array
9288
{
93-
$data = [$this->user, true, null, $this->attributes];
94-
95-
if (!$this->user instanceof EquatableInterface) {
96-
$data[] = $this->roleNames;
97-
}
98-
99-
return $data;
89+
return [$this->user, true, null, $this->attributes, $this->getRoleNames()];
10090
}
10191

10292
/**
@@ -160,12 +150,7 @@ public function __toString(): string
160150
$class = static::class;
161151
$class = substr($class, strrpos($class, '\\') + 1);
162152

163-
$roles = [];
164-
foreach ($this->roleNames as $role) {
165-
$roles[] = $role;
166-
}
167-
168-
return \sprintf('%s(user="%s", roles="%s")', $class, $this->getUserIdentifier(), implode(', ', $roles));
153+
return \sprintf('%s(user="%s", roles="%s")', $class, $this->getUserIdentifier(), implode(', ', $this->getRoleNames()));
169154
}
170155

171156
/**

0 commit comments

Comments
 (0)
0