@@ -31,66 +31,66 @@ |
||
| 31 | 31 | */ |
| 32 | 32 | class CappedMemoryCache implements ICache, \ArrayAccess { |
| 33 | 33 | |
| 34 | - private $capacity; |
|
| 35 | - private $cache = []; |
|
| 36 | - |
|
| 37 | - public function __construct($capacity = 512) { |
|
| 38 | - $this->capacity = $capacity; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - public function hasKey($key) { |
|
| 42 | - return isset($this->cache[$key]); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - public function get($key) { |
|
| 46 | - return isset($this->cache[$key]) ? $this->cache[$key] : null; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - public function set($key, $value, $ttl = 0) { |
|
| 50 | - if (is_null($key)) { |
|
| 51 | - $this->cache[] = $value; |
|
| 52 | - } else { |
|
| 53 | - $this->cache[$key] = $value; |
|
| 54 | - } |
|
| 55 | - $this->garbageCollect(); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - public function remove($key) { |
|
| 59 | - unset($this->cache[$key]); |
|
| 60 | - return true; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - public function clear($prefix = '') { |
|
| 64 | - $this->cache = []; |
|
| 65 | - return true; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - public function offsetExists($offset) { |
|
| 69 | - return $this->hasKey($offset); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - public function &offsetGet($offset) { |
|
| 73 | - return $this->cache[$offset]; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - public function offsetSet($offset, $value) { |
|
| 77 | - $this->set($offset, $value); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - public function offsetUnset($offset) { |
|
| 81 | - $this->remove($offset); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - public function getData() { |
|
| 85 | - return $this->cache; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - |
|
| 89 | - private function garbageCollect() { |
|
| 90 | - while (count($this->cache) > $this->capacity) { |
|
| 91 | - reset($this->cache); |
|
| 92 | - $key = key($this->cache); |
|
| 93 | - $this->remove($key); |
|
| 94 | - } |
|
| 95 | - } |
|
| 34 | + private $capacity; |
|
| 35 | + private $cache = []; |
|
| 36 | + |
|
| 37 | + public function __construct($capacity = 512) { |
|
| 38 | + $this->capacity = $capacity; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + public function hasKey($key) { |
|
| 42 | + return isset($this->cache[$key]); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + public function get($key) { |
|
| 46 | + return isset($this->cache[$key]) ? $this->cache[$key] : null; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + public function set($key, $value, $ttl = 0) { |
|
| 50 | + if (is_null($key)) { |
|
| 51 | + $this->cache[] = $value; |
|
| 52 | + } else { |
|
| 53 | + $this->cache[$key] = $value; |
|
| 54 | + } |
|
| 55 | + $this->garbageCollect(); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + public function remove($key) { |
|
| 59 | + unset($this->cache[$key]); |
|
| 60 | + return true; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + public function clear($prefix = '') { |
|
| 64 | + $this->cache = []; |
|
| 65 | + return true; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + public function offsetExists($offset) { |
|
| 69 | + return $this->hasKey($offset); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + public function &offsetGet($offset) { |
|
| 73 | + return $this->cache[$offset]; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + public function offsetSet($offset, $value) { |
|
| 77 | + $this->set($offset, $value); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + public function offsetUnset($offset) { |
|
| 81 | + $this->remove($offset); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + public function getData() { |
|
| 85 | + return $this->cache; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + |
|
| 89 | + private function garbageCollect() { |
|
| 90 | + while (count($this->cache) > $this->capacity) { |
|
| 91 | + reset($this->cache); |
|
| 92 | + $key = key($this->cache); |
|
| 93 | + $this->remove($key); |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | 96 | } |
@@ -62,321 +62,321 @@ |
||
| 62 | 62 | * Class for user management in a SQL Database (e.g. MySQL, SQLite) |
| 63 | 63 | */ |
| 64 | 64 | class Database extends Backend implements IUserBackend { |
| 65 | - /** @var CappedMemoryCache */ |
|
| 66 | - private $cache; |
|
| 67 | - |
|
| 68 | - /** @var EventDispatcher */ |
|
| 69 | - private $eventDispatcher; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * \OC\User\Database constructor. |
|
| 73 | - * |
|
| 74 | - * @param EventDispatcher $eventDispatcher |
|
| 75 | - */ |
|
| 76 | - public function __construct($eventDispatcher = null) { |
|
| 77 | - $this->cache = new CappedMemoryCache(); |
|
| 78 | - $this->eventDispatcher = $eventDispatcher ? $eventDispatcher : \OC::$server->getEventDispatcher(); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Create a new user |
|
| 83 | - * @param string $uid The username of the user to create |
|
| 84 | - * @param string $password The password of the new user |
|
| 85 | - * @return bool |
|
| 86 | - * |
|
| 87 | - * Creates a new user. Basic checking of username is done in OC_User |
|
| 88 | - * itself, not in its subclasses. |
|
| 89 | - */ |
|
| 90 | - public function createUser($uid, $password) { |
|
| 91 | - if (!$this->userExists($uid)) { |
|
| 92 | - $event = new GenericEvent($password); |
|
| 93 | - $this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event); |
|
| 94 | - $query = \OC_DB::prepare('INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )'); |
|
| 95 | - $result = $query->execute(array($uid, \OC::$server->getHasher()->hash($password))); |
|
| 96 | - |
|
| 97 | - // Clear cache |
|
| 98 | - unset($this->cache[$uid]); |
|
| 99 | - |
|
| 100 | - return $result ? true : false; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - return false; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * delete a user |
|
| 108 | - * @param string $uid The username of the user to delete |
|
| 109 | - * @return bool |
|
| 110 | - * |
|
| 111 | - * Deletes a user |
|
| 112 | - */ |
|
| 113 | - public function deleteUser($uid) { |
|
| 114 | - // Delete user-group-relation |
|
| 115 | - $query = \OC_DB::prepare('DELETE FROM `*PREFIX*users` WHERE `uid` = ?'); |
|
| 116 | - $result = $query->execute(array($uid)); |
|
| 117 | - |
|
| 118 | - if (isset($this->cache[$uid])) { |
|
| 119 | - unset($this->cache[$uid]); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - return $result ? true : false; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Set password |
|
| 127 | - * @param string $uid The username |
|
| 128 | - * @param string $password The new password |
|
| 129 | - * @return bool |
|
| 130 | - * |
|
| 131 | - * Change the password of a user |
|
| 132 | - */ |
|
| 133 | - public function setPassword($uid, $password) { |
|
| 134 | - if ($this->userExists($uid)) { |
|
| 135 | - $event = new GenericEvent($password); |
|
| 136 | - $this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event); |
|
| 137 | - $query = \OC_DB::prepare('UPDATE `*PREFIX*users` SET `password` = ? WHERE `uid` = ?'); |
|
| 138 | - $result = $query->execute(array(\OC::$server->getHasher()->hash($password), $uid)); |
|
| 139 | - |
|
| 140 | - return $result ? true : false; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - return false; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Set display name |
|
| 148 | - * @param string $uid The username |
|
| 149 | - * @param string $displayName The new display name |
|
| 150 | - * @return bool |
|
| 151 | - * |
|
| 152 | - * Change the display name of a user |
|
| 153 | - */ |
|
| 154 | - public function setDisplayName($uid, $displayName) { |
|
| 155 | - if ($this->userExists($uid)) { |
|
| 156 | - $query = \OC_DB::prepare('UPDATE `*PREFIX*users` SET `displayname` = ? WHERE LOWER(`uid`) = LOWER(?)'); |
|
| 157 | - $query->execute(array($displayName, $uid)); |
|
| 158 | - $this->cache[$uid]['displayname'] = $displayName; |
|
| 159 | - |
|
| 160 | - return true; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - return false; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * get display name of the user |
|
| 168 | - * @param string $uid user ID of the user |
|
| 169 | - * @return string display name |
|
| 170 | - */ |
|
| 171 | - public function getDisplayName($uid) { |
|
| 172 | - $this->loadUser($uid); |
|
| 173 | - return empty($this->cache[$uid]['displayname']) ? $uid : $this->cache[$uid]['displayname']; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Get a list of all display names and user ids. |
|
| 178 | - * |
|
| 179 | - * @param string $search |
|
| 180 | - * @param string|null $limit |
|
| 181 | - * @param string|null $offset |
|
| 182 | - * @return array an array of all displayNames (value) and the corresponding uids (key) |
|
| 183 | - */ |
|
| 184 | - public function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
| 185 | - $parameters = []; |
|
| 186 | - $searchLike = ''; |
|
| 187 | - if ($search !== '') { |
|
| 188 | - $parameters[] = '%' . \OC::$server->getDatabaseConnection()->escapeLikeParameter($search) . '%'; |
|
| 189 | - $parameters[] = '%' . \OC::$server->getDatabaseConnection()->escapeLikeParameter($search) . '%'; |
|
| 190 | - $searchLike = ' WHERE LOWER(`displayname`) LIKE LOWER(?) OR ' |
|
| 191 | - . 'LOWER(`uid`) LIKE LOWER(?)'; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - $displayNames = array(); |
|
| 195 | - $query = \OC_DB::prepare('SELECT `uid`, `displayname` FROM `*PREFIX*users`' |
|
| 196 | - . $searchLike .' ORDER BY LOWER(`displayname`), LOWER(`uid`) ASC', $limit, $offset); |
|
| 197 | - $result = $query->execute($parameters); |
|
| 198 | - while ($row = $result->fetchRow()) { |
|
| 199 | - $displayNames[$row['uid']] = $row['displayname']; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - return $displayNames; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * Check if the password is correct |
|
| 207 | - * @param string $uid The username |
|
| 208 | - * @param string $password The password |
|
| 209 | - * @return string |
|
| 210 | - * |
|
| 211 | - * Check if the password is correct without logging in the user |
|
| 212 | - * returns the user id or false |
|
| 213 | - */ |
|
| 214 | - public function checkPassword($uid, $password) { |
|
| 215 | - $query = \OC_DB::prepare('SELECT `uid`, `password` FROM `*PREFIX*users` WHERE LOWER(`uid`) = LOWER(?)'); |
|
| 216 | - $result = $query->execute(array($uid)); |
|
| 217 | - |
|
| 218 | - $row = $result->fetchRow(); |
|
| 219 | - if ($row) { |
|
| 220 | - $storedHash = $row['password']; |
|
| 221 | - $newHash = ''; |
|
| 222 | - if(\OC::$server->getHasher()->verify($password, $storedHash, $newHash)) { |
|
| 223 | - if(!empty($newHash)) { |
|
| 224 | - $this->setPassword($uid, $password); |
|
| 225 | - } |
|
| 226 | - return $row['uid']; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - return false; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * Load an user in the cache |
|
| 236 | - * @param string $uid the username |
|
| 237 | - * @return boolean true if user was found, false otherwise |
|
| 238 | - */ |
|
| 239 | - private function loadUser($uid) { |
|
| 240 | - $uid = (string) $uid; |
|
| 241 | - if (!isset($this->cache[$uid])) { |
|
| 242 | - //guests $uid could be NULL or '' |
|
| 243 | - if ($uid === '') { |
|
| 244 | - $this->cache[$uid]=false; |
|
| 245 | - return true; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - $query = \OC_DB::prepare('SELECT `uid`, `displayname` FROM `*PREFIX*users` WHERE LOWER(`uid`) = LOWER(?)'); |
|
| 249 | - $result = $query->execute(array($uid)); |
|
| 250 | - |
|
| 251 | - if ($result === false) { |
|
| 252 | - Util::writeLog('core', \OC_DB::getErrorMessage(), Util::ERROR); |
|
| 253 | - return false; |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - $this->cache[$uid] = false; |
|
| 257 | - |
|
| 258 | - // "uid" is primary key, so there can only be a single result |
|
| 259 | - if ($row = $result->fetchRow()) { |
|
| 260 | - $this->cache[$uid]['uid'] = $row['uid']; |
|
| 261 | - $this->cache[$uid]['displayname'] = $row['displayname']; |
|
| 262 | - $result->closeCursor(); |
|
| 263 | - } else { |
|
| 264 | - $result->closeCursor(); |
|
| 265 | - return false; |
|
| 266 | - } |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - return true; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * Get a list of all users |
|
| 274 | - * |
|
| 275 | - * @param string $search |
|
| 276 | - * @param null|int $limit |
|
| 277 | - * @param null|int $offset |
|
| 278 | - * @return string[] an array of all uids |
|
| 279 | - */ |
|
| 280 | - public function getUsers($search = '', $limit = null, $offset = null) { |
|
| 281 | - $parameters = []; |
|
| 282 | - $searchLike = ''; |
|
| 283 | - if ($search !== '') { |
|
| 284 | - $parameters[] = '%' . \OC::$server->getDatabaseConnection()->escapeLikeParameter($search) . '%'; |
|
| 285 | - $searchLike = ' WHERE LOWER(`uid`) LIKE LOWER(?)'; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - $query = \OC_DB::prepare('SELECT `uid` FROM `*PREFIX*users`' . $searchLike . ' ORDER BY LOWER(`uid`) ASC', $limit, $offset); |
|
| 289 | - $result = $query->execute($parameters); |
|
| 290 | - $users = array(); |
|
| 291 | - while ($row = $result->fetchRow()) { |
|
| 292 | - $users[] = $row['uid']; |
|
| 293 | - } |
|
| 294 | - return $users; |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * check if a user exists |
|
| 299 | - * @param string $uid the username |
|
| 300 | - * @return boolean |
|
| 301 | - */ |
|
| 302 | - public function userExists($uid) { |
|
| 303 | - $this->loadUser($uid); |
|
| 304 | - return $this->cache[$uid] !== false; |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - /** |
|
| 308 | - * get the user's home directory |
|
| 309 | - * @param string $uid the username |
|
| 310 | - * @return string|false |
|
| 311 | - */ |
|
| 312 | - public function getHome($uid) { |
|
| 313 | - if ($this->userExists($uid)) { |
|
| 314 | - return \OC::$server->getConfig()->getSystemValue("datadirectory", \OC::$SERVERROOT . "/data") . '/' . $uid; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - return false; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * @return bool |
|
| 322 | - */ |
|
| 323 | - public function hasUserListings() { |
|
| 324 | - return true; |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - /** |
|
| 328 | - * counts the users in the database |
|
| 329 | - * |
|
| 330 | - * @return int|bool |
|
| 331 | - */ |
|
| 332 | - public function countUsers() { |
|
| 333 | - $query = \OC_DB::prepare('SELECT COUNT(*) FROM `*PREFIX*users`'); |
|
| 334 | - $result = $query->execute(); |
|
| 335 | - if ($result === false) { |
|
| 336 | - Util::writeLog('core', \OC_DB::getErrorMessage(), Util::ERROR); |
|
| 337 | - return false; |
|
| 338 | - } |
|
| 339 | - return $result->fetchOne(); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * returns the username for the given login name in the correct casing |
|
| 344 | - * |
|
| 345 | - * @param string $loginName |
|
| 346 | - * @return string|false |
|
| 347 | - */ |
|
| 348 | - public function loginName2UserName($loginName) { |
|
| 349 | - if ($this->userExists($loginName)) { |
|
| 350 | - return $this->cache[$loginName]['uid']; |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - return false; |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * Backend name to be shown in user management |
|
| 358 | - * @return string the name of the backend to be shown |
|
| 359 | - */ |
|
| 360 | - public function getBackendName(){ |
|
| 361 | - return 'Database'; |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - public static function preLoginNameUsedAsUserName($param) { |
|
| 365 | - if(!isset($param['uid'])) { |
|
| 366 | - throw new \Exception('key uid is expected to be set in $param'); |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - $backends = \OC::$server->getUserManager()->getBackends(); |
|
| 370 | - foreach ($backends as $backend) { |
|
| 371 | - if ($backend instanceof Database) { |
|
| 372 | - /** @var \OC\User\Database $backend */ |
|
| 373 | - $uid = $backend->loginName2UserName($param['uid']); |
|
| 374 | - if ($uid !== false) { |
|
| 375 | - $param['uid'] = $uid; |
|
| 376 | - return; |
|
| 377 | - } |
|
| 378 | - } |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - } |
|
| 65 | + /** @var CappedMemoryCache */ |
|
| 66 | + private $cache; |
|
| 67 | + |
|
| 68 | + /** @var EventDispatcher */ |
|
| 69 | + private $eventDispatcher; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * \OC\User\Database constructor. |
|
| 73 | + * |
|
| 74 | + * @param EventDispatcher $eventDispatcher |
|
| 75 | + */ |
|
| 76 | + public function __construct($eventDispatcher = null) { |
|
| 77 | + $this->cache = new CappedMemoryCache(); |
|
| 78 | + $this->eventDispatcher = $eventDispatcher ? $eventDispatcher : \OC::$server->getEventDispatcher(); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Create a new user |
|
| 83 | + * @param string $uid The username of the user to create |
|
| 84 | + * @param string $password The password of the new user |
|
| 85 | + * @return bool |
|
| 86 | + * |
|
| 87 | + * Creates a new user. Basic checking of username is done in OC_User |
|
| 88 | + * itself, not in its subclasses. |
|
| 89 | + */ |
|
| 90 | + public function createUser($uid, $password) { |
|
| 91 | + if (!$this->userExists($uid)) { |
|
| 92 | + $event = new GenericEvent($password); |
|
| 93 | + $this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event); |
|
| 94 | + $query = \OC_DB::prepare('INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )'); |
|
| 95 | + $result = $query->execute(array($uid, \OC::$server->getHasher()->hash($password))); |
|
| 96 | + |
|
| 97 | + // Clear cache |
|
| 98 | + unset($this->cache[$uid]); |
|
| 99 | + |
|
| 100 | + return $result ? true : false; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + return false; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * delete a user |
|
| 108 | + * @param string $uid The username of the user to delete |
|
| 109 | + * @return bool |
|
| 110 | + * |
|
| 111 | + * Deletes a user |
|
| 112 | + */ |
|
| 113 | + public function deleteUser($uid) { |
|
| 114 | + // Delete user-group-relation |
|
| 115 | + $query = \OC_DB::prepare('DELETE FROM `*PREFIX*users` WHERE `uid` = ?'); |
|
| 116 | + $result = $query->execute(array($uid)); |
|
| 117 | + |
|
| 118 | + if (isset($this->cache[$uid])) { |
|
| 119 | + unset($this->cache[$uid]); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + return $result ? true : false; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Set password |
|
| 127 | + * @param string $uid The username |
|
| 128 | + * @param string $password The new password |
|
| 129 | + * @return bool |
|
| 130 | + * |
|
| 131 | + * Change the password of a user |
|
| 132 | + */ |
|
| 133 | + public function setPassword($uid, $password) { |
|
| 134 | + if ($this->userExists($uid)) { |
|
| 135 | + $event = new GenericEvent($password); |
|
| 136 | + $this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event); |
|
| 137 | + $query = \OC_DB::prepare('UPDATE `*PREFIX*users` SET `password` = ? WHERE `uid` = ?'); |
|
| 138 | + $result = $query->execute(array(\OC::$server->getHasher()->hash($password), $uid)); |
|
| 139 | + |
|
| 140 | + return $result ? true : false; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + return false; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Set display name |
|
| 148 | + * @param string $uid The username |
|
| 149 | + * @param string $displayName The new display name |
|
| 150 | + * @return bool |
|
| 151 | + * |
|
| 152 | + * Change the display name of a user |
|
| 153 | + */ |
|
| 154 | + public function setDisplayName($uid, $displayName) { |
|
| 155 | + if ($this->userExists($uid)) { |
|
| 156 | + $query = \OC_DB::prepare('UPDATE `*PREFIX*users` SET `displayname` = ? WHERE LOWER(`uid`) = LOWER(?)'); |
|
| 157 | + $query->execute(array($displayName, $uid)); |
|
| 158 | + $this->cache[$uid]['displayname'] = $displayName; |
|
| 159 | + |
|
| 160 | + return true; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + return false; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * get display name of the user |
|
| 168 | + * @param string $uid user ID of the user |
|
| 169 | + * @return string display name |
|
| 170 | + */ |
|
| 171 | + public function getDisplayName($uid) { |
|
| 172 | + $this->loadUser($uid); |
|
| 173 | + return empty($this->cache[$uid]['displayname']) ? $uid : $this->cache[$uid]['displayname']; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Get a list of all display names and user ids. |
|
| 178 | + * |
|
| 179 | + * @param string $search |
|
| 180 | + * @param string|null $limit |
|
| 181 | + * @param string|null $offset |
|
| 182 | + * @return array an array of all displayNames (value) and the corresponding uids (key) |
|
| 183 | + */ |
|
| 184 | + public function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
| 185 | + $parameters = []; |
|
| 186 | + $searchLike = ''; |
|
| 187 | + if ($search !== '') { |
|
| 188 | + $parameters[] = '%' . \OC::$server->getDatabaseConnection()->escapeLikeParameter($search) . '%'; |
|
| 189 | + $parameters[] = '%' . \OC::$server->getDatabaseConnection()->escapeLikeParameter($search) . '%'; |
|
| 190 | + $searchLike = ' WHERE LOWER(`displayname`) LIKE LOWER(?) OR ' |
|
| 191 | + . 'LOWER(`uid`) LIKE LOWER(?)'; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + $displayNames = array(); |
|
| 195 | + $query = \OC_DB::prepare('SELECT `uid`, `displayname` FROM `*PREFIX*users`' |
|
| 196 | + . $searchLike .' ORDER BY LOWER(`displayname`), LOWER(`uid`) ASC', $limit, $offset); |
|
| 197 | + $result = $query->execute($parameters); |
|
| 198 | + while ($row = $result->fetchRow()) { |
|
| 199 | + $displayNames[$row['uid']] = $row['displayname']; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + return $displayNames; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * Check if the password is correct |
|
| 207 | + * @param string $uid The username |
|
| 208 | + * @param string $password The password |
|
| 209 | + * @return string |
|
| 210 | + * |
|
| 211 | + * Check if the password is correct without logging in the user |
|
| 212 | + * returns the user id or false |
|
| 213 | + */ |
|
| 214 | + public function checkPassword($uid, $password) { |
|
| 215 | + $query = \OC_DB::prepare('SELECT `uid`, `password` FROM `*PREFIX*users` WHERE LOWER(`uid`) = LOWER(?)'); |
|
| 216 | + $result = $query->execute(array($uid)); |
|
| 217 | + |
|
| 218 | + $row = $result->fetchRow(); |
|
| 219 | + if ($row) { |
|
| 220 | + $storedHash = $row['password']; |
|
| 221 | + $newHash = ''; |
|
| 222 | + if(\OC::$server->getHasher()->verify($password, $storedHash, $newHash)) { |
|
| 223 | + if(!empty($newHash)) { |
|
| 224 | + $this->setPassword($uid, $password); |
|
| 225 | + } |
|
| 226 | + return $row['uid']; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + return false; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * Load an user in the cache |
|
| 236 | + * @param string $uid the username |
|
| 237 | + * @return boolean true if user was found, false otherwise |
|
| 238 | + */ |
|
| 239 | + private function loadUser($uid) { |
|
| 240 | + $uid = (string) $uid; |
|
| 241 | + if (!isset($this->cache[$uid])) { |
|
| 242 | + //guests $uid could be NULL or '' |
|
| 243 | + if ($uid === '') { |
|
| 244 | + $this->cache[$uid]=false; |
|
| 245 | + return true; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + $query = \OC_DB::prepare('SELECT `uid`, `displayname` FROM `*PREFIX*users` WHERE LOWER(`uid`) = LOWER(?)'); |
|
| 249 | + $result = $query->execute(array($uid)); |
|
| 250 | + |
|
| 251 | + if ($result === false) { |
|
| 252 | + Util::writeLog('core', \OC_DB::getErrorMessage(), Util::ERROR); |
|
| 253 | + return false; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + $this->cache[$uid] = false; |
|
| 257 | + |
|
| 258 | + // "uid" is primary key, so there can only be a single result |
|
| 259 | + if ($row = $result->fetchRow()) { |
|
| 260 | + $this->cache[$uid]['uid'] = $row['uid']; |
|
| 261 | + $this->cache[$uid]['displayname'] = $row['displayname']; |
|
| 262 | + $result->closeCursor(); |
|
| 263 | + } else { |
|
| 264 | + $result->closeCursor(); |
|
| 265 | + return false; |
|
| 266 | + } |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + return true; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * Get a list of all users |
|
| 274 | + * |
|
| 275 | + * @param string $search |
|
| 276 | + * @param null|int $limit |
|
| 277 | + * @param null|int $offset |
|
| 278 | + * @return string[] an array of all uids |
|
| 279 | + */ |
|
| 280 | + public function getUsers($search = '', $limit = null, $offset = null) { |
|
| 281 | + $parameters = []; |
|
| 282 | + $searchLike = ''; |
|
| 283 | + if ($search !== '') { |
|
| 284 | + $parameters[] = '%' . \OC::$server->getDatabaseConnection()->escapeLikeParameter($search) . '%'; |
|
| 285 | + $searchLike = ' WHERE LOWER(`uid`) LIKE LOWER(?)'; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + $query = \OC_DB::prepare('SELECT `uid` FROM `*PREFIX*users`' . $searchLike . ' ORDER BY LOWER(`uid`) ASC', $limit, $offset); |
|
| 289 | + $result = $query->execute($parameters); |
|
| 290 | + $users = array(); |
|
| 291 | + while ($row = $result->fetchRow()) { |
|
| 292 | + $users[] = $row['uid']; |
|
| 293 | + } |
|
| 294 | + return $users; |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * check if a user exists |
|
| 299 | + * @param string $uid the username |
|
| 300 | + * @return boolean |
|
| 301 | + */ |
|
| 302 | + public function userExists($uid) { |
|
| 303 | + $this->loadUser($uid); |
|
| 304 | + return $this->cache[$uid] !== false; |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + /** |
|
| 308 | + * get the user's home directory |
|
| 309 | + * @param string $uid the username |
|
| 310 | + * @return string|false |
|
| 311 | + */ |
|
| 312 | + public function getHome($uid) { |
|
| 313 | + if ($this->userExists($uid)) { |
|
| 314 | + return \OC::$server->getConfig()->getSystemValue("datadirectory", \OC::$SERVERROOT . "/data") . '/' . $uid; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + return false; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * @return bool |
|
| 322 | + */ |
|
| 323 | + public function hasUserListings() { |
|
| 324 | + return true; |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + /** |
|
| 328 | + * counts the users in the database |
|
| 329 | + * |
|
| 330 | + * @return int|bool |
|
| 331 | + */ |
|
| 332 | + public function countUsers() { |
|
| 333 | + $query = \OC_DB::prepare('SELECT COUNT(*) FROM `*PREFIX*users`'); |
|
| 334 | + $result = $query->execute(); |
|
| 335 | + if ($result === false) { |
|
| 336 | + Util::writeLog('core', \OC_DB::getErrorMessage(), Util::ERROR); |
|
| 337 | + return false; |
|
| 338 | + } |
|
| 339 | + return $result->fetchOne(); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * returns the username for the given login name in the correct casing |
|
| 344 | + * |
|
| 345 | + * @param string $loginName |
|
| 346 | + * @return string|false |
|
| 347 | + */ |
|
| 348 | + public function loginName2UserName($loginName) { |
|
| 349 | + if ($this->userExists($loginName)) { |
|
| 350 | + return $this->cache[$loginName]['uid']; |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + return false; |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * Backend name to be shown in user management |
|
| 358 | + * @return string the name of the backend to be shown |
|
| 359 | + */ |
|
| 360 | + public function getBackendName(){ |
|
| 361 | + return 'Database'; |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + public static function preLoginNameUsedAsUserName($param) { |
|
| 365 | + if(!isset($param['uid'])) { |
|
| 366 | + throw new \Exception('key uid is expected to be set in $param'); |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + $backends = \OC::$server->getUserManager()->getBackends(); |
|
| 370 | + foreach ($backends as $backend) { |
|
| 371 | + if ($backend instanceof Database) { |
|
| 372 | + /** @var \OC\User\Database $backend */ |
|
| 373 | + $uid = $backend->loginName2UserName($param['uid']); |
|
| 374 | + if ($uid !== false) { |
|
| 375 | + $param['uid'] = $uid; |
|
| 376 | + return; |
|
| 377 | + } |
|
| 378 | + } |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + } |
|
| 382 | 382 | } |