@@ -54,7 +54,7 @@ class User
5454 public array $ onLoggedOut = [];
5555
5656 /** Session storage for current user */
57- private UserStorage | IUserStorage $ storage ;
57+ private UserStorage $ storage ;
5858 private ?IAuthenticator $ authenticator ;
5959 private ?Authorizator $ authorizator ;
6060 private ?IIdentity $ identity = null ;
@@ -63,22 +63,17 @@ class User
6363
6464
6565 public function __construct (
66- ? IUserStorage $ legacyStorage = null ,
66+ UserStorage $ storage ,
6767 ?IAuthenticator $ authenticator = null ,
6868 ?Authorizator $ authorizator = null ,
69- ?UserStorage $ storage = null ,
7069 ) {
71- $ this ->storage = $ storage ?? $ legacyStorage ; // back compatibility
72- if (!$ this ->storage ) {
73- throw new Nette \InvalidStateException ('UserStorage has not been set. ' );
74- }
75-
70+ $ this ->storage = $ storage ;
7671 $ this ->authenticator = $ authenticator ;
7772 $ this ->authorizator = $ authorizator ;
7873 }
7974
8075
81- final public function getStorage (): UserStorage | IUserStorage
76+ final public function getStorage (): UserStorage
8277 {
8378 return $ this ->storage ;
8479 }
@@ -111,13 +106,8 @@ public function login(
111106 $ id = $ this ->authenticator instanceof IdentityHandler
112107 ? $ this ->authenticator ->sleepIdentity ($ this ->identity )
113108 : $ this ->identity ;
114- if ($ this ->storage instanceof UserStorage) {
115- $ this ->storage ->saveAuthentication ($ id );
116- } else {
117- $ this ->storage ->setIdentity ($ id );
118- $ this ->storage ->setAuthenticated (true );
119- }
120109
110+ $ this ->storage ->saveAuthentication ($ id );
121111 $ this ->authenticated = true ;
122112 $ this ->logoutReason = null ;
123113 Arrays::invoke ($ this ->onLoggedIn , $ this );
@@ -130,16 +120,7 @@ public function login(
130120 final public function logout (bool $ clearIdentity = false ): void
131121 {
132122 $ logged = $ this ->isLoggedIn ();
133-
134- if ($ this ->storage instanceof UserStorage) {
135- $ this ->storage ->clearAuthentication ($ clearIdentity );
136- } else {
137- $ this ->storage ->setAuthenticated (false );
138- if ($ clearIdentity ) {
139- $ this ->storage ->setIdentity (null );
140- }
141- }
142-
123+ $ this ->storage ->clearAuthentication ($ clearIdentity );
143124 $ this ->authenticated = false ;
144125 $ this ->logoutReason = self ::LogoutManual;
145126 if ($ logged ) {
@@ -178,17 +159,11 @@ final public function getIdentity(): ?IIdentity
178159
179160 private function getStoredData (): void
180161 {
181- if ($ this ->storage instanceof UserStorage) {
182- (function (bool $ state , ?IIdentity $ id , ?int $ reason ) use (&$ identity ) {
183- $ identity = $ id ;
184- $ this ->authenticated = $ state ;
185- $ this ->logoutReason = $ reason ;
186- })(...$ this ->storage ->getState ());
187- } else {
188- $ identity = $ this ->storage ->getIdentity ();
189- $ this ->authenticated = $ this ->storage ->isAuthenticated ();
190- $ this ->logoutReason = $ this ->storage ->getLogoutReason ();
191- }
162+ (function (bool $ state , ?IIdentity $ id , ?int $ reason ) use (&$ identity ) {
163+ $ identity = $ id ;
164+ $ this ->authenticated = $ state ;
165+ $ this ->logoutReason = $ reason ;
166+ })(...$ this ->storage ->getState ());
192167
193168 $ this ->identity = $ identity && $ this ->authenticator instanceof IdentityHandler
194169 ? $ this ->authenticator ->wakeupIdentity ($ identity )
@@ -255,12 +230,9 @@ final public function hasAuthenticator(): bool
255230 /**
256231 * Enables log out after inactivity (like '20 minutes').
257232 */
258- public function setExpiration (?string $ expire , bool | int | null $ clearIdentity = null )
233+ public function setExpiration (?string $ expire , bool $ clearIdentity = false )
259234 {
260- $ arg = $ this ->storage instanceof UserStorage
261- ? (bool ) $ clearIdentity
262- : ($ clearIdentity ? IUserStorage::CLEAR_IDENTITY : 0 );
263- $ this ->storage ->setExpiration ($ expire , $ arg );
235+ $ this ->storage ->setExpiration ($ expire , $ clearIdentity );
264236 return $ this ;
265237 }
266238
0 commit comments