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