Skip to content

Commit

Permalink
User: identity and authenticated state are cached, UserStorage is not…
Browse files Browse the repository at this point in the history
… called repeatedly
  • Loading branch information
dg committed Jan 13, 2020
1 parent db73664 commit 4830af0
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/Security/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ class User
/** @var IAuthorizator|null */
private $authorizator;

/** @var IIdentity|null|false false means undefined */
private $identity;

/** @var bool|null */
private $authenticated;


public function __construct(IUserStorage $storage, IAuthenticator $authenticator = null, IAuthorizator $authorizator = null)
{
Expand Down Expand Up @@ -84,6 +90,8 @@ public function login($user, string $password = null): void
}
$this->storage->setIdentity($user);
$this->storage->setAuthenticated(true);
$this->identity = $user;
$this->authenticated = true;
$this->onLoggedIn($this);
}

Expand All @@ -96,9 +104,11 @@ final public function logout(bool $clearIdentity = false): void
if ($this->isLoggedIn()) {
$this->onLoggedOut($this);
$this->storage->setAuthenticated(false);
$this->authenticated = false;
}
if ($clearIdentity) {
$this->storage->setIdentity(null);
$this->identity = null;
}
}

Expand All @@ -108,7 +118,10 @@ final public function logout(bool $clearIdentity = false): void
*/
final public function isLoggedIn(): bool
{
return $this->storage->isAuthenticated();
if ($this->authenticated === null) {
$this->authenticated = $this->storage->isAuthenticated();
}
return $this->authenticated;
}


Expand All @@ -117,7 +130,10 @@ final public function isLoggedIn(): bool
*/
final public function getIdentity(): ?IIdentity
{
return $this->storage->getIdentity();
if ($this->identity === false) {
$this->identity = $this->storage->getIdentity();
}
return $this->identity;
}


Expand Down

0 comments on commit 4830af0

Please sign in to comment.