@@ -55,7 +55,7 @@ class User
5555 public iterable $ onLoggedOut = [];
5656
5757 /** Session storage for current user */
58- private UserStorage | IUserStorage $ storage ;
58+ private UserStorage $ storage ;
5959 private ?IAuthenticator $ authenticator ;
6060 private ?Authorizator $ authorizator ;
6161 private ?IIdentity $ identity = null ;
@@ -64,22 +64,17 @@ class User
6464
6565
6666 public function __construct (
67- ? IUserStorage $ legacyStorage = null ,
67+ UserStorage $ storage ,
6868 ?IAuthenticator $ authenticator = null ,
6969 ?Authorizator $ authorizator = null ,
70- ?UserStorage $ storage = null ,
7170 ) {
72- $ this ->storage = $ storage ?? $ legacyStorage ; // back compatibility
73- if (!$ this ->storage ) {
74- throw new Nette \InvalidStateException ('UserStorage has not been set. ' );
75- }
76-
71+ $ this ->storage = $ storage ;
7772 $ this ->authenticator = $ authenticator ;
7873 $ this ->authorizator = $ authorizator ;
7974 }
8075
8176
82- final public function getStorage (): UserStorage | IUserStorage
77+ final public function getStorage (): UserStorage
8378 {
8479 return $ this ->storage ;
8580 }
@@ -108,13 +103,8 @@ public function login(string|IIdentity $user, ?string $password = null): void
108103 $ id = $ this ->authenticator instanceof IdentityHandler
109104 ? $ this ->authenticator ->sleepIdentity ($ this ->identity )
110105 : $ 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- }
117106
107+ $ this ->storage ->saveAuthentication ($ id );
118108 $ this ->authenticated = true ;
119109 $ this ->logoutReason = null ;
120110 Arrays::invoke ($ this ->onLoggedIn , $ this );
@@ -127,16 +117,7 @@ public function login(string|IIdentity $user, ?string $password = null): void
127117 final public function logout (bool $ clearIdentity = false ): void
128118 {
129119 $ 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-
120+ $ this ->storage ->clearAuthentication ($ clearIdentity );
140121 $ this ->authenticated = false ;
141122 $ this ->logoutReason = self ::MANUAL ;
142123 if ($ logged ) {
@@ -175,17 +156,11 @@ final public function getIdentity(): ?IIdentity
175156
176157 private function getStoredData (): void
177158 {
178- if ($ this ->storage instanceof UserStorage) {
179- (function (bool $ state , ?IIdentity $ id , ?int $ reason ) use (&$ identity ) {
180- $ identity = $ id ;
181- $ this ->authenticated = $ state ;
182- $ this ->logoutReason = $ reason ;
183- })(...$ this ->storage ->getState ());
184- } else {
185- $ identity = $ this ->storage ->getIdentity ();
186- $ this ->authenticated = $ this ->storage ->isAuthenticated ();
187- $ this ->logoutReason = $ this ->storage ->getLogoutReason ();
188- }
159+ (function (bool $ state , ?IIdentity $ id , ?int $ reason ) use (&$ identity ) {
160+ $ identity = $ id ;
161+ $ this ->authenticated = $ state ;
162+ $ this ->logoutReason = $ reason ;
163+ })(...$ this ->storage ->getState ());
189164
190165 $ this ->identity = $ identity && $ this ->authenticator instanceof IdentityHandler
191166 ? $ this ->authenticator ->wakeupIdentity ($ identity )
@@ -252,12 +227,9 @@ final public function hasAuthenticator(): bool
252227 /**
253228 * Enables log out after inactivity (like '20 minutes').
254229 */
255- public function setExpiration (?string $ expire , bool | int | null $ clearIdentity = null )
230+ public function setExpiration (?string $ expire , bool $ clearIdentity = false )
256231 {
257- $ arg = $ this ->storage instanceof UserStorage
258- ? (bool ) $ clearIdentity
259- : ($ clearIdentity ? IUserStorage::CLEAR_IDENTITY : 0 );
260- $ this ->storage ->setExpiration ($ expire , $ arg );
232+ $ this ->storage ->setExpiration ($ expire , $ clearIdentity );
261233 return $ this ;
262234 }
263235
0 commit comments