From 96e3111f5850a535b1314ebbaf00b24aed82094b Mon Sep 17 00:00:00 2001 From: Eric Lippmann <eric.lippmann@netways.de> Date: Wed, 29 Jul 2015 15:52:56 +0200 Subject: [PATCH] lib: Reorder functions in Auth refs #9660 --- library/Icinga/Authentication/Auth.php | 131 +++++++++++++------------ 1 file changed, 67 insertions(+), 64 deletions(-) diff --git a/library/Icinga/Authentication/Auth.php b/library/Icinga/Authentication/Auth.php index 2aaa9e0ac..74eb0ad96 100644 --- a/library/Icinga/Authentication/Auth.php +++ b/library/Icinga/Authentication/Auth.php @@ -32,6 +32,9 @@ class Auth private $user; + /** + * @see getInstance() + */ private function __construct() { } @@ -59,6 +62,21 @@ class Auth return new AuthChain(); } + /** + * Whether the user is authenticated + * + * @param bool $ignoreSession True to prevent session authentication + * + * @return bool + */ + public function isAuthenticated($ignoreSession = false) + { + if ($this->user === null && ! $ignoreSession) { + $this->authenticateFromSession(); + } + return is_object($this->user); + } + public function setAuthenticated(User $user, $persist = true) { $username = $user->getUsername(); @@ -131,58 +149,14 @@ class Auth } /** - * Writes the current user to the session + * Getter for groups belonged to authenticated user + * + * @return array + * @see User::getGroups */ - public function persistCurrentUser() + public function getGroups() { - Session::getSession()->set('user', $this->user)->refreshId(); - } - - /** - * Try to authenticate the user with the current session - * - * Authentication for externally-authenticated users will be revoked if the username changed or external - * authentication is no longer in effect - */ - public function authenticateFromSession() - { - $this->user = Session::getSession()->get('user'); - if ($this->user !== null && $this->user->isExternalUser() === true) { - list($originUsername, $field) = $this->user->getExternalUserInformation(); - if (! array_key_exists($field, $_SERVER) || $_SERVER[$field] !== $originUsername) { - $this->removeAuthorization(); - } - } - } - - /** - * Whether the user is authenticated - * - * @param bool $ignoreSession True to prevent session authentication - * - * @return bool - */ - public function isAuthenticated($ignoreSession = false) - { - if ($this->user === null && ! $ignoreSession) { - $this->authenticateFromSession(); - } - return is_object($this->user); - } - - /** - * Whether an authenticated user has a given permission - * - * @param string $permission Permission name - * - * @return bool True if the user owns the given permission, false if not or if not authenticated - */ - public function hasPermission($permission) - { - if (! $this->isAuthenticated()) { - return false; - } - return $this->user->can($permission); + return $this->user->getGroups(); } /** @@ -202,15 +176,6 @@ class Auth return $this->user->getRestrictions($restriction); } - /** - * Purges the current authorization information and session - */ - public function removeAuthorization() - { - $this->user = null; - Session::getSession()->purge(); - } - /** * Returns the current user or null if no user is authenticated * @@ -222,13 +187,51 @@ class Auth } /** - * Getter for groups belonged to authenticated user + * Try to authenticate the user with the current session * - * @return array - * @see User::getGroups + * Authentication for externally-authenticated users will be revoked if the username changed or external + * authentication is no longer in effect */ - public function getGroups() + public function authenticateFromSession() { - return $this->user->getGroups(); + $this->user = Session::getSession()->get('user'); + if ($this->user !== null && $this->user->isExternalUser() === true) { + list($originUsername, $field) = $this->user->getExternalUserInformation(); + if (! array_key_exists($field, $_SERVER) || $_SERVER[$field] !== $originUsername) { + $this->removeAuthorization(); + } + } + } + + /** + * Whether an authenticated user has a given permission + * + * @param string $permission Permission name + * + * @return bool True if the user owns the given permission, false if not or if not authenticated + */ + public function hasPermission($permission) + { + if (! $this->isAuthenticated()) { + return false; + } + return $this->user->can($permission); + } + + /** + * Writes the current user to the session + */ + public function persistCurrentUser() + { + Session::getSession()->set('user', $this->user)->refreshId(); + } + + /** + * Purges the current authorization information and session + */ + public function removeAuthorization() + { + $this->user = null; + Session::getSession()->purge(); } }