Skip to content
Permalink
Browse files

User: identity and authenticated state are cached, UserStorage is not…

… called repeatedly
  • Loading branch information
dg committed Jan 13, 2020
1 parent db73664 commit f53dcb32d1103669e9a639ebb1500ce8fc642c53
Showing with 18 additions and 2 deletions.
  1. +18 −2 src/Security/User.php
@@ -53,6 +53,12 @@ class User
/** @var IAuthorizator|null */
private $authorizator;

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

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


public function __construct(IUserStorage $storage, IAuthenticator $authenticator = null, IAuthorizator $authorizator = null)
{
@@ -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);
}

@@ -96,9 +104,11 @@ public function login($user, string $password = null): void
if ($this->isLoggedIn()) {
$this->onLoggedOut($this);
$this->storage->setAuthenticated(false);
$this->authenticated = false;
}
if ($clearIdentity) {
$this->storage->setIdentity(null);
$this->identity = null;
}
}

@@ -108,7 +118,10 @@ public function login($user, string $password = null): void
*/
final public function isLoggedIn(): bool
{
return $this->storage->isAuthenticated();
if ($this->authenticated === null) {
$this->authenticated = $this->storage->isAuthenticated();
}
return $this->authenticated;
}


@@ -117,7 +130,10 @@ public function login($user, string $password = null): void
*/
final public function getIdentity(): ?IIdentity
{
return $this->storage->getIdentity();
if ($this->identity === false) {
$this->identity = $this->storage->getIdentity();
}
return $this->identity;
}


0 comments on commit f53dcb3

Please sign in to comment.
You can’t perform that action at this time.