From 54a175a8bd78685c1aac81cea12da3e31eb06e8f Mon Sep 17 00:00:00 2001 From: aydreeihn Date: Tue, 24 Sep 2019 12:15:47 -0500 Subject: [PATCH] Audit Plugin: Reduce Lookups Reduce the number of calls we make to the database for users or agents loggin in or out of the helpdesk. --- include/class.auth.php | 18 ++++++------------ include/class.client.php | 7 +++++++ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/include/class.auth.php b/include/class.auth.php index d5dcec6a11..0a9da406ac 100644 --- a/include/class.auth.php +++ b/include/class.auth.php @@ -683,7 +683,7 @@ function login($user, $bk) { $user->getUserName(), $user->getId(), $_SERVER['REMOTE_ADDR']); $ost->logDebug(_S('User login'), $msg); - $u = User::lookup($user->getId()); + $u = $user->getSessionUser()->getUser(); $type = array('type' => 'login'); Signal::send('person.login', $u, $type); @@ -723,7 +723,7 @@ static function signOut($user) { sprintf(_S("%s logged out [%s]" /* Tokens are and */), $user->getUserName(), $_SERVER['REMOTE_ADDR'])); - $u = User::lookup($user->getId()); + $u = $user->getSessionUser()->getUser(); $type = array('type' => 'logout'); Signal::send('person.logout', $u, $type); } @@ -906,14 +906,9 @@ function authstrike($credentials) { $alert, $admin_alert); if ($username) { - $staffId = Staff::objects()->filter(array('username'=>$username))->values_flat('staff_id')->first(); - if ($staffId) - $staff = Staff::lookup($staffId[0]); - if ($staff) { - $agent = Staff::lookup($staff->getId()); - $type = array('type' => 'login', 'msg' => sprintf('Excessive login attempts (%s)', $authsession['strikes'])); - Signal::send('person.login', $agent, $type); - } + $agent = Staff::lookup($username); + $type = array('type' => 'login', 'msg' => sprintf('Excessive login attempts (%s)', $authsession['strikes'])); + Signal::send('person.login', $agent, $type); } return new AccessDenied(__('Forgot your login info? Contact Admin.')); @@ -986,9 +981,8 @@ function authstrike($credentials) { $user = User::lookup($id); if ($user) { - $u = User::lookup($user->getId()); $type = array('type' => 'login', 'msg' => sprintf('Excessive login attempts (%s)', $authsession['strikes'])); - Signal::send('person.login', $u, $type); + Signal::send('person.login', $user, $type); } } diff --git a/include/class.client.php b/include/class.client.php index d58f153156..d3ec99f1eb 100644 --- a/include/class.client.php +++ b/include/class.client.php @@ -312,6 +312,13 @@ function getAccount() { return $this->_account; } + function getUser() { + if ($this->user === false) + $this->user = User::lookup($this->getId()); + + return $this->user; + } + function getLanguage($flags=false) { if ($acct = $this->getAccount()) return $acct->getLanguage($flags);