@@ -75,381 +75,381 @@ |
||
| 75 | 75 | * Class for user management in a SQL Database (e.g. MySQL, SQLite) |
| 76 | 76 | */ |
| 77 | 77 | class Database extends ABackend |
| 78 | - implements ICreateUserBackend, |
|
| 79 | - ISetPasswordBackend, |
|
| 80 | - ISetDisplayNameBackend, |
|
| 81 | - IGetDisplayNameBackend, |
|
| 82 | - ICheckPasswordBackend, |
|
| 83 | - IGetHomeBackend, |
|
| 84 | - ICountUsersBackend { |
|
| 85 | - /** @var CappedMemoryCache */ |
|
| 86 | - private $cache; |
|
| 87 | - |
|
| 88 | - /** @var EventDispatcher */ |
|
| 89 | - private $eventDispatcher; |
|
| 90 | - |
|
| 91 | - /** @var IDBConnection */ |
|
| 92 | - private $dbConn; |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * \OC\User\Database constructor. |
|
| 96 | - * |
|
| 97 | - * @param EventDispatcher $eventDispatcher |
|
| 98 | - */ |
|
| 99 | - public function __construct($eventDispatcher = null) { |
|
| 100 | - $this->cache = new CappedMemoryCache(); |
|
| 101 | - $this->eventDispatcher = $eventDispatcher ? $eventDispatcher : \OC::$server->getEventDispatcher(); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * FIXME: This function should not be required! |
|
| 106 | - */ |
|
| 107 | - private function fixDI() { |
|
| 108 | - if ($this->dbConn === null) { |
|
| 109 | - $this->dbConn = \OC::$server->getDatabaseConnection(); |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Create a new user |
|
| 115 | - * |
|
| 116 | - * @param string $uid The username of the user to create |
|
| 117 | - * @param string $password The password of the new user |
|
| 118 | - * @return bool |
|
| 119 | - * |
|
| 120 | - * Creates a new user. Basic checking of username is done in OC_User |
|
| 121 | - * itself, not in its subclasses. |
|
| 122 | - */ |
|
| 123 | - public function createUser(string $uid, string $password): bool { |
|
| 124 | - $this->fixDI(); |
|
| 125 | - |
|
| 126 | - if (!$this->userExists($uid)) { |
|
| 127 | - $event = new GenericEvent($password); |
|
| 128 | - $this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event); |
|
| 129 | - |
|
| 130 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 131 | - $qb->insert('users') |
|
| 132 | - ->values([ |
|
| 133 | - 'uid' => $qb->createNamedParameter($uid), |
|
| 134 | - 'password' => $qb->createNamedParameter(\OC::$server->getHasher()->hash($password)), |
|
| 135 | - 'uid_lower' => $qb->createNamedParameter(mb_strtolower($uid)), |
|
| 136 | - ]); |
|
| 137 | - |
|
| 138 | - $result = $qb->execute(); |
|
| 139 | - |
|
| 140 | - // Clear cache |
|
| 141 | - unset($this->cache[$uid]); |
|
| 142 | - |
|
| 143 | - return $result ? true : false; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - return false; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * delete a user |
|
| 151 | - * |
|
| 152 | - * @param string $uid The username of the user to delete |
|
| 153 | - * @return bool |
|
| 154 | - * |
|
| 155 | - * Deletes a user |
|
| 156 | - */ |
|
| 157 | - public function deleteUser($uid) { |
|
| 158 | - $this->fixDI(); |
|
| 159 | - |
|
| 160 | - // Delete user-group-relation |
|
| 161 | - $query = \OC_DB::prepare('DELETE FROM `*PREFIX*users` WHERE `uid` = ?'); |
|
| 162 | - $result = $query->execute([$uid]); |
|
| 163 | - |
|
| 164 | - if (isset($this->cache[$uid])) { |
|
| 165 | - unset($this->cache[$uid]); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - return $result ? true : false; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Set password |
|
| 173 | - * |
|
| 174 | - * @param string $uid The username |
|
| 175 | - * @param string $password The new password |
|
| 176 | - * @return bool |
|
| 177 | - * |
|
| 178 | - * Change the password of a user |
|
| 179 | - */ |
|
| 180 | - public function setPassword(string $uid, string $password): bool { |
|
| 181 | - $this->fixDI(); |
|
| 182 | - |
|
| 183 | - if ($this->userExists($uid)) { |
|
| 184 | - $event = new GenericEvent($password); |
|
| 185 | - $this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event); |
|
| 186 | - $query = \OC_DB::prepare('UPDATE `*PREFIX*users` SET `password` = ? WHERE `uid` = ?'); |
|
| 187 | - $result = $query->execute([\OC::$server->getHasher()->hash($password), $uid]); |
|
| 188 | - |
|
| 189 | - return $result ? true : false; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - return false; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Set display name |
|
| 197 | - * |
|
| 198 | - * @param string $uid The username |
|
| 199 | - * @param string $displayName The new display name |
|
| 200 | - * @return bool |
|
| 201 | - * |
|
| 202 | - * Change the display name of a user |
|
| 203 | - */ |
|
| 204 | - public function setDisplayName(string $uid, string $displayName): bool { |
|
| 205 | - $this->fixDI(); |
|
| 206 | - |
|
| 207 | - if ($this->userExists($uid)) { |
|
| 208 | - $query = \OC_DB::prepare('UPDATE `*PREFIX*users` SET `displayname` = ? WHERE LOWER(`uid`) = LOWER(?)'); |
|
| 209 | - $query->execute([$displayName, $uid]); |
|
| 210 | - $this->cache[$uid]['displayname'] = $displayName; |
|
| 211 | - |
|
| 212 | - return true; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - return false; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * get display name of the user |
|
| 220 | - * |
|
| 221 | - * @param string $uid user ID of the user |
|
| 222 | - * @return string display name |
|
| 223 | - */ |
|
| 224 | - public function getDisplayName($uid): string { |
|
| 225 | - $this->loadUser($uid); |
|
| 226 | - return empty($this->cache[$uid]['displayname']) ? $uid : $this->cache[$uid]['displayname']; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - /** |
|
| 230 | - * Get a list of all display names and user ids. |
|
| 231 | - * |
|
| 232 | - * @param string $search |
|
| 233 | - * @param string|null $limit |
|
| 234 | - * @param string|null $offset |
|
| 235 | - * @return array an array of all displayNames (value) and the corresponding uids (key) |
|
| 236 | - */ |
|
| 237 | - public function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
| 238 | - $this->fixDI(); |
|
| 239 | - |
|
| 240 | - $query = $this->dbConn->getQueryBuilder(); |
|
| 241 | - |
|
| 242 | - $query->select('uid', 'displayname') |
|
| 243 | - ->from('users', 'u') |
|
| 244 | - ->leftJoin('u', 'preferences', 'p', $query->expr()->andX( |
|
| 245 | - $query->expr()->eq('userid', 'uid'), |
|
| 246 | - $query->expr()->eq('appid', $query->expr()->literal('settings')), |
|
| 247 | - $query->expr()->eq('configkey', $query->expr()->literal('email'))) |
|
| 248 | - ) |
|
| 249 | - // sqlite doesn't like re-using a single named parameter here |
|
| 250 | - ->where($query->expr()->iLike('uid', $query->createPositionalParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%'))) |
|
| 251 | - ->orWhere($query->expr()->iLike('displayname', $query->createPositionalParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%'))) |
|
| 252 | - ->orWhere($query->expr()->iLike('configvalue', $query->createPositionalParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%'))) |
|
| 253 | - ->orderBy($query->func()->lower('displayname'), 'ASC') |
|
| 254 | - ->orderBy($query->func()->lower('uid'), 'ASC') |
|
| 255 | - ->setMaxResults($limit) |
|
| 256 | - ->setFirstResult($offset); |
|
| 257 | - |
|
| 258 | - $result = $query->execute(); |
|
| 259 | - $displayNames = []; |
|
| 260 | - while ($row = $result->fetch()) { |
|
| 261 | - $displayNames[$row['uid']] = $row['displayname']; |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - return $displayNames; |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - /** |
|
| 268 | - * Check if the password is correct |
|
| 269 | - * |
|
| 270 | - * @param string $uid The username |
|
| 271 | - * @param string $password The password |
|
| 272 | - * @return string |
|
| 273 | - * |
|
| 274 | - * Check if the password is correct without logging in the user |
|
| 275 | - * returns the user id or false |
|
| 276 | - */ |
|
| 277 | - public function checkPassword(string $uid, string $password) { |
|
| 278 | - $this->fixDI(); |
|
| 279 | - |
|
| 280 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 281 | - $qb->select('uid', 'password') |
|
| 282 | - ->from('users') |
|
| 283 | - ->where( |
|
| 284 | - $qb->expr()->eq( |
|
| 285 | - 'uid_lower', $qb->createNamedParameter(mb_strtolower($uid)) |
|
| 286 | - ) |
|
| 287 | - ); |
|
| 288 | - $result = $qb->execute(); |
|
| 289 | - $row = $result->fetch(); |
|
| 290 | - $result->closeCursor(); |
|
| 291 | - |
|
| 292 | - if ($row) { |
|
| 293 | - $storedHash = $row['password']; |
|
| 294 | - $newHash = ''; |
|
| 295 | - if (\OC::$server->getHasher()->verify($password, $storedHash, $newHash)) { |
|
| 296 | - if (!empty($newHash)) { |
|
| 297 | - $this->setPassword($uid, $password); |
|
| 298 | - } |
|
| 299 | - return $row['uid']; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - return false; |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - /** |
|
| 308 | - * Load an user in the cache |
|
| 309 | - * |
|
| 310 | - * @param string $uid the username |
|
| 311 | - * @return boolean true if user was found, false otherwise |
|
| 312 | - */ |
|
| 313 | - private function loadUser($uid) { |
|
| 314 | - $this->fixDI(); |
|
| 315 | - |
|
| 316 | - $uid = (string)$uid; |
|
| 317 | - if (!isset($this->cache[$uid])) { |
|
| 318 | - //guests $uid could be NULL or '' |
|
| 319 | - if ($uid === '') { |
|
| 320 | - $this->cache[$uid] = false; |
|
| 321 | - return true; |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 325 | - $qb->select('uid', 'displayname') |
|
| 326 | - ->from('users') |
|
| 327 | - ->where( |
|
| 328 | - $qb->expr()->eq( |
|
| 329 | - 'uid_lower', $qb->createNamedParameter(mb_strtolower($uid)) |
|
| 330 | - ) |
|
| 331 | - ); |
|
| 332 | - $result = $qb->execute(); |
|
| 333 | - $row = $result->fetch(); |
|
| 334 | - $result->closeCursor(); |
|
| 335 | - |
|
| 336 | - $this->cache[$uid] = false; |
|
| 337 | - |
|
| 338 | - // "uid" is primary key, so there can only be a single result |
|
| 339 | - if ($row !== false) { |
|
| 340 | - $this->cache[$uid]['uid'] = $row['uid']; |
|
| 341 | - $this->cache[$uid]['displayname'] = $row['displayname']; |
|
| 342 | - } else { |
|
| 343 | - return false; |
|
| 344 | - } |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - return true; |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - /** |
|
| 351 | - * Get a list of all users |
|
| 352 | - * |
|
| 353 | - * @param string $search |
|
| 354 | - * @param null|int $limit |
|
| 355 | - * @param null|int $offset |
|
| 356 | - * @return string[] an array of all uids |
|
| 357 | - */ |
|
| 358 | - public function getUsers($search = '', $limit = null, $offset = null) { |
|
| 359 | - $users = $this->getDisplayNames($search, $limit, $offset); |
|
| 360 | - $userIds = array_keys($users); |
|
| 361 | - sort($userIds, SORT_STRING | SORT_FLAG_CASE); |
|
| 362 | - return $userIds; |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - /** |
|
| 366 | - * check if a user exists |
|
| 367 | - * |
|
| 368 | - * @param string $uid the username |
|
| 369 | - * @return boolean |
|
| 370 | - */ |
|
| 371 | - public function userExists($uid) { |
|
| 372 | - $this->loadUser($uid); |
|
| 373 | - return $this->cache[$uid] !== false; |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * get the user's home directory |
|
| 378 | - * |
|
| 379 | - * @param string $uid the username |
|
| 380 | - * @return string|false |
|
| 381 | - */ |
|
| 382 | - public function getHome(string $uid) { |
|
| 383 | - if ($this->userExists($uid)) { |
|
| 384 | - return \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/' . $uid; |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - return false; |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - /** |
|
| 391 | - * @return bool |
|
| 392 | - */ |
|
| 393 | - public function hasUserListings() { |
|
| 394 | - return true; |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * counts the users in the database |
|
| 399 | - * |
|
| 400 | - * @return int|bool |
|
| 401 | - */ |
|
| 402 | - public function countUsers() { |
|
| 403 | - $this->fixDI(); |
|
| 404 | - |
|
| 405 | - $query = \OC_DB::prepare('SELECT COUNT(*) FROM `*PREFIX*users`'); |
|
| 406 | - $result = $query->execute(); |
|
| 407 | - if ($result === false) { |
|
| 408 | - Util::writeLog('core', \OC_DB::getErrorMessage(), Util::ERROR); |
|
| 409 | - return false; |
|
| 410 | - } |
|
| 411 | - return $result->fetchOne(); |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - /** |
|
| 415 | - * returns the username for the given login name in the correct casing |
|
| 416 | - * |
|
| 417 | - * @param string $loginName |
|
| 418 | - * @return string|false |
|
| 419 | - */ |
|
| 420 | - public function loginName2UserName($loginName) { |
|
| 421 | - if ($this->userExists($loginName)) { |
|
| 422 | - return $this->cache[$loginName]['uid']; |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - return false; |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - /** |
|
| 429 | - * Backend name to be shown in user management |
|
| 430 | - * |
|
| 431 | - * @return string the name of the backend to be shown |
|
| 432 | - */ |
|
| 433 | - public function getBackendName() { |
|
| 434 | - return 'Database'; |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - public static function preLoginNameUsedAsUserName($param) { |
|
| 438 | - if (!isset($param['uid'])) { |
|
| 439 | - throw new \Exception('key uid is expected to be set in $param'); |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - $backends = \OC::$server->getUserManager()->getBackends(); |
|
| 443 | - foreach ($backends as $backend) { |
|
| 444 | - if ($backend instanceof Database) { |
|
| 445 | - /** @var \OC\User\Database $backend */ |
|
| 446 | - $uid = $backend->loginName2UserName($param['uid']); |
|
| 447 | - if ($uid !== false) { |
|
| 448 | - $param['uid'] = $uid; |
|
| 449 | - return; |
|
| 450 | - } |
|
| 451 | - } |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - } |
|
| 78 | + implements ICreateUserBackend, |
|
| 79 | + ISetPasswordBackend, |
|
| 80 | + ISetDisplayNameBackend, |
|
| 81 | + IGetDisplayNameBackend, |
|
| 82 | + ICheckPasswordBackend, |
|
| 83 | + IGetHomeBackend, |
|
| 84 | + ICountUsersBackend { |
|
| 85 | + /** @var CappedMemoryCache */ |
|
| 86 | + private $cache; |
|
| 87 | + |
|
| 88 | + /** @var EventDispatcher */ |
|
| 89 | + private $eventDispatcher; |
|
| 90 | + |
|
| 91 | + /** @var IDBConnection */ |
|
| 92 | + private $dbConn; |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * \OC\User\Database constructor. |
|
| 96 | + * |
|
| 97 | + * @param EventDispatcher $eventDispatcher |
|
| 98 | + */ |
|
| 99 | + public function __construct($eventDispatcher = null) { |
|
| 100 | + $this->cache = new CappedMemoryCache(); |
|
| 101 | + $this->eventDispatcher = $eventDispatcher ? $eventDispatcher : \OC::$server->getEventDispatcher(); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * FIXME: This function should not be required! |
|
| 106 | + */ |
|
| 107 | + private function fixDI() { |
|
| 108 | + if ($this->dbConn === null) { |
|
| 109 | + $this->dbConn = \OC::$server->getDatabaseConnection(); |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Create a new user |
|
| 115 | + * |
|
| 116 | + * @param string $uid The username of the user to create |
|
| 117 | + * @param string $password The password of the new user |
|
| 118 | + * @return bool |
|
| 119 | + * |
|
| 120 | + * Creates a new user. Basic checking of username is done in OC_User |
|
| 121 | + * itself, not in its subclasses. |
|
| 122 | + */ |
|
| 123 | + public function createUser(string $uid, string $password): bool { |
|
| 124 | + $this->fixDI(); |
|
| 125 | + |
|
| 126 | + if (!$this->userExists($uid)) { |
|
| 127 | + $event = new GenericEvent($password); |
|
| 128 | + $this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event); |
|
| 129 | + |
|
| 130 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 131 | + $qb->insert('users') |
|
| 132 | + ->values([ |
|
| 133 | + 'uid' => $qb->createNamedParameter($uid), |
|
| 134 | + 'password' => $qb->createNamedParameter(\OC::$server->getHasher()->hash($password)), |
|
| 135 | + 'uid_lower' => $qb->createNamedParameter(mb_strtolower($uid)), |
|
| 136 | + ]); |
|
| 137 | + |
|
| 138 | + $result = $qb->execute(); |
|
| 139 | + |
|
| 140 | + // Clear cache |
|
| 141 | + unset($this->cache[$uid]); |
|
| 142 | + |
|
| 143 | + return $result ? true : false; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + return false; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * delete a user |
|
| 151 | + * |
|
| 152 | + * @param string $uid The username of the user to delete |
|
| 153 | + * @return bool |
|
| 154 | + * |
|
| 155 | + * Deletes a user |
|
| 156 | + */ |
|
| 157 | + public function deleteUser($uid) { |
|
| 158 | + $this->fixDI(); |
|
| 159 | + |
|
| 160 | + // Delete user-group-relation |
|
| 161 | + $query = \OC_DB::prepare('DELETE FROM `*PREFIX*users` WHERE `uid` = ?'); |
|
| 162 | + $result = $query->execute([$uid]); |
|
| 163 | + |
|
| 164 | + if (isset($this->cache[$uid])) { |
|
| 165 | + unset($this->cache[$uid]); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + return $result ? true : false; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Set password |
|
| 173 | + * |
|
| 174 | + * @param string $uid The username |
|
| 175 | + * @param string $password The new password |
|
| 176 | + * @return bool |
|
| 177 | + * |
|
| 178 | + * Change the password of a user |
|
| 179 | + */ |
|
| 180 | + public function setPassword(string $uid, string $password): bool { |
|
| 181 | + $this->fixDI(); |
|
| 182 | + |
|
| 183 | + if ($this->userExists($uid)) { |
|
| 184 | + $event = new GenericEvent($password); |
|
| 185 | + $this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event); |
|
| 186 | + $query = \OC_DB::prepare('UPDATE `*PREFIX*users` SET `password` = ? WHERE `uid` = ?'); |
|
| 187 | + $result = $query->execute([\OC::$server->getHasher()->hash($password), $uid]); |
|
| 188 | + |
|
| 189 | + return $result ? true : false; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + return false; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Set display name |
|
| 197 | + * |
|
| 198 | + * @param string $uid The username |
|
| 199 | + * @param string $displayName The new display name |
|
| 200 | + * @return bool |
|
| 201 | + * |
|
| 202 | + * Change the display name of a user |
|
| 203 | + */ |
|
| 204 | + public function setDisplayName(string $uid, string $displayName): bool { |
|
| 205 | + $this->fixDI(); |
|
| 206 | + |
|
| 207 | + if ($this->userExists($uid)) { |
|
| 208 | + $query = \OC_DB::prepare('UPDATE `*PREFIX*users` SET `displayname` = ? WHERE LOWER(`uid`) = LOWER(?)'); |
|
| 209 | + $query->execute([$displayName, $uid]); |
|
| 210 | + $this->cache[$uid]['displayname'] = $displayName; |
|
| 211 | + |
|
| 212 | + return true; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + return false; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * get display name of the user |
|
| 220 | + * |
|
| 221 | + * @param string $uid user ID of the user |
|
| 222 | + * @return string display name |
|
| 223 | + */ |
|
| 224 | + public function getDisplayName($uid): string { |
|
| 225 | + $this->loadUser($uid); |
|
| 226 | + return empty($this->cache[$uid]['displayname']) ? $uid : $this->cache[$uid]['displayname']; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + /** |
|
| 230 | + * Get a list of all display names and user ids. |
|
| 231 | + * |
|
| 232 | + * @param string $search |
|
| 233 | + * @param string|null $limit |
|
| 234 | + * @param string|null $offset |
|
| 235 | + * @return array an array of all displayNames (value) and the corresponding uids (key) |
|
| 236 | + */ |
|
| 237 | + public function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
| 238 | + $this->fixDI(); |
|
| 239 | + |
|
| 240 | + $query = $this->dbConn->getQueryBuilder(); |
|
| 241 | + |
|
| 242 | + $query->select('uid', 'displayname') |
|
| 243 | + ->from('users', 'u') |
|
| 244 | + ->leftJoin('u', 'preferences', 'p', $query->expr()->andX( |
|
| 245 | + $query->expr()->eq('userid', 'uid'), |
|
| 246 | + $query->expr()->eq('appid', $query->expr()->literal('settings')), |
|
| 247 | + $query->expr()->eq('configkey', $query->expr()->literal('email'))) |
|
| 248 | + ) |
|
| 249 | + // sqlite doesn't like re-using a single named parameter here |
|
| 250 | + ->where($query->expr()->iLike('uid', $query->createPositionalParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%'))) |
|
| 251 | + ->orWhere($query->expr()->iLike('displayname', $query->createPositionalParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%'))) |
|
| 252 | + ->orWhere($query->expr()->iLike('configvalue', $query->createPositionalParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%'))) |
|
| 253 | + ->orderBy($query->func()->lower('displayname'), 'ASC') |
|
| 254 | + ->orderBy($query->func()->lower('uid'), 'ASC') |
|
| 255 | + ->setMaxResults($limit) |
|
| 256 | + ->setFirstResult($offset); |
|
| 257 | + |
|
| 258 | + $result = $query->execute(); |
|
| 259 | + $displayNames = []; |
|
| 260 | + while ($row = $result->fetch()) { |
|
| 261 | + $displayNames[$row['uid']] = $row['displayname']; |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + return $displayNames; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + /** |
|
| 268 | + * Check if the password is correct |
|
| 269 | + * |
|
| 270 | + * @param string $uid The username |
|
| 271 | + * @param string $password The password |
|
| 272 | + * @return string |
|
| 273 | + * |
|
| 274 | + * Check if the password is correct without logging in the user |
|
| 275 | + * returns the user id or false |
|
| 276 | + */ |
|
| 277 | + public function checkPassword(string $uid, string $password) { |
|
| 278 | + $this->fixDI(); |
|
| 279 | + |
|
| 280 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 281 | + $qb->select('uid', 'password') |
|
| 282 | + ->from('users') |
|
| 283 | + ->where( |
|
| 284 | + $qb->expr()->eq( |
|
| 285 | + 'uid_lower', $qb->createNamedParameter(mb_strtolower($uid)) |
|
| 286 | + ) |
|
| 287 | + ); |
|
| 288 | + $result = $qb->execute(); |
|
| 289 | + $row = $result->fetch(); |
|
| 290 | + $result->closeCursor(); |
|
| 291 | + |
|
| 292 | + if ($row) { |
|
| 293 | + $storedHash = $row['password']; |
|
| 294 | + $newHash = ''; |
|
| 295 | + if (\OC::$server->getHasher()->verify($password, $storedHash, $newHash)) { |
|
| 296 | + if (!empty($newHash)) { |
|
| 297 | + $this->setPassword($uid, $password); |
|
| 298 | + } |
|
| 299 | + return $row['uid']; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + return false; |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + /** |
|
| 308 | + * Load an user in the cache |
|
| 309 | + * |
|
| 310 | + * @param string $uid the username |
|
| 311 | + * @return boolean true if user was found, false otherwise |
|
| 312 | + */ |
|
| 313 | + private function loadUser($uid) { |
|
| 314 | + $this->fixDI(); |
|
| 315 | + |
|
| 316 | + $uid = (string)$uid; |
|
| 317 | + if (!isset($this->cache[$uid])) { |
|
| 318 | + //guests $uid could be NULL or '' |
|
| 319 | + if ($uid === '') { |
|
| 320 | + $this->cache[$uid] = false; |
|
| 321 | + return true; |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 325 | + $qb->select('uid', 'displayname') |
|
| 326 | + ->from('users') |
|
| 327 | + ->where( |
|
| 328 | + $qb->expr()->eq( |
|
| 329 | + 'uid_lower', $qb->createNamedParameter(mb_strtolower($uid)) |
|
| 330 | + ) |
|
| 331 | + ); |
|
| 332 | + $result = $qb->execute(); |
|
| 333 | + $row = $result->fetch(); |
|
| 334 | + $result->closeCursor(); |
|
| 335 | + |
|
| 336 | + $this->cache[$uid] = false; |
|
| 337 | + |
|
| 338 | + // "uid" is primary key, so there can only be a single result |
|
| 339 | + if ($row !== false) { |
|
| 340 | + $this->cache[$uid]['uid'] = $row['uid']; |
|
| 341 | + $this->cache[$uid]['displayname'] = $row['displayname']; |
|
| 342 | + } else { |
|
| 343 | + return false; |
|
| 344 | + } |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + return true; |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + /** |
|
| 351 | + * Get a list of all users |
|
| 352 | + * |
|
| 353 | + * @param string $search |
|
| 354 | + * @param null|int $limit |
|
| 355 | + * @param null|int $offset |
|
| 356 | + * @return string[] an array of all uids |
|
| 357 | + */ |
|
| 358 | + public function getUsers($search = '', $limit = null, $offset = null) { |
|
| 359 | + $users = $this->getDisplayNames($search, $limit, $offset); |
|
| 360 | + $userIds = array_keys($users); |
|
| 361 | + sort($userIds, SORT_STRING | SORT_FLAG_CASE); |
|
| 362 | + return $userIds; |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + /** |
|
| 366 | + * check if a user exists |
|
| 367 | + * |
|
| 368 | + * @param string $uid the username |
|
| 369 | + * @return boolean |
|
| 370 | + */ |
|
| 371 | + public function userExists($uid) { |
|
| 372 | + $this->loadUser($uid); |
|
| 373 | + return $this->cache[$uid] !== false; |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * get the user's home directory |
|
| 378 | + * |
|
| 379 | + * @param string $uid the username |
|
| 380 | + * @return string|false |
|
| 381 | + */ |
|
| 382 | + public function getHome(string $uid) { |
|
| 383 | + if ($this->userExists($uid)) { |
|
| 384 | + return \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/' . $uid; |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + return false; |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + /** |
|
| 391 | + * @return bool |
|
| 392 | + */ |
|
| 393 | + public function hasUserListings() { |
|
| 394 | + return true; |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * counts the users in the database |
|
| 399 | + * |
|
| 400 | + * @return int|bool |
|
| 401 | + */ |
|
| 402 | + public function countUsers() { |
|
| 403 | + $this->fixDI(); |
|
| 404 | + |
|
| 405 | + $query = \OC_DB::prepare('SELECT COUNT(*) FROM `*PREFIX*users`'); |
|
| 406 | + $result = $query->execute(); |
|
| 407 | + if ($result === false) { |
|
| 408 | + Util::writeLog('core', \OC_DB::getErrorMessage(), Util::ERROR); |
|
| 409 | + return false; |
|
| 410 | + } |
|
| 411 | + return $result->fetchOne(); |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + /** |
|
| 415 | + * returns the username for the given login name in the correct casing |
|
| 416 | + * |
|
| 417 | + * @param string $loginName |
|
| 418 | + * @return string|false |
|
| 419 | + */ |
|
| 420 | + public function loginName2UserName($loginName) { |
|
| 421 | + if ($this->userExists($loginName)) { |
|
| 422 | + return $this->cache[$loginName]['uid']; |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + return false; |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + /** |
|
| 429 | + * Backend name to be shown in user management |
|
| 430 | + * |
|
| 431 | + * @return string the name of the backend to be shown |
|
| 432 | + */ |
|
| 433 | + public function getBackendName() { |
|
| 434 | + return 'Database'; |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + public static function preLoginNameUsedAsUserName($param) { |
|
| 438 | + if (!isset($param['uid'])) { |
|
| 439 | + throw new \Exception('key uid is expected to be set in $param'); |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + $backends = \OC::$server->getUserManager()->getBackends(); |
|
| 443 | + foreach ($backends as $backend) { |
|
| 444 | + if ($backend instanceof Database) { |
|
| 445 | + /** @var \OC\User\Database $backend */ |
|
| 446 | + $uid = $backend->loginName2UserName($param['uid']); |
|
| 447 | + if ($uid !== false) { |
|
| 448 | + $param['uid'] = $uid; |
|
| 449 | + return; |
|
| 450 | + } |
|
| 451 | + } |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + } |
|
| 455 | 455 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -declare(strict_types=1); |
|
| 2 | +declare(strict_types = 1); |
|
| 3 | 3 | /** |
| 4 | 4 | * @copyright Copyright (c) 2016, ownCloud, Inc. |
| 5 | 5 | * |
@@ -247,9 +247,9 @@ discard block |
||
| 247 | 247 | $query->expr()->eq('configkey', $query->expr()->literal('email'))) |
| 248 | 248 | ) |
| 249 | 249 | // sqlite doesn't like re-using a single named parameter here |
| 250 | - ->where($query->expr()->iLike('uid', $query->createPositionalParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%'))) |
|
| 251 | - ->orWhere($query->expr()->iLike('displayname', $query->createPositionalParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%'))) |
|
| 252 | - ->orWhere($query->expr()->iLike('configvalue', $query->createPositionalParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%'))) |
|
| 250 | + ->where($query->expr()->iLike('uid', $query->createPositionalParameter('%'.$this->dbConn->escapeLikeParameter($search).'%'))) |
|
| 251 | + ->orWhere($query->expr()->iLike('displayname', $query->createPositionalParameter('%'.$this->dbConn->escapeLikeParameter($search).'%'))) |
|
| 252 | + ->orWhere($query->expr()->iLike('configvalue', $query->createPositionalParameter('%'.$this->dbConn->escapeLikeParameter($search).'%'))) |
|
| 253 | 253 | ->orderBy($query->func()->lower('displayname'), 'ASC') |
| 254 | 254 | ->orderBy($query->func()->lower('uid'), 'ASC') |
| 255 | 255 | ->setMaxResults($limit) |
@@ -313,7 +313,7 @@ discard block |
||
| 313 | 313 | private function loadUser($uid) { |
| 314 | 314 | $this->fixDI(); |
| 315 | 315 | |
| 316 | - $uid = (string)$uid; |
|
| 316 | + $uid = (string) $uid; |
|
| 317 | 317 | if (!isset($this->cache[$uid])) { |
| 318 | 318 | //guests $uid could be NULL or '' |
| 319 | 319 | if ($uid === '') { |
@@ -381,7 +381,7 @@ discard block |
||
| 381 | 381 | */ |
| 382 | 382 | public function getHome(string $uid) { |
| 383 | 383 | if ($this->userExists($uid)) { |
| 384 | - return \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/' . $uid; |
|
| 384 | + return \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT.'/data').'/'.$uid; |
|
| 385 | 385 | } |
| 386 | 386 | |
| 387 | 387 | return false; |
@@ -34,45 +34,45 @@ |
||
| 34 | 34 | */ |
| 35 | 35 | class Version14000Date20180404140050 extends SimpleMigrationStep { |
| 36 | 36 | |
| 37 | - /** @var IDBConnection */ |
|
| 38 | - private $connection; |
|
| 37 | + /** @var IDBConnection */ |
|
| 38 | + private $connection; |
|
| 39 | 39 | |
| 40 | - public function __construct(IDBConnection $connection) { |
|
| 41 | - $this->connection = $connection; |
|
| 42 | - } |
|
| 40 | + public function __construct(IDBConnection $connection) { |
|
| 41 | + $this->connection = $connection; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * @param IOutput $output |
|
| 46 | - * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 47 | - * @param array $options |
|
| 48 | - * @return null|ISchemaWrapper |
|
| 49 | - */ |
|
| 50 | - public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { |
|
| 51 | - /** @var ISchemaWrapper $schema */ |
|
| 52 | - $schema = $schemaClosure(); |
|
| 44 | + /** |
|
| 45 | + * @param IOutput $output |
|
| 46 | + * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 47 | + * @param array $options |
|
| 48 | + * @return null|ISchemaWrapper |
|
| 49 | + */ |
|
| 50 | + public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { |
|
| 51 | + /** @var ISchemaWrapper $schema */ |
|
| 52 | + $schema = $schemaClosure(); |
|
| 53 | 53 | |
| 54 | - $table = $schema->getTable('users'); |
|
| 54 | + $table = $schema->getTable('users'); |
|
| 55 | 55 | |
| 56 | - $table->addColumn('uid_lower', 'string', [ |
|
| 57 | - 'notnull' => false, |
|
| 58 | - 'length' => 64, |
|
| 59 | - 'default' => '', |
|
| 60 | - ]); |
|
| 61 | - $table->addIndex(['uid_lower'], 'user_uid_lower'); |
|
| 56 | + $table->addColumn('uid_lower', 'string', [ |
|
| 57 | + 'notnull' => false, |
|
| 58 | + 'length' => 64, |
|
| 59 | + 'default' => '', |
|
| 60 | + ]); |
|
| 61 | + $table->addIndex(['uid_lower'], 'user_uid_lower'); |
|
| 62 | 62 | |
| 63 | - return $schema; |
|
| 64 | - } |
|
| 63 | + return $schema; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * @param IOutput $output |
|
| 68 | - * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 69 | - * @param array $options |
|
| 70 | - */ |
|
| 71 | - public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) { |
|
| 72 | - $qb = $this->connection->getQueryBuilder(); |
|
| 66 | + /** |
|
| 67 | + * @param IOutput $output |
|
| 68 | + * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 69 | + * @param array $options |
|
| 70 | + */ |
|
| 71 | + public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) { |
|
| 72 | + $qb = $this->connection->getQueryBuilder(); |
|
| 73 | 73 | |
| 74 | - $qb->update('users') |
|
| 75 | - ->set('uid_lower', $qb->createFunction('LOWER(`uid`)')); |
|
| 76 | - $qb->execute(); |
|
| 77 | - } |
|
| 74 | + $qb->update('users') |
|
| 75 | + ->set('uid_lower', $qb->createFunction('LOWER(`uid`)')); |
|
| 76 | + $qb->execute(); |
|
| 77 | + } |
|
| 78 | 78 | } |
@@ -1,5 +1,5 @@ |
||
| 1 | 1 | <?php |
| 2 | -declare(strict_types=1); |
|
| 2 | +declare(strict_types = 1); |
|
| 3 | 3 | /** |
| 4 | 4 | * @copyright Copyright (c) 2018 Roeland Jago Douma <[email protected]> |
| 5 | 5 | * |