@@ -53,6 +53,12 @@ class User
53
53
/** @var IAuthorizator|null */
54
54
private $ authorizator ;
55
55
56
+ /** @var IIdentity|null|false false means undefined */
57
+ private $ identity = false ;
58
+
59
+ /** @var bool|null */
60
+ private $ authenticated ;
61
+
56
62
57
63
public function __construct (IUserStorage $ storage , IAuthenticator $ authenticator = null , IAuthorizator $ authorizator = null )
58
64
{
@@ -84,6 +90,8 @@ public function login($user, string $password = null): void
84
90
}
85
91
$ this ->storage ->setIdentity ($ user );
86
92
$ this ->storage ->setAuthenticated (true );
93
+ $ this ->identity = $ user ;
94
+ $ this ->authenticated = true ;
87
95
$ this ->onLoggedIn ($ this );
88
96
}
89
97
@@ -96,9 +104,11 @@ final public function logout(bool $clearIdentity = false): void
96
104
if ($ this ->isLoggedIn ()) {
97
105
$ this ->onLoggedOut ($ this );
98
106
$ this ->storage ->setAuthenticated (false );
107
+ $ this ->authenticated = false ;
99
108
}
100
109
if ($ clearIdentity ) {
101
110
$ this ->storage ->setIdentity (null );
111
+ $ this ->identity = null ;
102
112
}
103
113
}
104
114
@@ -108,7 +118,10 @@ final public function logout(bool $clearIdentity = false): void
108
118
*/
109
119
final public function isLoggedIn (): bool
110
120
{
111
- return $ this ->storage ->isAuthenticated ();
121
+ if ($ this ->authenticated === null ) {
122
+ $ this ->authenticated = $ this ->storage ->isAuthenticated ();
123
+ }
124
+ return $ this ->authenticated ;
112
125
}
113
126
114
127
@@ -117,7 +130,10 @@ final public function isLoggedIn(): bool
117
130
*/
118
131
final public function getIdentity (): ?IIdentity
119
132
{
120
- return $ this ->storage ->getIdentity ();
133
+ if ($ this ->identity === false ) {
134
+ $ this ->identity = $ this ->storage ->getIdentity ();
135
+ }
136
+ return $ this ->identity ;
121
137
}
122
138
123
139
0 commit comments