From 860a3fb48326ded13778f4ed1dd2116fba3fcf14 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 17 Sep 2018 17:57:51 +0200 Subject: [PATCH] User: added hasAuthenticator() & hasAuthorizator(), deprecated $throw in getAuthenticator() & getAuthorizator() --- src/Security/User.php | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/Security/User.php b/src/Security/User.php index 769678e7..e11b953b 100644 --- a/src/Security/User.php +++ b/src/Security/User.php @@ -146,15 +146,28 @@ public function setAuthenticator(IAuthenticator $handler) /** * Returns authentication handler. */ - final public function getAuthenticator(bool $throw = true): ?IAuthenticator + final public function getAuthenticator(): ?IAuthenticator { - if ($throw && !$this->authenticator) { + if (func_num_args()) { + trigger_error(__METHOD__ . '() parameter $throw is deprecated, use hasAuthenticator()', E_USER_DEPRECATED); + $throw = func_get_arg(0); + } + if (($throw ?? true) && !$this->authenticator) { throw new Nette\InvalidStateException('Authenticator has not been set.'); } return $this->authenticator; } + /** + * Does the authentication exist? + */ + final public function hasAuthenticator(): bool + { + return (bool) $this->authenticator; + } + + /** * Enables log out after inactivity (like '20 minutes'). Accepts flag IUserStorage::CLEAR_IDENTITY. * @param string|null $expire @@ -244,11 +257,24 @@ public function setAuthorizator(IAuthorizator $handler) /** * Returns current authorization handler. */ - final public function getAuthorizator(bool $throw = true): ?IAuthorizator + final public function getAuthorizator(): ?IAuthorizator { - if ($throw && !$this->authorizator) { + if (func_num_args()) { + trigger_error(__METHOD__ . '() parameter $throw is deprecated, use hasAuthorizator()', E_USER_DEPRECATED); + $throw = func_get_arg(0); + } + if (($throw ?? true) && !$this->authorizator) { throw new Nette\InvalidStateException('Authorizator has not been set.'); } return $this->authorizator; } + + + /** + * Does the authorization exist? + */ + final public function hasAuthorizator(): bool + { + return (bool) $this->authorizator; + } }