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