aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/user
diff options
context:
space:
mode:
authoradrien <adrien.waksberg@believedigital.com>2014-03-06 17:57:09 +0100
committeradrien <adrien.waksberg@believedigital.com>2014-03-06 17:57:09 +0100
commit08a46e30806434bcd854f7868c3c9c6553d4ac11 (patch)
tree0afd7d50ffdcf239dc94e6b48f2d47d15beb20f6 /lib/private/user
parent256dca935bbd683515912f555cee90f8cc43b341 (diff)
downloadnextcloud-server-08a46e30806434bcd854f7868c3c9c6553d4ac11.tar.gz
nextcloud-server-08a46e30806434bcd854f7868c3c9c6553d4ac11.zip
add cache for single users
Diffstat (limited to 'lib/private/user')
-rw-r--r--lib/private/user/database.php78
1 files changed, 45 insertions, 33 deletions
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`');