Completed
Pull Request — master (#9868)
by Robin
57:18 queued 28:47
created
lib/private/User/Database.php 3 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.
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.
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.
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.