From 08a46e30806434bcd854f7868c3c9c6553d4ac11 Mon Sep 17 00:00:00 2001 From: adrien Date: Thu, 6 Mar 2014 17:57:09 +0100 Subject: add cache for single users --- lib/private/user/database.php | 78 +++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 33 deletions(-) (limited to 'lib/private/user/database.php') diff --git a/lib/private/user/database.php b/lib/private/user/database.php index 15e6643dfb3..911073c133e 100644 --- a/lib/private/user/database.php +++ b/lib/private/user/database.php @@ -42,7 +42,9 @@ class OC_User_Database extends OC_User_Backend { /** * @var PasswordHash */ - static private $hasher = null; + private static $hasher = null; + + protected static $cache = array(); private function getHasher() { if (!self::$hasher) { @@ -135,11 +137,9 @@ class OC_User_Database extends OC_User_Backend { * @return string display name */ public function getDisplayName($uid) { - $query = OC_DB::prepare('SELECT `displayname` FROM `*PREFIX*users` WHERE `uid` = ?'); - $result = $query->execute(array($uid))->fetchAll(); - $displayName = trim($result[0]['displayname'], ' '); - if (!empty($displayName)) { - return $displayName; + $this->loadUser($uid); + if (!empty(self::$cache['uid']['displayname'])) { + return self::$cache['uid']['displayname']; } else { return $uid; } @@ -183,23 +183,41 @@ class OC_User_Database extends OC_User_Backend { $storedHash = $row['password']; if ($storedHash[0] == '$') { //the new phpass based hashing $hasher = $this->getHasher(); - if ($hasher->CheckPassword($password . OC_Config::getValue('passwordsalt', ''), $storedHash)) { - return $row['uid']; - } else { - return false; - } - } else { //old sha1 based hashing - if (sha1($password) == $storedHash) { - //upgrade to new hashing - $this->setPassword($row['uid'], $password); + if ($hasher->CheckPassword($password . OC_Config::getValue('passwordsalt', ''), $storedHash)) return $row['uid']; - } else { - return false; - } + + //old sha1 based hashing + } elseif (sha1($password) == $storedHash) { + //upgrade to new hashing + $this->setPassword($row['uid'], $password); + return $row['uid']; + } + } + + return false; + } + + /** + * @brief Load an user in the cache + * @returns boolean + */ + protected function loadUser($uid) { + if (empty(self::$cache[$uid])) { + $query = OC_DB::prepare('SELECT `uid`, `displayname` FROM `*PREFIX*users` WHERE LOWER(`uid`) = LOWER(?)'); + $result = $query->execute(array($uid)); + + if (OC_DB::isError($result)) { + OC_Log::write('core', OC_DB::getErrorMessage($result), OC_Log::ERROR); + return false; + } + + while ($row = $result->fetchRow()) { + self::$cache[$uid]['uid'] = $row['uid']; + self::$cache[$uid]['displayname'] = $row['displayname']; } - } else { - return false; } + + return true; } /** @@ -224,26 +242,20 @@ class OC_User_Database extends OC_User_Backend { * @return boolean */ public function userExists($uid) { - $query = OC_DB::prepare('SELECT COUNT(*) FROM `*PREFIX*users` WHERE LOWER(`uid`) = LOWER(?)'); - $result = $query->execute(array($uid)); - if (OC_DB::isError($result)) { - OC_Log::write('core', OC_DB::getErrorMessage($result), OC_Log::ERROR); - return false; - } - return $result->fetchOne() > 0; + $this->loadUser($uid); + return empty(self::$cache[$uid]) ? false : true; } /** * @brief get the user's home directory * @param string $uid the username - * @return string|false + * @return boolean */ public function getHome($uid) { - if ($this->userExists($uid)) { + if ($this->userExists($uid)) return OC_Config::getValue("datadirectory", OC::$SERVERROOT . "/data") . '/' . $uid; - } else { - return false; - } + + return false; } /** @@ -256,7 +268,7 @@ class OC_User_Database extends OC_User_Backend { /** * counts the users in the database * - * @return false|string | bool + * @return int | bool */ public function countUsers() { $query = OC_DB::prepare('SELECT COUNT(*) FROM `*PREFIX*users`'); -- cgit v1.2.3 From dde4f2f91720a5eb4ad7db4fdb15dfc115f0e8c0 Mon Sep 17 00:00:00 2001 From: adrien Date: Thu, 6 Mar 2014 22:23:17 +0100 Subject: upgrade the cache user --- lib/private/user/database.php | 52 +++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 14 deletions(-) (limited to 'lib/private/user/database.php') diff --git a/lib/private/user/database.php b/lib/private/user/database.php index 911073c133e..1e3d457eb50 100644 --- a/lib/private/user/database.php +++ b/lib/private/user/database.php @@ -39,13 +39,15 @@ require_once 'phpass/PasswordHash.php'; * Class for user management in a SQL Database (e.g. MySQL, SQLite) */ class OC_User_Database extends OC_User_Backend { + + protected static $cache = array(); + protected static $cache_complete = true; + /** * @var PasswordHash */ private static $hasher = null; - protected static $cache = array(); - private function getHasher() { if (!self::$hasher) { //we don't want to use DES based crypt(), since it doesn't return a hash with a recognisable prefix @@ -199,6 +201,7 @@ class OC_User_Database extends OC_User_Backend { /** * @brief Load an user in the cache + * @param string $uid the username * @returns boolean */ protected function loadUser($uid) { @@ -220,6 +223,32 @@ class OC_User_Database extends OC_User_Backend { return true; } + /** + * @brief Load an user in the cache + * @param string $uid the username + * @returns boolean + */ + protected function loadUsers() { + if (!self::$cache_complete) { + $query = OC_DB::prepare('SELECT `uid`, `displayname` FROM `*PREFIX*users` ORDER BY `uid`'); + $result = $query->execute(array($uid)); + + if (OC_DB::isError($result)) { + OC_Log::write('core', OC_DB::getErrorMessage($result), OC_Log::ERROR); + return false; + } + + while ($row = $result->fetchRow()) { + self::$cache[$uid]['uid'] = $row['uid']; + self::$cache[$uid]['displayname'] = $row['displayname']; + } + + self::$cache_complete = true; + } + + return true; + } + /** * @brief Get a list of all users * @returns array with all uids @@ -227,12 +256,12 @@ class OC_User_Database extends OC_User_Backend { * Get a list of all users. */ public function getUsers($search = '', $limit = null, $offset = null) { - $query = OC_DB::prepare('SELECT `uid` FROM `*PREFIX*users` WHERE LOWER(`uid`) LIKE LOWER(?)', $limit, $offset); - $result = $query->execute(array($search . '%')); + $this->loadUsers(); + $users = array(); - while ($row = $result->fetchRow()) { - $users[] = $row['uid']; - } + foreach (self::$cache as $uid => $value) + $users[] = $uid; + return $users; } @@ -271,13 +300,8 @@ class OC_User_Database extends OC_User_Backend { * @return int | bool */ public function countUsers() { - $query = OC_DB::prepare('SELECT COUNT(*) FROM `*PREFIX*users`'); - $result = $query->execute(); - if (OC_DB::isError($result)) { - OC_Log::write('core', OC_DB::getErrorMessage($result), OC_Log::ERROR); - return false; - } - return $result->fetchOne(); + $this->loadUsers(); + return count(self::$cache); } } -- cgit v1.2.3 From 5cdfc56867d0fe8dc5be9e2112c53e3ddf212104 Mon Sep 17 00:00:00 2001 From: adrien Date: Thu, 6 Mar 2014 22:34:43 +0100 Subject: update the cache when add user --- lib/private/user/database.php | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'lib/private/user/database.php') diff --git a/lib/private/user/database.php b/lib/private/user/database.php index 1e3d457eb50..bbd72fc65e7 100644 --- a/lib/private/user/database.php +++ b/lib/private/user/database.php @@ -39,15 +39,14 @@ require_once 'phpass/PasswordHash.php'; * Class for user management in a SQL Database (e.g. MySQL, SQLite) */ class OC_User_Database extends OC_User_Backend { - - protected static $cache = array(); - protected static $cache_complete = true; - /** * @var PasswordHash */ private static $hasher = null; + protected static $cache = array(); + protected static $cache_complete = false; + private function getHasher() { if (!self::$hasher) { //we don't want to use DES based crypt(), since it doesn't return a hash with a recognisable prefix @@ -68,16 +67,19 @@ class OC_User_Database extends OC_User_Backend { * itself, not in its subclasses. */ public function createUser($uid, $password) { - if ($this->userExists($uid)) { - return false; - } else { + if (!$this->userExists($uid)) { $hasher = $this->getHasher(); $hash = $hasher->HashPassword($password . OC_Config::getValue('passwordsalt', '')); $query = OC_DB::prepare('INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )'); $result = $query->execute(array($uid, $hash)); - - return $result ? true : false; + + if ($result) { + self::$cache[$uid]['uid'] = $uid; + return true; + } } + + return false; } /** @@ -127,10 +129,11 @@ class OC_User_Database extends OC_User_Backend { if ($this->userExists($uid)) { $query = OC_DB::prepare('UPDATE `*PREFIX*users` SET `displayname` = ? WHERE LOWER(`uid`) = ?'); $query->execute(array($displayName, $uid)); + self::$cache[$uid]['displayname'] = $displayName; return true; - } else { - return false; } + + return false; } /** @@ -140,8 +143,8 @@ class OC_User_Database extends OC_User_Backend { */ public function getDisplayName($uid) { $this->loadUser($uid); - if (!empty(self::$cache['uid']['displayname'])) { - return self::$cache['uid']['displayname']; + if (!empty(self::$cache[$uid]['displayname'])) { + return self::$cache[$uid]['displayname']; } else { return $uid; } -- cgit v1.2.3 From fbde24c89ae61294bb6327ccc2b5b150b2ec4928 Mon Sep 17 00:00:00 2001 From: adrien Date: Fri, 7 Mar 2014 08:46:34 +0100 Subject: fix undefined in loadUsers --- lib/private/user/database.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'lib/private/user/database.php') diff --git a/lib/private/user/database.php b/lib/private/user/database.php index bbd72fc65e7..80d52feffc8 100644 --- a/lib/private/user/database.php +++ b/lib/private/user/database.php @@ -54,7 +54,6 @@ class OC_User_Database extends OC_User_Backend { self::$hasher = new PasswordHash(8, $forcePortable); } return self::$hasher; - } /** @@ -112,9 +111,9 @@ class OC_User_Database extends OC_User_Backend { $query->execute(array($hash, $uid)); return true; - } else { - return false; } + + return false; } /** @@ -188,8 +187,9 @@ class OC_User_Database extends OC_User_Backend { $storedHash = $row['password']; if ($storedHash[0] == '$') { //the new phpass based hashing $hasher = $this->getHasher(); - if ($hasher->CheckPassword($password . OC_Config::getValue('passwordsalt', ''), $storedHash)) + if ($hasher->CheckPassword($password . OC_Config::getValue('passwordsalt', ''), $storedHash)) { return $row['uid']; + } //old sha1 based hashing } elseif (sha1($password) == $storedHash) { @@ -234,7 +234,7 @@ class OC_User_Database extends OC_User_Backend { protected function loadUsers() { if (!self::$cache_complete) { $query = OC_DB::prepare('SELECT `uid`, `displayname` FROM `*PREFIX*users` ORDER BY `uid`'); - $result = $query->execute(array($uid)); + $result = $query->execute(); if (OC_DB::isError($result)) { OC_Log::write('core', OC_DB::getErrorMessage($result), OC_Log::ERROR); @@ -262,8 +262,9 @@ class OC_User_Database extends OC_User_Backend { $this->loadUsers(); $users = array(); - foreach (self::$cache as $uid => $value) + foreach (self::$cache as $uid => $value) { $users[] = $uid; + } return $users; } @@ -281,11 +282,12 @@ class OC_User_Database extends OC_User_Backend { /** * @brief get the user's home directory * @param string $uid the username - * @return boolean + * @return string|false */ public function getHome($uid) { - if ($this->userExists($uid)) + if ($this->userExists($uid)) { return OC_Config::getValue("datadirectory", OC::$SERVERROOT . "/data") . '/' . $uid; + } return false; } -- cgit v1.2.3 From d8843f6cd3b37bc7e95b5baa437d7087b09fc6fd Mon Sep 17 00:00:00 2001 From: nishiki Date: Sun, 9 Mar 2014 12:01:35 +0100 Subject: minor clean code --- lib/private/user/database.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/private/user/database.php') diff --git a/lib/private/user/database.php b/lib/private/user/database.php index 80d52feffc8..c472c5445b7 100644 --- a/lib/private/user/database.php +++ b/lib/private/user/database.php @@ -276,7 +276,7 @@ class OC_User_Database extends OC_User_Backend { */ public function userExists($uid) { $this->loadUser($uid); - return empty(self::$cache[$uid]) ? false : true; + return !empty(self::$cache[$uid]); } /** -- cgit v1.2.3 From 75011c2e09e2ee7e9c76a86e938c4d8656225352 Mon Sep 17 00:00:00 2001 From: nishiki Date: Sun, 9 Mar 2014 12:22:47 +0100 Subject: add query result (boolean) for update or delete --- lib/private/user/database.php | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'lib/private/user/database.php') diff --git a/lib/private/user/database.php b/lib/private/user/database.php index c472c5445b7..57ecdda9dad 100644 --- a/lib/private/user/database.php +++ b/lib/private/user/database.php @@ -91,8 +91,9 @@ class OC_User_Database extends OC_User_Backend { public function deleteUser($uid) { // Delete user-group-relation $query = OC_DB::prepare('DELETE FROM `*PREFIX*users` WHERE `uid` = ?'); - $query->execute(array($uid)); - return true; + $result = $query->execute(array($uid)); + + return $result ? true : false; } /** @@ -108,9 +109,9 @@ class OC_User_Database extends OC_User_Backend { $hasher = $this->getHasher(); $hash = $hasher->HashPassword($password . OC_Config::getValue('passwordsalt', '')); $query = OC_DB::prepare('UPDATE `*PREFIX*users` SET `password` = ? WHERE `uid` = ?'); - $query->execute(array($hash, $uid)); + $result = $query->execute(array($hash, $uid)); - return true; + return $result ? true : false; } return false; @@ -142,11 +143,7 @@ class OC_User_Database extends OC_User_Backend { */ public function getDisplayName($uid) { $this->loadUser($uid); - if (!empty(self::$cache[$uid]['displayname'])) { - return self::$cache[$uid]['displayname']; - } else { - return $uid; - } + return empty(self::$cache[$uid]['displayname']) ? $uid : self::$cache[$uid]['displayname']; } /** -- cgit v1.2.3 From ba9d8f7c1a448e0a769ee31d20ee75c1616b22e7 Mon Sep 17 00:00:00 2001 From: nishiki Date: Sun, 9 Mar 2014 12:47:19 +0100 Subject: fix undifined uid --- lib/private/user/database.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/private/user/database.php') diff --git a/lib/private/user/database.php b/lib/private/user/database.php index 57ecdda9dad..591ea2a5c98 100644 --- a/lib/private/user/database.php +++ b/lib/private/user/database.php @@ -239,7 +239,8 @@ class OC_User_Database extends OC_User_Backend { } while ($row = $result->fetchRow()) { - self::$cache[$uid]['uid'] = $row['uid']; + $uid = $row['uid']; + self::$cache[$uid]['uid'] = $uid; self::$cache[$uid]['displayname'] = $row['displayname']; } -- cgit v1.2.3 From 415b1d03bca12c082ff70e44d3b7b8b3fbc8f347 Mon Sep 17 00:00:00 2001 From: adrien Date: Mon, 10 Mar 2014 17:27:51 +0100 Subject: fix cache when remove an user --- lib/private/user/database.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'lib/private/user/database.php') diff --git a/lib/private/user/database.php b/lib/private/user/database.php index 591ea2a5c98..4a9ad1b825e 100644 --- a/lib/private/user/database.php +++ b/lib/private/user/database.php @@ -93,7 +93,14 @@ class OC_User_Database extends OC_User_Backend { $query = OC_DB::prepare('DELETE FROM `*PREFIX*users` WHERE `uid` = ?'); $result = $query->execute(array($uid)); - return $result ? true : false; + if ($result) { + if (isset(self::$cache[$uid])) { + unset(self::$cache[$uid]); + } + return true; + } + + return false; } /** -- cgit v1.2.3 From f827761e7111d4f3b082074facef31efd74939e7 Mon Sep 17 00:00:00 2001 From: adrien Date: Tue, 11 Mar 2014 11:56:46 +0100 Subject: remove static variable, add limit and offset --- lib/private/user/database.php | 80 +++++++++++++++++++++++++++---------------- 1 file changed, 51 insertions(+), 29 deletions(-) (limited to 'lib/private/user/database.php') diff --git a/lib/private/user/database.php b/lib/private/user/database.php index 4a9ad1b825e..5df9c9e2b42 100644 --- a/lib/private/user/database.php +++ b/lib/private/user/database.php @@ -44,8 +44,8 @@ class OC_User_Database extends OC_User_Backend { */ private static $hasher = null; - protected static $cache = array(); - protected static $cache_complete = false; + private $cache = array(); + private $cache_complete = false; private function getHasher() { if (!self::$hasher) { @@ -73,7 +73,7 @@ class OC_User_Database extends OC_User_Backend { $result = $query->execute(array($uid, $hash)); if ($result) { - self::$cache[$uid]['uid'] = $uid; + $this->cache[$uid]['uid'] = $uid; return true; } } @@ -94,8 +94,8 @@ class OC_User_Database extends OC_User_Backend { $result = $query->execute(array($uid)); if ($result) { - if (isset(self::$cache[$uid])) { - unset(self::$cache[$uid]); + if (isset($this->cache[$uid])) { + unset($this->cache[$uid]); } return true; } @@ -136,7 +136,8 @@ class OC_User_Database extends OC_User_Backend { if ($this->userExists($uid)) { $query = OC_DB::prepare('UPDATE `*PREFIX*users` SET `displayname` = ? WHERE LOWER(`uid`) = ?'); $query->execute(array($displayName, $uid)); - self::$cache[$uid]['displayname'] = $displayName; + $this->cache[$uid]['displayname'] = $displayName; + return true; } @@ -150,7 +151,7 @@ class OC_User_Database extends OC_User_Backend { */ public function getDisplayName($uid) { $this->loadUser($uid); - return empty(self::$cache[$uid]['displayname']) ? $uid : self::$cache[$uid]['displayname']; + return empty($this->cache[$uid]['displayname']) ? $uid : $this->cache[$uid]['displayname']; } /** @@ -159,15 +160,24 @@ class OC_User_Database extends OC_User_Backend { * * Get a list of all display names and user ids. */ - public function getDisplayNames($search = '', $limit = null, $offset = null) { + public function getDisplayNames($search = '', $limit = null, $offset = 0) { + $this->loadUsers(); + + $search = strtolower($search); + $i = 0; + $displayNames = array(); - $query = OC_DB::prepare('SELECT `uid`, `displayname` FROM `*PREFIX*users`' - . ' WHERE LOWER(`displayname`) LIKE LOWER(?) OR ' - . 'LOWER(`uid`) LIKE LOWER(?)', $limit, $offset); - $result = $query->execute(array($search . '%', $search . '%')); - $users = array(); - while ($row = $result->fetchRow()) { - $displayNames[$row['uid']] = $row['displayname']; + foreach ($this->cache as $uid => $value) { + if ((preg_match('/^.*'.$search.'.*/', strtolower($uid)) || preg_match('/^.*'.$search.'.*/', strtolower($value['displayname']))) && $offset <= $i) { + $displayNames[$uid] = $value['displayname']; + if (!is_null($limit)) { + $limit--; + if ($limit <= 0) { + break; + } + } + } + $i++; } return $displayNames; @@ -211,8 +221,8 @@ class OC_User_Database extends OC_User_Backend { * @param string $uid the username * @returns boolean */ - protected function loadUser($uid) { - if (empty(self::$cache[$uid])) { + private function loadUser($uid) { + if (empty($this->cache[$uid])) { $query = OC_DB::prepare('SELECT `uid`, `displayname` FROM `*PREFIX*users` WHERE LOWER(`uid`) = LOWER(?)'); $result = $query->execute(array($uid)); @@ -222,8 +232,8 @@ class OC_User_Database extends OC_User_Backend { } while ($row = $result->fetchRow()) { - self::$cache[$uid]['uid'] = $row['uid']; - self::$cache[$uid]['displayname'] = $row['displayname']; + $this->cache[$uid]['uid'] = $row['uid']; + $this->cache[$uid]['displayname'] = $row['displayname']; } } @@ -235,8 +245,8 @@ class OC_User_Database extends OC_User_Backend { * @param string $uid the username * @returns boolean */ - protected function loadUsers() { - if (!self::$cache_complete) { + private function loadUsers() { + if (!$this->cache_complete) { $query = OC_DB::prepare('SELECT `uid`, `displayname` FROM `*PREFIX*users` ORDER BY `uid`'); $result = $query->execute(); @@ -247,11 +257,11 @@ class OC_User_Database extends OC_User_Backend { while ($row = $result->fetchRow()) { $uid = $row['uid']; - self::$cache[$uid]['uid'] = $uid; - self::$cache[$uid]['displayname'] = $row['displayname']; + $this->cache[$uid]['uid'] = $uid; + $this->cache[$uid]['displayname'] = $row['displayname']; } - self::$cache_complete = true; + $this->cache_complete = true; } return true; @@ -263,12 +273,24 @@ class OC_User_Database extends OC_User_Backend { * * Get a list of all users. */ - public function getUsers($search = '', $limit = null, $offset = null) { + public function getUsers($search = '', $limit = null, $offset = 0) { $this->loadUsers(); + $search = strtolower($search); + $i = 0; + $users = array(); - foreach (self::$cache as $uid => $value) { - $users[] = $uid; + foreach ($this->cache as $uid => $value) { + if (preg_match('/^'.$search.'.*/', strtolower($uid)) && $offset <= $i) { + $users[] = $uid; + if (!is_null($limit)) { + $limit--; + if ($limit <= 0) { + break; + } + } + } + $i++; } return $users; @@ -281,7 +303,7 @@ class OC_User_Database extends OC_User_Backend { */ public function userExists($uid) { $this->loadUser($uid); - return !empty(self::$cache[$uid]); + return !empty($this->cache[$uid]); } /** @@ -311,7 +333,7 @@ class OC_User_Database extends OC_User_Backend { */ public function countUsers() { $this->loadUsers(); - return count(self::$cache); + return count($this->cache); } } -- cgit v1.2.3 From ea6f8ba352ac7b3c9069407d6f64664ddee81c4d Mon Sep 17 00:00:00 2001 From: adrien Date: Tue, 11 Mar 2014 16:58:10 +0100 Subject: fix remove cache when delete --- lib/private/user/database.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'lib/private/user/database.php') diff --git a/lib/private/user/database.php b/lib/private/user/database.php index 5df9c9e2b42..683ba90e558 100644 --- a/lib/private/user/database.php +++ b/lib/private/user/database.php @@ -93,14 +93,11 @@ class OC_User_Database extends OC_User_Backend { $query = OC_DB::prepare('DELETE FROM `*PREFIX*users` WHERE `uid` = ?'); $result = $query->execute(array($uid)); - if ($result) { - if (isset($this->cache[$uid])) { - unset($this->cache[$uid]); - } - return true; + if (isset($this->cache[$uid])) { + unset($this->cache[$uid]); } - return false; + return $result ? true : false; } /** -- cgit v1.2.3 From 0da61a26ee842e611f33d32109572c70bbc170b1 Mon Sep 17 00:00:00 2001 From: adrien Date: Fri, 21 Mar 2014 15:50:25 +0100 Subject: remove cache all user --- lib/private/user/database.php | 92 +++++++++++-------------------------------- 1 file changed, 22 insertions(+), 70 deletions(-) (limited to 'lib/private/user/database.php') diff --git a/lib/private/user/database.php b/lib/private/user/database.php index 683ba90e558..706982bb75d 100644 --- a/lib/private/user/database.php +++ b/lib/private/user/database.php @@ -45,7 +45,6 @@ class OC_User_Database extends OC_User_Backend { private static $hasher = null; private $cache = array(); - private $cache_complete = false; private function getHasher() { if (!self::$hasher) { @@ -71,11 +70,8 @@ class OC_User_Database extends OC_User_Backend { $hash = $hasher->HashPassword($password . OC_Config::getValue('passwordsalt', '')); $query = OC_DB::prepare('INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )'); $result = $query->execute(array($uid, $hash)); - - if ($result) { - $this->cache[$uid]['uid'] = $uid; - return true; - } + + return $result ? true : false; } return false; @@ -157,24 +153,15 @@ class OC_User_Database extends OC_User_Backend { * * Get a list of all display names and user ids. */ - public function getDisplayNames($search = '', $limit = null, $offset = 0) { - $this->loadUsers(); - - $search = strtolower($search); - $i = 0; - + public function getDisplayNames($search = '', $limit = null, $offset = null) { $displayNames = array(); - foreach ($this->cache as $uid => $value) { - if ((preg_match('/^.*'.$search.'.*/', strtolower($uid)) || preg_match('/^.*'.$search.'.*/', strtolower($value['displayname']))) && $offset <= $i) { - $displayNames[$uid] = $value['displayname']; - if (!is_null($limit)) { - $limit--; - if ($limit <= 0) { - break; - } - } - } - $i++; + $query = OC_DB::prepare('SELECT `uid`, `displayname` FROM `*PREFIX*users`' + . ' WHERE LOWER(`displayname`) LIKE LOWER(?) OR ' + . 'LOWER(`uid`) LIKE LOWER(?)', $limit, $offset); + $result = $query->execute(array($search . '%', $search . '%')); + $users = array(); + while ($row = $result->fetchRow()) { + $displayNames[$row['uid']] = $row['displayname']; } return $displayNames; @@ -237,59 +224,19 @@ class OC_User_Database extends OC_User_Backend { return true; } - /** - * @brief Load an user in the cache - * @param string $uid the username - * @returns boolean - */ - private function loadUsers() { - if (!$this->cache_complete) { - $query = OC_DB::prepare('SELECT `uid`, `displayname` FROM `*PREFIX*users` ORDER BY `uid`'); - $result = $query->execute(); - - if (OC_DB::isError($result)) { - OC_Log::write('core', OC_DB::getErrorMessage($result), OC_Log::ERROR); - return false; - } - - while ($row = $result->fetchRow()) { - $uid = $row['uid']; - $this->cache[$uid]['uid'] = $uid; - $this->cache[$uid]['displayname'] = $row['displayname']; - } - - $this->cache_complete = true; - } - - return true; - } - /** * @brief Get a list of all users * @returns array with all uids * * Get a list of all users. */ - public function getUsers($search = '', $limit = null, $offset = 0) { - $this->loadUsers(); - - $search = strtolower($search); - $i = 0; - + public function getUsers($search = '', $limit = null, $offset = null) { + $query = OC_DB::prepare('SELECT `uid` FROM `*PREFIX*users` WHERE LOWER(`uid`) LIKE LOWER(?)', $limit, $offset); + $result = $query->execute(array($search . '%')); $users = array(); - foreach ($this->cache as $uid => $value) { - if (preg_match('/^'.$search.'.*/', strtolower($uid)) && $offset <= $i) { - $users[] = $uid; - if (!is_null($limit)) { - $limit--; - if ($limit <= 0) { - break; - } - } - } - $i++; + while ($row = $result->fetchRow()) { + $users[] = $row['uid']; } - return $users; } @@ -329,8 +276,13 @@ class OC_User_Database extends OC_User_Backend { * @return int | bool */ public function countUsers() { - $this->loadUsers(); - return count($this->cache); + $query = OC_DB::prepare('SELECT COUNT(*) FROM `*PREFIX*users`'); + $result = $query->execute(); + if (OC_DB::isError($result)) { + OC_Log::write('core', OC_DB::getErrorMessage($result), OC_Log::ERROR); + return false; + } + return $result->fetchOne(); } } -- cgit v1.2.3