11
11
12
12
namespace Symfony \Component \Security \Core \Authentication \Token ;
13
13
14
+ use Symfony \Component \Security \Core \User \EquatableInterface ;
14
15
use Symfony \Component \Security \Core \User \InMemoryUser ;
15
16
use Symfony \Component \Security \Core \User \UserInterface ;
16
17
23
24
abstract class AbstractToken implements TokenInterface, \Serializable
24
25
{
25
26
private ?UserInterface $ user = null ;
26
- private array $ roleNames = [] ;
27
+ private array $ roleNames ;
27
28
private array $ attributes = [];
28
29
29
30
/**
30
31
* @param string[] $roles An array of roles
31
- *
32
- * @throws \InvalidArgumentException
33
32
*/
34
33
public function __construct (array $ roles = [])
35
34
{
35
+ $ this ->roleNames = [];
36
+
36
37
foreach ($ roles as $ role ) {
37
- $ this ->roleNames [] = $ role ;
38
+ $ this ->roleNames [] = ( string ) $ role ;
38
39
}
39
40
}
40
41
41
42
public function getRoleNames (): array
42
43
{
43
- return $ this ->roleNames ;
44
+ return $ this ->roleNames ??= self :: __construct ( $ this -> user -> getRoles ()) ?? $ this -> roleNames ;
44
45
}
45
46
46
47
public function getUserIdentifier (): string
@@ -82,7 +83,13 @@ public function eraseCredentials(): void
82
83
*/
83
84
public function __serialize (): array
84
85
{
85
- return [$ this ->user , true , null , $ this ->attributes , $ this ->roleNames ];
86
+ $ data = [$ this ->user , true , null , $ this ->attributes ];
87
+
88
+ if (!$ this ->user instanceof EquatableInterface) {
89
+ $ data [] = $ this ->roleNames ;
90
+ }
91
+
92
+ return $ data ;
86
93
}
87
94
88
95
/**
@@ -103,7 +110,12 @@ public function __serialize(): array
103
110
*/
104
111
public function __unserialize (array $ data ): void
105
112
{
106
- [$ user , , , $ this ->attributes , $ this ->roleNames ] = $ data ;
113
+ [$ user , , , $ this ->attributes ] = $ data ;
114
+
115
+ if (\array_key_exists (4 , $ data )) {
116
+ $ this ->roleNames = $ data [4 ];
117
+ }
118
+
107
119
$ this ->user = \is_string ($ user ) ? new InMemoryUser ($ user , '' , $ this ->roleNames , false ) : $ user ;
108
120
}
109
121
0 commit comments