@@ -51,7 +51,7 @@ class User
5151 public iterable $ onLoggedOut = [];
5252
5353 /** Session storage for current user */
54- private UserStorage | IUserStorage $ storage ;
54+ private UserStorage $ storage ;
5555 private ?IAuthenticator $ authenticator ;
5656 private ?Authorizator $ authorizator ;
5757 private ?IIdentity $ identity = null ;
@@ -60,22 +60,17 @@ class User
6060
6161
6262 public function __construct (
63- ? IUserStorage $ legacyStorage = null ,
63+ UserStorage $ storage ,
6464 ?IAuthenticator $ authenticator = null ,
6565 ?Authorizator $ authorizator = null ,
66- ?UserStorage $ storage = null ,
6766 ) {
68- $ this ->storage = $ storage ?? $ legacyStorage ; // back compatibility
69- if (!$ this ->storage ) {
70- throw new Nette \InvalidStateException ('UserStorage has not been set. ' );
71- }
72-
67+ $ this ->storage = $ storage ;
7368 $ this ->authenticator = $ authenticator ;
7469 $ this ->authorizator = $ authorizator ;
7570 }
7671
7772
78- final public function getStorage (): UserStorage | IUserStorage
73+ final public function getStorage (): UserStorage
7974 {
8075 return $ this ->storage ;
8176 }
@@ -104,13 +99,8 @@ public function login(string|IIdentity $user, ?string $password = null): void
10499 $ id = $ this ->authenticator instanceof IdentityHandler
105100 ? $ this ->authenticator ->sleepIdentity ($ this ->identity )
106101 : $ this ->identity ;
107- if ($ this ->storage instanceof UserStorage) {
108- $ this ->storage ->saveAuthentication ($ id );
109- } else {
110- $ this ->storage ->setIdentity ($ id );
111- $ this ->storage ->setAuthenticated (true );
112- }
113102
103+ $ this ->storage ->saveAuthentication ($ id );
114104 $ this ->authenticated = true ;
115105 $ this ->logoutReason = null ;
116106 Arrays::invoke ($ this ->onLoggedIn , $ this );
@@ -123,16 +113,7 @@ public function login(string|IIdentity $user, ?string $password = null): void
123113 final public function logout (bool $ clearIdentity = false ): void
124114 {
125115 $ logged = $ this ->isLoggedIn ();
126-
127- if ($ this ->storage instanceof UserStorage) {
128- $ this ->storage ->clearAuthentication ($ clearIdentity );
129- } else {
130- $ this ->storage ->setAuthenticated (false );
131- if ($ clearIdentity ) {
132- $ this ->storage ->setIdentity (null );
133- }
134- }
135-
116+ $ this ->storage ->clearAuthentication ($ clearIdentity );
136117 $ this ->authenticated = false ;
137118 $ this ->logoutReason = self ::MANUAL ;
138119 if ($ logged ) {
@@ -171,17 +152,11 @@ final public function getIdentity(): ?IIdentity
171152
172153 private function getStoredData (): void
173154 {
174- if ($ this ->storage instanceof UserStorage) {
175- (function (bool $ state , ?IIdentity $ id , ?int $ reason ) use (&$ identity ) {
176- $ identity = $ id ;
177- $ this ->authenticated = $ state ;
178- $ this ->logoutReason = $ reason ;
179- })(...$ this ->storage ->getState ());
180- } else {
181- $ identity = $ this ->storage ->getIdentity ();
182- $ this ->authenticated = $ this ->storage ->isAuthenticated ();
183- $ this ->logoutReason = $ this ->storage ->getLogoutReason ();
184- }
155+ (function (bool $ state , ?IIdentity $ id , ?int $ reason ) use (&$ identity ) {
156+ $ identity = $ id ;
157+ $ this ->authenticated = $ state ;
158+ $ this ->logoutReason = $ reason ;
159+ })(...$ this ->storage ->getState ());
185160
186161 $ this ->identity = $ identity && $ this ->authenticator instanceof IdentityHandler
187162 ? $ this ->authenticator ->wakeupIdentity ($ identity )
@@ -248,12 +223,9 @@ final public function hasAuthenticator(): bool
248223 /**
249224 * Enables log out after inactivity (like '20 minutes').
250225 */
251- public function setExpiration (?string $ expire , bool | int | null $ clearIdentity = null )
226+ public function setExpiration (?string $ expire , bool $ clearIdentity = false )
252227 {
253- $ arg = $ this ->storage instanceof UserStorage
254- ? (bool ) $ clearIdentity
255- : ($ clearIdentity ? IUserStorage::CLEAR_IDENTITY : 0 );
256- $ this ->storage ->setExpiration ($ expire , $ arg );
228+ $ this ->storage ->setExpiration ($ expire , $ clearIdentity );
257229 return $ this ;
258230 }
259231
0 commit comments