Completed
Pull Request — master (#9868)
by Morris
269:29 queued 226:47
created
lib/private/User/Database.php 1 patch
Indentation   +397 added lines, -397 removed lines patch added patch discarded remove patch
@@ -76,401 +76,401 @@
 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
-	/** @var string */
96
-	private $table;
97
-
98
-	/**
99
-	 * \OC\User\Database constructor.
100
-	 *
101
-	 * @param EventDispatcher $eventDispatcher
102
-	 * @param string $table
103
-	 */
104
-	public function __construct($eventDispatcher = null, $table = 'users') {
105
-		$this->cache = new CappedMemoryCache();
106
-		$this->table = $table;
107
-		$this->eventDispatcher = $eventDispatcher ? $eventDispatcher : \OC::$server->getEventDispatcher();
108
-	}
109
-
110
-	/**
111
-	 * FIXME: This function should not be required!
112
-	 */
113
-	private function fixDI() {
114
-		if ($this->dbConn === null) {
115
-			$this->dbConn = \OC::$server->getDatabaseConnection();
116
-		}
117
-	}
118
-
119
-	/**
120
-	 * Create a new user
121
-	 *
122
-	 * @param string $uid The username of the user to create
123
-	 * @param string $password The password of the new user
124
-	 * @return bool
125
-	 *
126
-	 * Creates a new user. Basic checking of username is done in OC_User
127
-	 * itself, not in its subclasses.
128
-	 */
129
-	public function createUser(string $uid, string $password): bool {
130
-		$this->fixDI();
131
-
132
-		if (!$this->userExists($uid)) {
133
-			$event = new GenericEvent($password);
134
-			$this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event);
135
-
136
-			$qb = $this->dbConn->getQueryBuilder();
137
-			$qb->insert($this->table)
138
-				->values([
139
-					'uid' => $qb->createNamedParameter($uid),
140
-					'password' => $qb->createNamedParameter(\OC::$server->getHasher()->hash($password)),
141
-					'uid_lower' => $qb->createNamedParameter(mb_strtolower($uid)),
142
-				]);
143
-
144
-			$result = $qb->execute();
145
-
146
-			// Clear cache
147
-			unset($this->cache[$uid]);
148
-
149
-			return $result ? true : false;
150
-		}
151
-
152
-		return false;
153
-	}
154
-
155
-	/**
156
-	 * delete a user
157
-	 *
158
-	 * @param string $uid The username of the user to delete
159
-	 * @return bool
160
-	 *
161
-	 * Deletes a user
162
-	 */
163
-	public function deleteUser($uid) {
164
-		$this->fixDI();
165
-
166
-		// Delete user-group-relation
167
-		$query = $this->dbConn->getQueryBuilder();
168
-		$query->delete($this->table)
169
-			->where($query->expr()->eq('uid_lower', $query->createNamedParameter(mb_strtolower($uid))));
170
-		$result = $query->execute();
171
-
172
-		if (isset($this->cache[$uid])) {
173
-			unset($this->cache[$uid]);
174
-		}
175
-
176
-		return $result ? true : false;
177
-	}
178
-
179
-	/**
180
-	 * Set password
181
-	 *
182
-	 * @param string $uid The username
183
-	 * @param string $password The new password
184
-	 * @return bool
185
-	 *
186
-	 * Change the password of a user
187
-	 */
188
-	public function setPassword(string $uid, string $password): bool {
189
-		$this->fixDI();
190
-
191
-		if ($this->userExists($uid)) {
192
-			$event = new GenericEvent($password);
193
-			$this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event);
194
-
195
-			$hasher = \OC::$server->getHasher();
196
-			$hashedPassword = $hasher->hash($password);
197
-
198
-			$query = $this->dbConn->getQueryBuilder();
199
-			$query->update($this->table)
200
-				->set('password', $query->createNamedParameter($hashedPassword))
201
-				->where($query->expr()->eq('uid_lower', $query->createNamedParameter(mb_strtolower($uid))));
202
-			$result = $query->execute();
203
-
204
-			return $result ? true : false;
205
-		}
206
-
207
-		return false;
208
-	}
209
-
210
-	/**
211
-	 * Set display name
212
-	 *
213
-	 * @param string $uid The username
214
-	 * @param string $displayName The new display name
215
-	 * @return bool
216
-	 *
217
-	 * Change the display name of a user
218
-	 */
219
-	public function setDisplayName(string $uid, string $displayName): bool {
220
-		$this->fixDI();
221
-
222
-		if ($this->userExists($uid)) {
223
-			$query = $this->dbConn->getQueryBuilder();
224
-			$query->update($this->table)
225
-				->set('displayname', $query->createNamedParameter($displayName))
226
-				->where($query->expr()->eq('uid_lower', $query->createNamedParameter(mb_strtolower($uid))));
227
-			$query->execute();
228
-
229
-			$this->cache[$uid]['displayname'] = $displayName;
230
-
231
-			return true;
232
-		}
233
-
234
-		return false;
235
-	}
236
-
237
-	/**
238
-	 * get display name of the user
239
-	 *
240
-	 * @param string $uid user ID of the user
241
-	 * @return string display name
242
-	 */
243
-	public function getDisplayName($uid): string {
244
-		$uid = (string)$uid;
245
-		$this->loadUser($uid);
246
-		return empty($this->cache[$uid]['displayname']) ? $uid : $this->cache[$uid]['displayname'];
247
-	}
248
-
249
-	/**
250
-	 * Get a list of all display names and user ids.
251
-	 *
252
-	 * @param string $search
253
-	 * @param string|null $limit
254
-	 * @param string|null $offset
255
-	 * @return array an array of all displayNames (value) and the corresponding uids (key)
256
-	 */
257
-	public function getDisplayNames($search = '', $limit = null, $offset = null) {
258
-		$this->fixDI();
259
-
260
-		$query = $this->dbConn->getQueryBuilder();
261
-
262
-		$query->select('uid', 'displayname')
263
-			->from($this->table, 'u')
264
-			->leftJoin('u', 'preferences', 'p', $query->expr()->andX(
265
-				$query->expr()->eq('userid', 'uid'),
266
-				$query->expr()->eq('appid', $query->expr()->literal('settings')),
267
-				$query->expr()->eq('configkey', $query->expr()->literal('email')))
268
-			)
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) . '%')))
273
-			->orderBy($query->func()->lower('displayname'), 'ASC')
274
-			->orderBy('uid_lower', 'ASC')
275
-			->setMaxResults($limit)
276
-			->setFirstResult($offset);
277
-
278
-		$result = $query->execute();
279
-		$displayNames = [];
280
-		while ($row = $result->fetch()) {
281
-			$displayNames[(string)$row['uid']] = (string)$row['displayname'];
282
-		}
283
-
284
-		return $displayNames;
285
-	}
286
-
287
-	/**
288
-	 * Check if the password is correct
289
-	 *
290
-	 * @param string $uid The username
291
-	 * @param string $password The password
292
-	 * @return string
293
-	 *
294
-	 * Check if the password is correct without logging in the user
295
-	 * returns the user id or false
296
-	 */
297
-	public function checkPassword(string $uid, string $password) {
298
-		$this->fixDI();
299
-
300
-		$qb = $this->dbConn->getQueryBuilder();
301
-		$qb->select('uid', 'password')
302
-			->from($this->table)
303
-			->where(
304
-				$qb->expr()->eq(
305
-					'uid_lower', $qb->createNamedParameter(mb_strtolower($uid))
306
-				)
307
-			);
308
-		$result = $qb->execute();
309
-		$row = $result->fetch();
310
-		$result->closeCursor();
311
-
312
-		if ($row) {
313
-			$storedHash = $row['password'];
314
-			$newHash = '';
315
-			if (\OC::$server->getHasher()->verify($password, $storedHash, $newHash)) {
316
-				if (!empty($newHash)) {
317
-					$this->setPassword($uid, $password);
318
-				}
319
-				return (string)$row['uid'];
320
-			}
321
-
322
-		}
323
-
324
-		return false;
325
-	}
326
-
327
-	/**
328
-	 * Load an user in the cache
329
-	 *
330
-	 * @param string $uid the username
331
-	 * @return boolean true if user was found, false otherwise
332
-	 */
333
-	private function loadUser($uid) {
334
-		$this->fixDI();
335
-
336
-		$uid = (string)$uid;
337
-		if (!isset($this->cache[$uid])) {
338
-			//guests $uid could be NULL or ''
339
-			if ($uid === '') {
340
-				$this->cache[$uid] = false;
341
-				return true;
342
-			}
343
-
344
-			$qb = $this->dbConn->getQueryBuilder();
345
-			$qb->select('uid', 'displayname')
346
-				->from($this->table)
347
-				->where(
348
-					$qb->expr()->eq(
349
-						'uid_lower', $qb->createNamedParameter(mb_strtolower($uid))
350
-					)
351
-				);
352
-			$result = $qb->execute();
353
-			$row = $result->fetch();
354
-			$result->closeCursor();
355
-
356
-			$this->cache[$uid] = false;
357
-
358
-			// "uid" is primary key, so there can only be a single result
359
-			if ($row !== false) {
360
-				$this->cache[$uid]['uid'] = (string)$row['uid'];
361
-				$this->cache[$uid]['displayname'] = (string)$row['displayname'];
362
-			} else {
363
-				return false;
364
-			}
365
-		}
366
-
367
-		return true;
368
-	}
369
-
370
-	/**
371
-	 * Get a list of all users
372
-	 *
373
-	 * @param string $search
374
-	 * @param null|int $limit
375
-	 * @param null|int $offset
376
-	 * @return string[] an array of all uids
377
-	 */
378
-	public function getUsers($search = '', $limit = null, $offset = null) {
379
-		$users = $this->getDisplayNames($search, $limit, $offset);
380
-		$userIds = array_map(function ($uid) {
381
-			return (string)$uid;
382
-		}, array_keys($users));
383
-		sort($userIds, SORT_STRING | SORT_FLAG_CASE);
384
-		return $userIds;
385
-	}
386
-
387
-	/**
388
-	 * check if a user exists
389
-	 *
390
-	 * @param string $uid the username
391
-	 * @return boolean
392
-	 */
393
-	public function userExists($uid) {
394
-		$this->loadUser($uid);
395
-		return $this->cache[$uid] !== false;
396
-	}
397
-
398
-	/**
399
-	 * get the user's home directory
400
-	 *
401
-	 * @param string $uid the username
402
-	 * @return string|false
403
-	 */
404
-	public function getHome(string $uid) {
405
-		if ($this->userExists($uid)) {
406
-			return \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/' . $uid;
407
-		}
408
-
409
-		return false;
410
-	}
411
-
412
-	/**
413
-	 * @return bool
414
-	 */
415
-	public function hasUserListings() {
416
-		return true;
417
-	}
418
-
419
-	/**
420
-	 * counts the users in the database
421
-	 *
422
-	 * @return int|bool
423
-	 */
424
-	public function countUsers() {
425
-		$this->fixDI();
426
-
427
-		$query = $this->dbConn->getQueryBuilder();
428
-		$query->select($query->func()->count('uid'))
429
-			->from($this->table);
430
-		$result = $query->execute();
431
-
432
-		return $result->fetchColumn();
433
-	}
434
-
435
-	/**
436
-	 * returns the username for the given login name in the correct casing
437
-	 *
438
-	 * @param string $loginName
439
-	 * @return string|false
440
-	 */
441
-	public function loginName2UserName($loginName) {
442
-		if ($this->userExists($loginName)) {
443
-			return $this->cache[$loginName]['uid'];
444
-		}
445
-
446
-		return false;
447
-	}
448
-
449
-	/**
450
-	 * Backend name to be shown in user management
451
-	 *
452
-	 * @return string the name of the backend to be shown
453
-	 */
454
-	public function getBackendName() {
455
-		return 'Database';
456
-	}
457
-
458
-	public static function preLoginNameUsedAsUserName($param) {
459
-		if (!isset($param['uid'])) {
460
-			throw new \Exception('key uid is expected to be set in $param');
461
-		}
462
-
463
-		$backends = \OC::$server->getUserManager()->getBackends();
464
-		foreach ($backends as $backend) {
465
-			if ($backend instanceof Database) {
466
-				/** @var \OC\User\Database $backend */
467
-				$uid = $backend->loginName2UserName($param['uid']);
468
-				if ($uid !== false) {
469
-					$param['uid'] = $uid;
470
-					return;
471
-				}
472
-			}
473
-		}
474
-
475
-	}
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
+    /** @var string */
96
+    private $table;
97
+
98
+    /**
99
+     * \OC\User\Database constructor.
100
+     *
101
+     * @param EventDispatcher $eventDispatcher
102
+     * @param string $table
103
+     */
104
+    public function __construct($eventDispatcher = null, $table = 'users') {
105
+        $this->cache = new CappedMemoryCache();
106
+        $this->table = $table;
107
+        $this->eventDispatcher = $eventDispatcher ? $eventDispatcher : \OC::$server->getEventDispatcher();
108
+    }
109
+
110
+    /**
111
+     * FIXME: This function should not be required!
112
+     */
113
+    private function fixDI() {
114
+        if ($this->dbConn === null) {
115
+            $this->dbConn = \OC::$server->getDatabaseConnection();
116
+        }
117
+    }
118
+
119
+    /**
120
+     * Create a new user
121
+     *
122
+     * @param string $uid The username of the user to create
123
+     * @param string $password The password of the new user
124
+     * @return bool
125
+     *
126
+     * Creates a new user. Basic checking of username is done in OC_User
127
+     * itself, not in its subclasses.
128
+     */
129
+    public function createUser(string $uid, string $password): bool {
130
+        $this->fixDI();
131
+
132
+        if (!$this->userExists($uid)) {
133
+            $event = new GenericEvent($password);
134
+            $this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event);
135
+
136
+            $qb = $this->dbConn->getQueryBuilder();
137
+            $qb->insert($this->table)
138
+                ->values([
139
+                    'uid' => $qb->createNamedParameter($uid),
140
+                    'password' => $qb->createNamedParameter(\OC::$server->getHasher()->hash($password)),
141
+                    'uid_lower' => $qb->createNamedParameter(mb_strtolower($uid)),
142
+                ]);
143
+
144
+            $result = $qb->execute();
145
+
146
+            // Clear cache
147
+            unset($this->cache[$uid]);
148
+
149
+            return $result ? true : false;
150
+        }
151
+
152
+        return false;
153
+    }
154
+
155
+    /**
156
+     * delete a user
157
+     *
158
+     * @param string $uid The username of the user to delete
159
+     * @return bool
160
+     *
161
+     * Deletes a user
162
+     */
163
+    public function deleteUser($uid) {
164
+        $this->fixDI();
165
+
166
+        // Delete user-group-relation
167
+        $query = $this->dbConn->getQueryBuilder();
168
+        $query->delete($this->table)
169
+            ->where($query->expr()->eq('uid_lower', $query->createNamedParameter(mb_strtolower($uid))));
170
+        $result = $query->execute();
171
+
172
+        if (isset($this->cache[$uid])) {
173
+            unset($this->cache[$uid]);
174
+        }
175
+
176
+        return $result ? true : false;
177
+    }
178
+
179
+    /**
180
+     * Set password
181
+     *
182
+     * @param string $uid The username
183
+     * @param string $password The new password
184
+     * @return bool
185
+     *
186
+     * Change the password of a user
187
+     */
188
+    public function setPassword(string $uid, string $password): bool {
189
+        $this->fixDI();
190
+
191
+        if ($this->userExists($uid)) {
192
+            $event = new GenericEvent($password);
193
+            $this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event);
194
+
195
+            $hasher = \OC::$server->getHasher();
196
+            $hashedPassword = $hasher->hash($password);
197
+
198
+            $query = $this->dbConn->getQueryBuilder();
199
+            $query->update($this->table)
200
+                ->set('password', $query->createNamedParameter($hashedPassword))
201
+                ->where($query->expr()->eq('uid_lower', $query->createNamedParameter(mb_strtolower($uid))));
202
+            $result = $query->execute();
203
+
204
+            return $result ? true : false;
205
+        }
206
+
207
+        return false;
208
+    }
209
+
210
+    /**
211
+     * Set display name
212
+     *
213
+     * @param string $uid The username
214
+     * @param string $displayName The new display name
215
+     * @return bool
216
+     *
217
+     * Change the display name of a user
218
+     */
219
+    public function setDisplayName(string $uid, string $displayName): bool {
220
+        $this->fixDI();
221
+
222
+        if ($this->userExists($uid)) {
223
+            $query = $this->dbConn->getQueryBuilder();
224
+            $query->update($this->table)
225
+                ->set('displayname', $query->createNamedParameter($displayName))
226
+                ->where($query->expr()->eq('uid_lower', $query->createNamedParameter(mb_strtolower($uid))));
227
+            $query->execute();
228
+
229
+            $this->cache[$uid]['displayname'] = $displayName;
230
+
231
+            return true;
232
+        }
233
+
234
+        return false;
235
+    }
236
+
237
+    /**
238
+     * get display name of the user
239
+     *
240
+     * @param string $uid user ID of the user
241
+     * @return string display name
242
+     */
243
+    public function getDisplayName($uid): string {
244
+        $uid = (string)$uid;
245
+        $this->loadUser($uid);
246
+        return empty($this->cache[$uid]['displayname']) ? $uid : $this->cache[$uid]['displayname'];
247
+    }
248
+
249
+    /**
250
+     * Get a list of all display names and user ids.
251
+     *
252
+     * @param string $search
253
+     * @param string|null $limit
254
+     * @param string|null $offset
255
+     * @return array an array of all displayNames (value) and the corresponding uids (key)
256
+     */
257
+    public function getDisplayNames($search = '', $limit = null, $offset = null) {
258
+        $this->fixDI();
259
+
260
+        $query = $this->dbConn->getQueryBuilder();
261
+
262
+        $query->select('uid', 'displayname')
263
+            ->from($this->table, 'u')
264
+            ->leftJoin('u', 'preferences', 'p', $query->expr()->andX(
265
+                $query->expr()->eq('userid', 'uid'),
266
+                $query->expr()->eq('appid', $query->expr()->literal('settings')),
267
+                $query->expr()->eq('configkey', $query->expr()->literal('email')))
268
+            )
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) . '%')))
273
+            ->orderBy($query->func()->lower('displayname'), 'ASC')
274
+            ->orderBy('uid_lower', 'ASC')
275
+            ->setMaxResults($limit)
276
+            ->setFirstResult($offset);
277
+
278
+        $result = $query->execute();
279
+        $displayNames = [];
280
+        while ($row = $result->fetch()) {
281
+            $displayNames[(string)$row['uid']] = (string)$row['displayname'];
282
+        }
283
+
284
+        return $displayNames;
285
+    }
286
+
287
+    /**
288
+     * Check if the password is correct
289
+     *
290
+     * @param string $uid The username
291
+     * @param string $password The password
292
+     * @return string
293
+     *
294
+     * Check if the password is correct without logging in the user
295
+     * returns the user id or false
296
+     */
297
+    public function checkPassword(string $uid, string $password) {
298
+        $this->fixDI();
299
+
300
+        $qb = $this->dbConn->getQueryBuilder();
301
+        $qb->select('uid', 'password')
302
+            ->from($this->table)
303
+            ->where(
304
+                $qb->expr()->eq(
305
+                    'uid_lower', $qb->createNamedParameter(mb_strtolower($uid))
306
+                )
307
+            );
308
+        $result = $qb->execute();
309
+        $row = $result->fetch();
310
+        $result->closeCursor();
311
+
312
+        if ($row) {
313
+            $storedHash = $row['password'];
314
+            $newHash = '';
315
+            if (\OC::$server->getHasher()->verify($password, $storedHash, $newHash)) {
316
+                if (!empty($newHash)) {
317
+                    $this->setPassword($uid, $password);
318
+                }
319
+                return (string)$row['uid'];
320
+            }
321
+
322
+        }
323
+
324
+        return false;
325
+    }
326
+
327
+    /**
328
+     * Load an user in the cache
329
+     *
330
+     * @param string $uid the username
331
+     * @return boolean true if user was found, false otherwise
332
+     */
333
+    private function loadUser($uid) {
334
+        $this->fixDI();
335
+
336
+        $uid = (string)$uid;
337
+        if (!isset($this->cache[$uid])) {
338
+            //guests $uid could be NULL or ''
339
+            if ($uid === '') {
340
+                $this->cache[$uid] = false;
341
+                return true;
342
+            }
343
+
344
+            $qb = $this->dbConn->getQueryBuilder();
345
+            $qb->select('uid', 'displayname')
346
+                ->from($this->table)
347
+                ->where(
348
+                    $qb->expr()->eq(
349
+                        'uid_lower', $qb->createNamedParameter(mb_strtolower($uid))
350
+                    )
351
+                );
352
+            $result = $qb->execute();
353
+            $row = $result->fetch();
354
+            $result->closeCursor();
355
+
356
+            $this->cache[$uid] = false;
357
+
358
+            // "uid" is primary key, so there can only be a single result
359
+            if ($row !== false) {
360
+                $this->cache[$uid]['uid'] = (string)$row['uid'];
361
+                $this->cache[$uid]['displayname'] = (string)$row['displayname'];
362
+            } else {
363
+                return false;
364
+            }
365
+        }
366
+
367
+        return true;
368
+    }
369
+
370
+    /**
371
+     * Get a list of all users
372
+     *
373
+     * @param string $search
374
+     * @param null|int $limit
375
+     * @param null|int $offset
376
+     * @return string[] an array of all uids
377
+     */
378
+    public function getUsers($search = '', $limit = null, $offset = null) {
379
+        $users = $this->getDisplayNames($search, $limit, $offset);
380
+        $userIds = array_map(function ($uid) {
381
+            return (string)$uid;
382
+        }, array_keys($users));
383
+        sort($userIds, SORT_STRING | SORT_FLAG_CASE);
384
+        return $userIds;
385
+    }
386
+
387
+    /**
388
+     * check if a user exists
389
+     *
390
+     * @param string $uid the username
391
+     * @return boolean
392
+     */
393
+    public function userExists($uid) {
394
+        $this->loadUser($uid);
395
+        return $this->cache[$uid] !== false;
396
+    }
397
+
398
+    /**
399
+     * get the user's home directory
400
+     *
401
+     * @param string $uid the username
402
+     * @return string|false
403
+     */
404
+    public function getHome(string $uid) {
405
+        if ($this->userExists($uid)) {
406
+            return \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/' . $uid;
407
+        }
408
+
409
+        return false;
410
+    }
411
+
412
+    /**
413
+     * @return bool
414
+     */
415
+    public function hasUserListings() {
416
+        return true;
417
+    }
418
+
419
+    /**
420
+     * counts the users in the database
421
+     *
422
+     * @return int|bool
423
+     */
424
+    public function countUsers() {
425
+        $this->fixDI();
426
+
427
+        $query = $this->dbConn->getQueryBuilder();
428
+        $query->select($query->func()->count('uid'))
429
+            ->from($this->table);
430
+        $result = $query->execute();
431
+
432
+        return $result->fetchColumn();
433
+    }
434
+
435
+    /**
436
+     * returns the username for the given login name in the correct casing
437
+     *
438
+     * @param string $loginName
439
+     * @return string|false
440
+     */
441
+    public function loginName2UserName($loginName) {
442
+        if ($this->userExists($loginName)) {
443
+            return $this->cache[$loginName]['uid'];
444
+        }
445
+
446
+        return false;
447
+    }
448
+
449
+    /**
450
+     * Backend name to be shown in user management
451
+     *
452
+     * @return string the name of the backend to be shown
453
+     */
454
+    public function getBackendName() {
455
+        return 'Database';
456
+    }
457
+
458
+    public static function preLoginNameUsedAsUserName($param) {
459
+        if (!isset($param['uid'])) {
460
+            throw new \Exception('key uid is expected to be set in $param');
461
+        }
462
+
463
+        $backends = \OC::$server->getUserManager()->getBackends();
464
+        foreach ($backends as $backend) {
465
+            if ($backend instanceof Database) {
466
+                /** @var \OC\User\Database $backend */
467
+                $uid = $backend->loginName2UserName($param['uid']);
468
+                if ($uid !== false) {
469
+                    $param['uid'] = $uid;
470
+                    return;
471
+                }
472
+            }
473
+        }
474
+
475
+    }
476 476
 }
Please login to merge, or discard this patch.