1111
1212namespace Symfony \Component \Security \Core \Authentication \Token ;
1313
14+ use Symfony \Component \Security \Core \User \EquatableInterface ;
1415use Symfony \Component \Security \Core \User \InMemoryUser ;
1516use Symfony \Component \Security \Core \User \UserInterface ;
1617
2324abstract class AbstractToken implements TokenInterface, \Serializable
2425{
2526 private ?UserInterface $ user = null ;
26- private array $ roleNames = [] ;
27+ private array $ roleNames ;
2728 private array $ attributes = [];
2829
2930 /**
3031 * @param string[] $roles An array of roles
31- *
32- * @throws \InvalidArgumentException
3332 */
3433 public function __construct (array $ roles = [])
3534 {
35+ $ this ->roleNames = [];
36+
3637 foreach ($ roles as $ role ) {
37- $ this ->roleNames [] = $ role ;
38+ $ this ->roleNames [] = ( string ) $ role ;
3839 }
3940 }
4041
4142 public function getRoleNames (): array
4243 {
43- return $ this ->roleNames ;
44+ return $ this ->roleNames ??= self :: __construct ( $ this -> user -> getRoles ()) ?? $ this -> roleNames ;
4445 }
4546
4647 public function getUserIdentifier (): string
@@ -82,7 +83,13 @@ public function eraseCredentials(): void
8283 */
8384 public function __serialize (): array
8485 {
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 ;
8693 }
8794
8895 /**
@@ -103,7 +110,12 @@ public function __serialize(): array
103110 */
104111 public function __unserialize (array $ data ): void
105112 {
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+
107119 $ this ->user = \is_string ($ user ) ? new InMemoryUser ($ user , '' , $ this ->roleNames , false ) : $ user ;
108120 }
109121
0 commit comments