@@ -44,7 +44,7 @@ class User
4444 /** @var callable[] function (User $sender): void; Occurs when the user is logged out */
4545 public $ onLoggedOut ;
4646
47- /** @var IUserStorage Session storage for current user */
47+ /** @var UserStorage| IUserStorage Session storage for current user */
4848 private $ storage ;
4949
5050 /** @var IAuthenticator|null */
@@ -64,17 +64,24 @@ class User
6464
6565
6666 public function __construct (
67- IUserStorage $ storage ,
67+ UserStorage $ storage = null ,
6868 IAuthenticator $ authenticator = null ,
69- Authorizator $ authorizator = null
69+ Authorizator $ authorizator = null ,
70+ IUserStorage $ legacyStorage = null
7071 ) {
71- $ this ->storage = $ storage ;
72+ $ this ->storage = $ storage ?? $ legacyStorage ; // back compatibility
73+ if (!$ this ->storage ) {
74+ throw new Nette \InvalidStateException ('UserStorage has not been set. ' );
75+ }
7276 $ this ->authenticator = $ authenticator ;
7377 $ this ->authorizator = $ authorizator ;
7478 }
7579
7680
77- final public function getStorage (): IUserStorage
81+ /**
82+ * @return UserStorage|IUserStorage
83+ */
84+ final public function getStorage ()
7885 {
7986 return $ this ->storage ;
8087 }
@@ -97,8 +104,12 @@ public function login($user, string $password = null): void
97104 ? $ authenticator ->authenticate ($ user , $ password )
98105 : $ authenticator ->authenticate (func_get_args ());
99106 }
100- $ this ->storage ->setIdentity ($ user );
101- $ this ->storage ->setAuthenticated (true );
107+ if ($ this ->storage instanceof UserStorage) {
108+ $ this ->storage ->saveAuthentication ($ user );
109+ } else {
110+ $ this ->storage ->setIdentity ($ user );
111+ $ this ->storage ->setAuthenticated (true );
112+ }
102113 $ this ->identity = $ user ;
103114 $ this ->authenticated = true ;
104115 $ this ->logoutReason = null ;
@@ -113,14 +124,20 @@ final public function logout(bool $clearIdentity = false): void
113124 {
114125 if ($ this ->isLoggedIn ()) {
115126 $ this ->onLoggedOut ($ this );
127+ }
128+
129+ if ($ this ->storage instanceof UserStorage) {
130+ $ this ->storage ->clearAuthentication ($ clearIdentity );
131+ } else {
116132 $ this ->storage ->setAuthenticated (false );
117- $ this ->authenticated = false ;
133+ if ($ clearIdentity ) {
134+ $ this ->storage ->setIdentity (null );
135+ }
118136 $ this ->logoutReason = self ::MANUAL ;
119137 }
120- if ($ clearIdentity ) {
121- $ this ->storage ->setIdentity (null );
122- $ this ->identity = null ;
123- }
138+
139+ $ this ->authenticated = false ;
140+ $ this ->identity = $ clearIdentity ? null : $ this ->identity ;
124141 }
125142
126143
@@ -150,9 +167,17 @@ final public function getIdentity(): ?IIdentity
150167
151168 private function getStoredData (): void
152169 {
153- $ this ->authenticated = $ this ->storage ->isAuthenticated ();
154- $ this ->identity = $ this ->storage ->getIdentity ();
155- $ this ->logoutReason = $ this ->storage ->getLogoutReason ();
170+ if ($ this ->storage instanceof UserStorage) {
171+ (function (bool $ state , ?IIdentity $ identity , ?int $ reason ) {
172+ $ this ->authenticated = $ state ;
173+ $ this ->identity = $ identity ;
174+ $ this ->logoutReason = $ reason ;
175+ })(...$ this ->storage ->getState ());
176+ } else {
177+ $ this ->authenticated = $ this ->storage ->isAuthenticated ();
178+ $ this ->identity = $ this ->storage ->getIdentity ();
179+ $ this ->logoutReason = $ this ->storage ->getLogoutReason ();
180+ }
156181 }
157182
158183
@@ -235,7 +260,11 @@ public function setExpiration($expire, /*int*/$flags = 0)
235260 $ clearIdentity = $ clearIdentity || func_get_arg (2 );
236261 trigger_error (__METHOD__ . '() third parameter is deprecated, use flag setExpiration($time, IUserStorage::CLEAR_IDENTITY) ' , E_USER_DEPRECATED );
237262 }
238- $ this ->storage ->setExpiration ($ expire , $ clearIdentity ? IUserStorage::CLEAR_IDENTITY : 0 );
263+
264+ $ arg = $ this ->storage instanceof UserStorage
265+ ? $ clearIdentity
266+ : ($ clearIdentity ? IUserStorage::CLEAR_IDENTITY : 0 );
267+ $ this ->storage ->setExpiration ($ expire , $ arg );
239268 return $ this ;
240269 }
241270
0 commit comments