@@ -51,7 +51,7 @@ class User
5151 public array $ onLoggedOut = [];
5252
5353 /** Session storage for current user */
54- private UserStorage | IUserStorage $ storage ;
54+ private UserStorage $ storage ;
5555
5656 private ?IAuthenticator $ authenticator ;
5757
@@ -65,21 +65,17 @@ class User
6565
6666
6767 public function __construct (
68- IUserStorage $ legacyStorage = null ,
68+ UserStorage $ storage ,
6969 IAuthenticator $ authenticator = null ,
7070 Authorizator $ authorizator = null ,
71- UserStorage $ storage = null ,
7271 ) {
73- $ this ->storage = $ storage ?? $ legacyStorage ; // back compatibility
74- if (!$ this ->storage ) {
75- throw new Nette \InvalidStateException ('UserStorage has not been set. ' );
76- }
72+ $ this ->storage = $ storage ;
7773 $ this ->authenticator = $ authenticator ;
7874 $ this ->authorizator = $ authorizator ;
7975 }
8076
8177
82- final public function getStorage (): UserStorage | IUserStorage
78+ final public function getStorage (): UserStorage
8379 {
8480 return $ this ->storage ;
8581 }
@@ -108,13 +104,8 @@ public function login(string|IIdentity $user, string $password = null): void
108104 $ id = $ this ->authenticator instanceof IdentityHandler
109105 ? $ this ->authenticator ->sleepIdentity ($ this ->identity )
110106 : $ this ->identity ;
111- if ($ this ->storage instanceof UserStorage) {
112- $ this ->storage ->saveAuthentication ($ id );
113- } else {
114- $ this ->storage ->setIdentity ($ id );
115- $ this ->storage ->setAuthenticated (true );
116- }
117107
108+ $ this ->storage ->saveAuthentication ($ id );
118109 $ this ->authenticated = true ;
119110 $ this ->logoutReason = null ;
120111 Arrays::invoke ($ this ->onLoggedIn , $ this );
@@ -127,16 +118,7 @@ public function login(string|IIdentity $user, string $password = null): void
127118 final public function logout (bool $ clearIdentity = false ): void
128119 {
129120 $ logged = $ this ->isLoggedIn ();
130-
131- if ($ this ->storage instanceof UserStorage) {
132- $ this ->storage ->clearAuthentication ($ clearIdentity );
133- } else {
134- $ this ->storage ->setAuthenticated (false );
135- if ($ clearIdentity ) {
136- $ this ->storage ->setIdentity (null );
137- }
138- }
139-
121+ $ this ->storage ->clearAuthentication ($ clearIdentity );
140122 $ this ->authenticated = false ;
141123 $ this ->logoutReason = self ::MANUAL ;
142124 if ($ logged ) {
@@ -172,17 +154,11 @@ final public function getIdentity(): ?IIdentity
172154
173155 private function getStoredData (): void
174156 {
175- if ($ this ->storage instanceof UserStorage) {
176- (function (bool $ state , ?IIdentity $ id , ?int $ reason ) use (&$ identity ) {
177- $ identity = $ id ;
178- $ this ->authenticated = $ state ;
179- $ this ->logoutReason = $ reason ;
180- })(...$ this ->storage ->getState ());
181- } else {
182- $ identity = $ this ->storage ->getIdentity ();
183- $ this ->authenticated = $ this ->storage ->isAuthenticated ();
184- $ this ->logoutReason = $ this ->storage ->getLogoutReason ();
185- }
157+ (function (bool $ state , ?IIdentity $ id , ?int $ reason ) use (&$ identity ) {
158+ $ identity = $ id ;
159+ $ this ->authenticated = $ state ;
160+ $ this ->logoutReason = $ reason ;
161+ })(...$ this ->storage ->getState ());
186162
187163 $ this ->identity = $ identity && $ this ->authenticator instanceof IdentityHandler
188164 ? $ this ->authenticator ->wakeupIdentity ($ identity )
@@ -248,12 +224,9 @@ final public function hasAuthenticator(): bool
248224 /**
249225 * Enables log out after inactivity (like '20 minutes').
250226 */
251- public function setExpiration (?string $ expire , bool | int $ clearIdentity = null )
227+ public function setExpiration (?string $ expire , bool $ clearIdentity = false )
252228 {
253- $ arg = $ this ->storage instanceof UserStorage
254- ? (bool ) $ clearIdentity
255- : ($ clearIdentity ? IUserStorage::CLEAR_IDENTITY : 0 );
256- $ this ->storage ->setExpiration ($ expire , $ arg );
229+ $ this ->storage ->setExpiration ($ expire , $ clearIdentity );
257230 return $ this ;
258231 }
259232
0 commit comments