1+ <?php
2+ /**
3+ * Created by PhpStorm.
4+ * User: Jozef Môstka
5+ * Date: 9. 10. 2024
6+ * Time: 21:24
7+ */
8+
9+ namespace BugCatcher \Security ;
10+
11+ use BadMethodCallException ;
12+ use Serializable ;
13+ use Symfony \Component \Security \Core \Authentication \Token \TokenInterface ;
14+ use Symfony \Component \Security \Core \Authentication \Token \UsernamePasswordToken ;
15+ use Symfony \Component \Security \Core \User \UserInterface ;
16+
17+ class MultipleLoginToken implements TokenInterface, Serializable
18+ {
19+
20+ /**
21+ * @var TokenInterface[]
22+ */
23+ private array $ tokens = [];
24+
25+ public function __construct (
26+ private TokenInterface $ currentToken ,
27+ TokenInterface $ originalToken
28+ ) {
29+ $ this ->tokens [] = $ originalToken ;
30+ }
31+
32+ public function setToken (TokenInterface $ newToken ): void
33+ {
34+ $ this ->tokens [] = $ this ->currentToken ;
35+ $ this ->tokens = array_unique ($ this ->tokens , SORT_REGULAR );
36+ $ this ->currentToken = $ newToken ;
37+ }
38+
39+ public function trySwitchUser (TokenInterface $ newToken ): bool
40+ {
41+ foreach ($ this ->tokens as $ token ) {
42+ if ($ token ->getUserIdentifier () == $ newToken ->getUserIdentifier ()) {
43+ $ this ->tokens [] = $ this ->currentToken ;
44+ $ this ->tokens = array_unique ($ this ->tokens , SORT_REGULAR );
45+ $ this ->currentToken = $ token ;
46+ return true ;
47+ }
48+ }
49+ return false ;
50+ }
51+
52+ public function getUserIdentifiers (): array
53+ {
54+ $ data = [];
55+ foreach ($ this ->tokens as $ token ) {
56+ $ data [] = $ token ->getUserIdentifier ();
57+ }
58+ return $ data ;
59+ }
60+
61+ /**
62+ * @return TokenInterface[]
63+ */
64+ public function getTokens (): array
65+ {
66+ return $ this ->tokens ;
67+ }
68+
69+ public function __serialize (): array
70+ {
71+ $ data = $ this ->tokens ;
72+ $ data [] = $ this ->currentToken ;
73+ return $ data ;
74+ }
75+
76+ public function __unserialize (array $ data ): void
77+ {
78+ $ currentToken = array_pop ($ data );
79+ $ this ->currentToken = $ currentToken ;
80+ $ this ->tokens = $ data ;
81+ }
82+
83+ public function __toString (): string
84+ {
85+ return $ this ->currentToken ->__toString ();
86+ }
87+
88+ public function getUserIdentifier (): string
89+ {
90+ return $ this ->currentToken ->getUserIdentifier ();
91+ }
92+
93+ public function getRoleNames (): array
94+ {
95+ return $ this ->currentToken ->getRoleNames ();
96+ }
97+
98+ public function getUser (): ?UserInterface
99+ {
100+ return $ this ->currentToken ->getUser ();
101+ }
102+
103+ public function setUser (UserInterface $ user ): void
104+ {
105+ $ this ->currentToken ->setUser ($ user );
106+ }
107+
108+ public function eraseCredentials (): void
109+ {
110+ $ this ->currentToken ->eraseCredentials ();
111+ }
112+
113+ public function getAttributes (): array
114+ {
115+ return $ this ->currentToken ->getAttributes ();
116+ }
117+
118+ public function setAttributes (array $ attributes ): void
119+ {
120+ $ this ->currentToken ->setAttributes ($ attributes );
121+ }
122+
123+ public function hasAttribute (string $ name ): bool
124+ {
125+ return $ this ->currentToken ->hasAttribute ($ name );
126+ }
127+
128+ public function getAttribute (string $ name ): mixed
129+ {
130+ return $ this ->currentToken ->getAttribute ($ name );
131+ }
132+
133+ public function setAttribute (string $ name , mixed $ value ): void
134+ {
135+ $ this ->currentToken ->setAttribute ($ name , $ value );
136+ }
137+
138+ public function serialize ()
139+ {
140+ throw new BadMethodCallException ('Cannot serialize ' . __CLASS__ );
141+ }
142+
143+ public function unserialize (string $ serialized )
144+ {
145+ $ this ->__unserialize (unserialize ($ serialized ));
146+ }
147+
148+
149+ }
0 commit comments