Completed
Push — master ( c84528...050fa6 )
by Morris
15:18
created
lib/private/legacy/user.php 3 patches
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -94,7 +94,7 @@  discard block
 block discarded – undo
94 94
 				case 'database':
95 95
 				case 'mysql':
96 96
 				case 'sqlite':
97
-					\OCP\Util::writeLog('core', 'Adding user backend ' . $backend . '.', \OCP\Util::DEBUG);
97
+					\OCP\Util::writeLog('core', 'Adding user backend '.$backend.'.', \OCP\Util::DEBUG);
98 98
 					self::$_usedBackends[$backend] = new \OC\User\Database();
99 99
 					\OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
100 100
 					break;
@@ -103,8 +103,8 @@  discard block
 block discarded – undo
103 103
 					\OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
104 104
 					break;
105 105
 				default:
106
-					\OCP\Util::writeLog('core', 'Adding default user backend ' . $backend . '.', \OCP\Util::DEBUG);
107
-					$className = 'OC_USER_' . strtoupper($backend);
106
+					\OCP\Util::writeLog('core', 'Adding default user backend '.$backend.'.', \OCP\Util::DEBUG);
107
+					$className = 'OC_USER_'.strtoupper($backend);
108 108
 					self::$_usedBackends[$backend] = new $className();
109 109
 					\OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
110 110
 					break;
@@ -140,10 +140,10 @@  discard block
 block discarded – undo
140 140
 					self::useBackend($backend);
141 141
 					self::$_setupedBackends[] = $i;
142 142
 				} else {
143
-					\OCP\Util::writeLog('core', 'User backend ' . $class . ' already initialized.', \OCP\Util::DEBUG);
143
+					\OCP\Util::writeLog('core', 'User backend '.$class.' already initialized.', \OCP\Util::DEBUG);
144 144
 				}
145 145
 			} else {
146
-				\OCP\Util::writeLog('core', 'User backend ' . $class . ' not found.', \OCP\Util::ERROR);
146
+				\OCP\Util::writeLog('core', 'User backend '.$class.' not found.', \OCP\Util::ERROR);
147 147
 			}
148 148
 		}
149 149
 	}
@@ -471,7 +471,7 @@  discard block
 block discarded – undo
471 471
 		if ($user) {
472 472
 			return $user->getHome();
473 473
 		} else {
474
-			return \OC::$server->getSystemConfig()->getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid;
474
+			return \OC::$server->getSystemConfig()->getValue('datadirectory', OC::$SERVERROOT.'/data').'/'.$uid;
475 475
 		}
476 476
 	}
477 477
 
Please login to merge, or discard this patch.
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -363,7 +363,7 @@
 block discarded – undo
363 363
 	 * get the display name of the user currently logged in.
364 364
 	 *
365 365
 	 * @param string $uid
366
-	 * @return string|bool uid or false
366
+	 * @return string|false uid or false
367 367
 	 */
368 368
 	public static function getDisplayName($uid = null) {
369 369
 		if ($uid) {
Please login to merge, or discard this patch.
Indentation   +434 added lines, -434 removed lines patch added patch discarded remove patch
@@ -57,438 +57,438 @@
 block discarded – undo
57 57
  */
58 58
 class OC_User {
59 59
 
60
-	/**
61
-	 * @return \OC\User\Session
62
-	 */
63
-	public static function getUserSession() {
64
-		return OC::$server->getUserSession();
65
-	}
66
-
67
-	private static $_usedBackends = array();
68
-
69
-	private static $_setupedBackends = array();
70
-
71
-	// bool, stores if a user want to access a resource anonymously, e.g if they open a public link
72
-	private static $incognitoMode = false;
73
-
74
-	/**
75
-	 * Adds the backend to the list of used backends
76
-	 *
77
-	 * @param string|\OCP\UserInterface $backend default: database The backend to use for user management
78
-	 * @return bool
79
-	 *
80
-	 * Set the User Authentication Module
81
-	 * @suppress PhanDeprecatedFunction
82
-	 */
83
-	public static function useBackend($backend = 'database') {
84
-		if ($backend instanceof \OCP\UserInterface) {
85
-			self::$_usedBackends[get_class($backend)] = $backend;
86
-			\OC::$server->getUserManager()->registerBackend($backend);
87
-		} else {
88
-			// You'll never know what happens
89
-			if (null === $backend OR !is_string($backend)) {
90
-				$backend = 'database';
91
-			}
92
-
93
-			// Load backend
94
-			switch ($backend) {
95
-				case 'database':
96
-				case 'mysql':
97
-				case 'sqlite':
98
-					\OCP\Util::writeLog('core', 'Adding user backend ' . $backend . '.', \OCP\Util::DEBUG);
99
-					self::$_usedBackends[$backend] = new \OC\User\Database();
100
-					\OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
101
-					break;
102
-				case 'dummy':
103
-					self::$_usedBackends[$backend] = new \Test\Util\User\Dummy();
104
-					\OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
105
-					break;
106
-				default:
107
-					\OCP\Util::writeLog('core', 'Adding default user backend ' . $backend . '.', \OCP\Util::DEBUG);
108
-					$className = 'OC_USER_' . strtoupper($backend);
109
-					self::$_usedBackends[$backend] = new $className();
110
-					\OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
111
-					break;
112
-			}
113
-		}
114
-		return true;
115
-	}
116
-
117
-	/**
118
-	 * remove all used backends
119
-	 */
120
-	public static function clearBackends() {
121
-		self::$_usedBackends = array();
122
-		\OC::$server->getUserManager()->clearBackends();
123
-	}
124
-
125
-	/**
126
-	 * setup the configured backends in config.php
127
-	 * @suppress PhanDeprecatedFunction
128
-	 */
129
-	public static function setupBackends() {
130
-		OC_App::loadApps(['prelogin']);
131
-		$backends = \OC::$server->getSystemConfig()->getValue('user_backends', []);
132
-		if (isset($backends['default']) && !$backends['default']) {
133
-			// clear default backends
134
-			self::clearBackends();
135
-		}
136
-		foreach ($backends as $i => $config) {
137
-			if (!is_array($config)) {
138
-				continue;
139
-			}
140
-			$class = $config['class'];
141
-			$arguments = $config['arguments'];
142
-			if (class_exists($class)) {
143
-				if (array_search($i, self::$_setupedBackends) === false) {
144
-					// make a reflection object
145
-					$reflectionObj = new ReflectionClass($class);
146
-
147
-					// use Reflection to create a new instance, using the $args
148
-					$backend = $reflectionObj->newInstanceArgs($arguments);
149
-					self::useBackend($backend);
150
-					self::$_setupedBackends[] = $i;
151
-				} else {
152
-					\OCP\Util::writeLog('core', 'User backend ' . $class . ' already initialized.', \OCP\Util::DEBUG);
153
-				}
154
-			} else {
155
-				\OCP\Util::writeLog('core', 'User backend ' . $class . ' not found.', \OCP\Util::ERROR);
156
-			}
157
-		}
158
-	}
159
-
160
-	/**
161
-	 * Try to login a user, assuming authentication
162
-	 * has already happened (e.g. via Single Sign On).
163
-	 *
164
-	 * Log in a user and regenerate a new session.
165
-	 *
166
-	 * @param \OCP\Authentication\IApacheBackend $backend
167
-	 * @return bool
168
-	 */
169
-	public static function loginWithApache(\OCP\Authentication\IApacheBackend $backend) {
170
-
171
-		$uid = $backend->getCurrentUserId();
172
-		$run = true;
173
-		OC_Hook::emit("OC_User", "pre_login", array("run" => &$run, "uid" => $uid));
174
-
175
-		if ($uid) {
176
-			if (self::getUser() !== $uid) {
177
-				self::setUserId($uid);
178
-				$userSession = self::getUserSession();
179
-				$userSession->setLoginName($uid);
180
-				$request = OC::$server->getRequest();
181
-				$userSession->createSessionToken($request, $uid, $uid);
182
-				// setup the filesystem
183
-				OC_Util::setupFS($uid);
184
-				// first call the post_login hooks, the login-process needs to be
185
-				// completed before we can safely create the users folder.
186
-				// For example encryption needs to initialize the users keys first
187
-				// before we can create the user folder with the skeleton files
188
-				OC_Hook::emit("OC_User", "post_login", array("uid" => $uid, 'password' => ''));
189
-				//trigger creation of user home and /files folder
190
-				\OC::$server->getUserFolder($uid);
191
-			}
192
-			return true;
193
-		}
194
-		return false;
195
-	}
196
-
197
-	/**
198
-	 * Verify with Apache whether user is authenticated.
199
-	 *
200
-	 * @return boolean|null
201
-	 *          true: authenticated
202
-	 *          false: not authenticated
203
-	 *          null: not handled / no backend available
204
-	 */
205
-	public static function handleApacheAuth() {
206
-		$backend = self::findFirstActiveUsedBackend();
207
-		if ($backend) {
208
-			OC_App::loadApps();
209
-
210
-			//setup extra user backends
211
-			self::setupBackends();
212
-			self::getUserSession()->unsetMagicInCookie();
213
-
214
-			return self::loginWithApache($backend);
215
-		}
216
-
217
-		return null;
218
-	}
219
-
220
-
221
-	/**
222
-	 * Sets user id for session and triggers emit
223
-	 *
224
-	 * @param string $uid
225
-	 */
226
-	public static function setUserId($uid) {
227
-		$userSession = \OC::$server->getUserSession();
228
-		$userManager = \OC::$server->getUserManager();
229
-		if ($user = $userManager->get($uid)) {
230
-			$userSession->setUser($user);
231
-		} else {
232
-			\OC::$server->getSession()->set('user_id', $uid);
233
-		}
234
-	}
235
-
236
-	/**
237
-	 * Sets user display name for session
238
-	 *
239
-	 * @param string $uid
240
-	 * @param string $displayName
241
-	 * @return bool Whether the display name could get set
242
-	 */
243
-	public static function setDisplayName($uid, $displayName = null) {
244
-		if (is_null($displayName)) {
245
-			$displayName = $uid;
246
-		}
247
-		$user = \OC::$server->getUserManager()->get($uid);
248
-		if ($user) {
249
-			return $user->setDisplayName($displayName);
250
-		} else {
251
-			return false;
252
-		}
253
-	}
254
-
255
-	/**
256
-	 * Check if the user is logged in, considers also the HTTP basic credentials
257
-	 *
258
-	 * @deprecated use \OC::$server->getUserSession()->isLoggedIn()
259
-	 * @return bool
260
-	 */
261
-	public static function isLoggedIn() {
262
-		return \OC::$server->getUserSession()->isLoggedIn();
263
-	}
264
-
265
-	/**
266
-	 * set incognito mode, e.g. if a user wants to open a public link
267
-	 *
268
-	 * @param bool $status
269
-	 */
270
-	public static function setIncognitoMode($status) {
271
-		self::$incognitoMode = $status;
272
-	}
273
-
274
-	/**
275
-	 * get incognito mode status
276
-	 *
277
-	 * @return bool
278
-	 */
279
-	public static function isIncognitoMode() {
280
-		return self::$incognitoMode;
281
-	}
282
-
283
-	/**
284
-	 * Supplies an attribute to the logout hyperlink. The default behaviour
285
-	 * is to return an href with '?logout=true' appended. However, it can
286
-	 * supply any attribute(s) which are valid for <a>.
287
-	 *
288
-	 * @return string with one or more HTML attributes.
289
-	 */
290
-	public static function getLogoutAttribute() {
291
-		$backend = self::findFirstActiveUsedBackend();
292
-		if ($backend) {
293
-			return $backend->getLogoutAttribute();
294
-		}
295
-
296
-		$logoutUrl = \OC::$server->getURLGenerator()->linkToRouteAbsolute(
297
-			'core.login.logout',
298
-			[
299
-				'requesttoken' => \OCP\Util::callRegister(),
300
-			]
301
-		);
302
-
303
-		return 'href="'.$logoutUrl.'"';
304
-	}
305
-
306
-	/**
307
-	 * Check if the user is an admin user
308
-	 *
309
-	 * @param string $uid uid of the admin
310
-	 * @return bool
311
-	 */
312
-	public static function isAdminUser($uid) {
313
-		$group = \OC::$server->getGroupManager()->get('admin');
314
-		$user = \OC::$server->getUserManager()->get($uid);
315
-		if ($group && $user && $group->inGroup($user) && self::$incognitoMode === false) {
316
-			return true;
317
-		}
318
-		return false;
319
-	}
320
-
321
-
322
-	/**
323
-	 * get the user id of the user currently logged in.
324
-	 *
325
-	 * @return string|bool uid or false
326
-	 */
327
-	public static function getUser() {
328
-		$uid = \OC::$server->getSession() ? \OC::$server->getSession()->get('user_id') : null;
329
-		if (!is_null($uid) && self::$incognitoMode === false) {
330
-			return $uid;
331
-		} else {
332
-			return false;
333
-		}
334
-	}
335
-
336
-	/**
337
-	 * get the display name of the user currently logged in.
338
-	 *
339
-	 * @param string $uid
340
-	 * @return string|bool uid or false
341
-	 */
342
-	public static function getDisplayName($uid = null) {
343
-		if ($uid) {
344
-			$user = \OC::$server->getUserManager()->get($uid);
345
-			if ($user) {
346
-				return $user->getDisplayName();
347
-			} else {
348
-				return $uid;
349
-			}
350
-		} else {
351
-			$user = self::getUserSession()->getUser();
352
-			if ($user) {
353
-				return $user->getDisplayName();
354
-			} else {
355
-				return false;
356
-			}
357
-		}
358
-	}
359
-
360
-	/**
361
-	 * Set password
362
-	 *
363
-	 * @param string $uid The username
364
-	 * @param string $password The new password
365
-	 * @param string $recoveryPassword for the encryption app to reset encryption keys
366
-	 * @return bool
367
-	 *
368
-	 * Change the password of a user
369
-	 */
370
-	public static function setPassword($uid, $password, $recoveryPassword = null) {
371
-		$user = \OC::$server->getUserManager()->get($uid);
372
-		if ($user) {
373
-			return $user->setPassword($password, $recoveryPassword);
374
-		} else {
375
-			return false;
376
-		}
377
-	}
378
-
379
-	/**
380
-	 * Check if the password is correct
381
-	 *
382
-	 * @param string $uid The username
383
-	 * @param string $password The password
384
-	 * @return string|false user id a string on success, false otherwise
385
-	 *
386
-	 * Check if the password is correct without logging in the user
387
-	 * returns the user id or false
388
-	 */
389
-	public static function checkPassword($uid, $password) {
390
-		$manager = \OC::$server->getUserManager();
391
-		$username = $manager->checkPassword($uid, $password);
392
-		if ($username !== false) {
393
-			return $username->getUID();
394
-		}
395
-		return false;
396
-	}
397
-
398
-	/**
399
-	 * @param string $uid The username
400
-	 * @return string
401
-	 *
402
-	 * returns the path to the users home directory
403
-	 * @deprecated Use \OC::$server->getUserManager->getHome()
404
-	 */
405
-	public static function getHome($uid) {
406
-		$user = \OC::$server->getUserManager()->get($uid);
407
-		if ($user) {
408
-			return $user->getHome();
409
-		} else {
410
-			return \OC::$server->getSystemConfig()->getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid;
411
-		}
412
-	}
413
-
414
-	/**
415
-	 * Get a list of all users
416
-	 *
417
-	 * @return array an array of all uids
418
-	 *
419
-	 * Get a list of all users.
420
-	 * @param string $search
421
-	 * @param integer $limit
422
-	 * @param integer $offset
423
-	 */
424
-	public static function getUsers($search = '', $limit = null, $offset = null) {
425
-		$users = \OC::$server->getUserManager()->search($search, $limit, $offset);
426
-		$uids = array();
427
-		foreach ($users as $user) {
428
-			$uids[] = $user->getUID();
429
-		}
430
-		return $uids;
431
-	}
432
-
433
-	/**
434
-	 * Get a list of all users display name
435
-	 *
436
-	 * @param string $search
437
-	 * @param int $limit
438
-	 * @param int $offset
439
-	 * @return array associative array with all display names (value) and corresponding uids (key)
440
-	 *
441
-	 * Get a list of all display names and user ids.
442
-	 * @deprecated Use \OC::$server->getUserManager->searchDisplayName($search, $limit, $offset) instead.
443
-	 */
444
-	public static function getDisplayNames($search = '', $limit = null, $offset = null) {
445
-		$displayNames = array();
446
-		$users = \OC::$server->getUserManager()->searchDisplayName($search, $limit, $offset);
447
-		foreach ($users as $user) {
448
-			$displayNames[$user->getUID()] = $user->getDisplayName();
449
-		}
450
-		return $displayNames;
451
-	}
452
-
453
-	/**
454
-	 * check if a user exists
455
-	 *
456
-	 * @param string $uid the username
457
-	 * @return boolean
458
-	 */
459
-	public static function userExists($uid) {
460
-		return \OC::$server->getUserManager()->userExists($uid);
461
-	}
462
-
463
-	/**
464
-	 * checks if a user is enabled
465
-	 *
466
-	 * @param string $uid
467
-	 * @return bool
468
-	 */
469
-	public static function isEnabled($uid) {
470
-		$user = \OC::$server->getUserManager()->get($uid);
471
-		if ($user) {
472
-			return $user->isEnabled();
473
-		} else {
474
-			return false;
475
-		}
476
-	}
477
-
478
-	/**
479
-	 * Returns the first active backend from self::$_usedBackends.
480
-	 *
481
-	 * @return OCP\Authentication\IApacheBackend|null if no backend active, otherwise OCP\Authentication\IApacheBackend
482
-	 */
483
-	private static function findFirstActiveUsedBackend() {
484
-		foreach (self::$_usedBackends as $backend) {
485
-			if ($backend instanceof OCP\Authentication\IApacheBackend) {
486
-				if ($backend->isSessionActive()) {
487
-					return $backend;
488
-				}
489
-			}
490
-		}
491
-
492
-		return null;
493
-	}
60
+    /**
61
+     * @return \OC\User\Session
62
+     */
63
+    public static function getUserSession() {
64
+        return OC::$server->getUserSession();
65
+    }
66
+
67
+    private static $_usedBackends = array();
68
+
69
+    private static $_setupedBackends = array();
70
+
71
+    // bool, stores if a user want to access a resource anonymously, e.g if they open a public link
72
+    private static $incognitoMode = false;
73
+
74
+    /**
75
+     * Adds the backend to the list of used backends
76
+     *
77
+     * @param string|\OCP\UserInterface $backend default: database The backend to use for user management
78
+     * @return bool
79
+     *
80
+     * Set the User Authentication Module
81
+     * @suppress PhanDeprecatedFunction
82
+     */
83
+    public static function useBackend($backend = 'database') {
84
+        if ($backend instanceof \OCP\UserInterface) {
85
+            self::$_usedBackends[get_class($backend)] = $backend;
86
+            \OC::$server->getUserManager()->registerBackend($backend);
87
+        } else {
88
+            // You'll never know what happens
89
+            if (null === $backend OR !is_string($backend)) {
90
+                $backend = 'database';
91
+            }
92
+
93
+            // Load backend
94
+            switch ($backend) {
95
+                case 'database':
96
+                case 'mysql':
97
+                case 'sqlite':
98
+                    \OCP\Util::writeLog('core', 'Adding user backend ' . $backend . '.', \OCP\Util::DEBUG);
99
+                    self::$_usedBackends[$backend] = new \OC\User\Database();
100
+                    \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
101
+                    break;
102
+                case 'dummy':
103
+                    self::$_usedBackends[$backend] = new \Test\Util\User\Dummy();
104
+                    \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
105
+                    break;
106
+                default:
107
+                    \OCP\Util::writeLog('core', 'Adding default user backend ' . $backend . '.', \OCP\Util::DEBUG);
108
+                    $className = 'OC_USER_' . strtoupper($backend);
109
+                    self::$_usedBackends[$backend] = new $className();
110
+                    \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
111
+                    break;
112
+            }
113
+        }
114
+        return true;
115
+    }
116
+
117
+    /**
118
+     * remove all used backends
119
+     */
120
+    public static function clearBackends() {
121
+        self::$_usedBackends = array();
122
+        \OC::$server->getUserManager()->clearBackends();
123
+    }
124
+
125
+    /**
126
+     * setup the configured backends in config.php
127
+     * @suppress PhanDeprecatedFunction
128
+     */
129
+    public static function setupBackends() {
130
+        OC_App::loadApps(['prelogin']);
131
+        $backends = \OC::$server->getSystemConfig()->getValue('user_backends', []);
132
+        if (isset($backends['default']) && !$backends['default']) {
133
+            // clear default backends
134
+            self::clearBackends();
135
+        }
136
+        foreach ($backends as $i => $config) {
137
+            if (!is_array($config)) {
138
+                continue;
139
+            }
140
+            $class = $config['class'];
141
+            $arguments = $config['arguments'];
142
+            if (class_exists($class)) {
143
+                if (array_search($i, self::$_setupedBackends) === false) {
144
+                    // make a reflection object
145
+                    $reflectionObj = new ReflectionClass($class);
146
+
147
+                    // use Reflection to create a new instance, using the $args
148
+                    $backend = $reflectionObj->newInstanceArgs($arguments);
149
+                    self::useBackend($backend);
150
+                    self::$_setupedBackends[] = $i;
151
+                } else {
152
+                    \OCP\Util::writeLog('core', 'User backend ' . $class . ' already initialized.', \OCP\Util::DEBUG);
153
+                }
154
+            } else {
155
+                \OCP\Util::writeLog('core', 'User backend ' . $class . ' not found.', \OCP\Util::ERROR);
156
+            }
157
+        }
158
+    }
159
+
160
+    /**
161
+     * Try to login a user, assuming authentication
162
+     * has already happened (e.g. via Single Sign On).
163
+     *
164
+     * Log in a user and regenerate a new session.
165
+     *
166
+     * @param \OCP\Authentication\IApacheBackend $backend
167
+     * @return bool
168
+     */
169
+    public static function loginWithApache(\OCP\Authentication\IApacheBackend $backend) {
170
+
171
+        $uid = $backend->getCurrentUserId();
172
+        $run = true;
173
+        OC_Hook::emit("OC_User", "pre_login", array("run" => &$run, "uid" => $uid));
174
+
175
+        if ($uid) {
176
+            if (self::getUser() !== $uid) {
177
+                self::setUserId($uid);
178
+                $userSession = self::getUserSession();
179
+                $userSession->setLoginName($uid);
180
+                $request = OC::$server->getRequest();
181
+                $userSession->createSessionToken($request, $uid, $uid);
182
+                // setup the filesystem
183
+                OC_Util::setupFS($uid);
184
+                // first call the post_login hooks, the login-process needs to be
185
+                // completed before we can safely create the users folder.
186
+                // For example encryption needs to initialize the users keys first
187
+                // before we can create the user folder with the skeleton files
188
+                OC_Hook::emit("OC_User", "post_login", array("uid" => $uid, 'password' => ''));
189
+                //trigger creation of user home and /files folder
190
+                \OC::$server->getUserFolder($uid);
191
+            }
192
+            return true;
193
+        }
194
+        return false;
195
+    }
196
+
197
+    /**
198
+     * Verify with Apache whether user is authenticated.
199
+     *
200
+     * @return boolean|null
201
+     *          true: authenticated
202
+     *          false: not authenticated
203
+     *          null: not handled / no backend available
204
+     */
205
+    public static function handleApacheAuth() {
206
+        $backend = self::findFirstActiveUsedBackend();
207
+        if ($backend) {
208
+            OC_App::loadApps();
209
+
210
+            //setup extra user backends
211
+            self::setupBackends();
212
+            self::getUserSession()->unsetMagicInCookie();
213
+
214
+            return self::loginWithApache($backend);
215
+        }
216
+
217
+        return null;
218
+    }
219
+
220
+
221
+    /**
222
+     * Sets user id for session and triggers emit
223
+     *
224
+     * @param string $uid
225
+     */
226
+    public static function setUserId($uid) {
227
+        $userSession = \OC::$server->getUserSession();
228
+        $userManager = \OC::$server->getUserManager();
229
+        if ($user = $userManager->get($uid)) {
230
+            $userSession->setUser($user);
231
+        } else {
232
+            \OC::$server->getSession()->set('user_id', $uid);
233
+        }
234
+    }
235
+
236
+    /**
237
+     * Sets user display name for session
238
+     *
239
+     * @param string $uid
240
+     * @param string $displayName
241
+     * @return bool Whether the display name could get set
242
+     */
243
+    public static function setDisplayName($uid, $displayName = null) {
244
+        if (is_null($displayName)) {
245
+            $displayName = $uid;
246
+        }
247
+        $user = \OC::$server->getUserManager()->get($uid);
248
+        if ($user) {
249
+            return $user->setDisplayName($displayName);
250
+        } else {
251
+            return false;
252
+        }
253
+    }
254
+
255
+    /**
256
+     * Check if the user is logged in, considers also the HTTP basic credentials
257
+     *
258
+     * @deprecated use \OC::$server->getUserSession()->isLoggedIn()
259
+     * @return bool
260
+     */
261
+    public static function isLoggedIn() {
262
+        return \OC::$server->getUserSession()->isLoggedIn();
263
+    }
264
+
265
+    /**
266
+     * set incognito mode, e.g. if a user wants to open a public link
267
+     *
268
+     * @param bool $status
269
+     */
270
+    public static function setIncognitoMode($status) {
271
+        self::$incognitoMode = $status;
272
+    }
273
+
274
+    /**
275
+     * get incognito mode status
276
+     *
277
+     * @return bool
278
+     */
279
+    public static function isIncognitoMode() {
280
+        return self::$incognitoMode;
281
+    }
282
+
283
+    /**
284
+     * Supplies an attribute to the logout hyperlink. The default behaviour
285
+     * is to return an href with '?logout=true' appended. However, it can
286
+     * supply any attribute(s) which are valid for <a>.
287
+     *
288
+     * @return string with one or more HTML attributes.
289
+     */
290
+    public static function getLogoutAttribute() {
291
+        $backend = self::findFirstActiveUsedBackend();
292
+        if ($backend) {
293
+            return $backend->getLogoutAttribute();
294
+        }
295
+
296
+        $logoutUrl = \OC::$server->getURLGenerator()->linkToRouteAbsolute(
297
+            'core.login.logout',
298
+            [
299
+                'requesttoken' => \OCP\Util::callRegister(),
300
+            ]
301
+        );
302
+
303
+        return 'href="'.$logoutUrl.'"';
304
+    }
305
+
306
+    /**
307
+     * Check if the user is an admin user
308
+     *
309
+     * @param string $uid uid of the admin
310
+     * @return bool
311
+     */
312
+    public static function isAdminUser($uid) {
313
+        $group = \OC::$server->getGroupManager()->get('admin');
314
+        $user = \OC::$server->getUserManager()->get($uid);
315
+        if ($group && $user && $group->inGroup($user) && self::$incognitoMode === false) {
316
+            return true;
317
+        }
318
+        return false;
319
+    }
320
+
321
+
322
+    /**
323
+     * get the user id of the user currently logged in.
324
+     *
325
+     * @return string|bool uid or false
326
+     */
327
+    public static function getUser() {
328
+        $uid = \OC::$server->getSession() ? \OC::$server->getSession()->get('user_id') : null;
329
+        if (!is_null($uid) && self::$incognitoMode === false) {
330
+            return $uid;
331
+        } else {
332
+            return false;
333
+        }
334
+    }
335
+
336
+    /**
337
+     * get the display name of the user currently logged in.
338
+     *
339
+     * @param string $uid
340
+     * @return string|bool uid or false
341
+     */
342
+    public static function getDisplayName($uid = null) {
343
+        if ($uid) {
344
+            $user = \OC::$server->getUserManager()->get($uid);
345
+            if ($user) {
346
+                return $user->getDisplayName();
347
+            } else {
348
+                return $uid;
349
+            }
350
+        } else {
351
+            $user = self::getUserSession()->getUser();
352
+            if ($user) {
353
+                return $user->getDisplayName();
354
+            } else {
355
+                return false;
356
+            }
357
+        }
358
+    }
359
+
360
+    /**
361
+     * Set password
362
+     *
363
+     * @param string $uid The username
364
+     * @param string $password The new password
365
+     * @param string $recoveryPassword for the encryption app to reset encryption keys
366
+     * @return bool
367
+     *
368
+     * Change the password of a user
369
+     */
370
+    public static function setPassword($uid, $password, $recoveryPassword = null) {
371
+        $user = \OC::$server->getUserManager()->get($uid);
372
+        if ($user) {
373
+            return $user->setPassword($password, $recoveryPassword);
374
+        } else {
375
+            return false;
376
+        }
377
+    }
378
+
379
+    /**
380
+     * Check if the password is correct
381
+     *
382
+     * @param string $uid The username
383
+     * @param string $password The password
384
+     * @return string|false user id a string on success, false otherwise
385
+     *
386
+     * Check if the password is correct without logging in the user
387
+     * returns the user id or false
388
+     */
389
+    public static function checkPassword($uid, $password) {
390
+        $manager = \OC::$server->getUserManager();
391
+        $username = $manager->checkPassword($uid, $password);
392
+        if ($username !== false) {
393
+            return $username->getUID();
394
+        }
395
+        return false;
396
+    }
397
+
398
+    /**
399
+     * @param string $uid The username
400
+     * @return string
401
+     *
402
+     * returns the path to the users home directory
403
+     * @deprecated Use \OC::$server->getUserManager->getHome()
404
+     */
405
+    public static function getHome($uid) {
406
+        $user = \OC::$server->getUserManager()->get($uid);
407
+        if ($user) {
408
+            return $user->getHome();
409
+        } else {
410
+            return \OC::$server->getSystemConfig()->getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid;
411
+        }
412
+    }
413
+
414
+    /**
415
+     * Get a list of all users
416
+     *
417
+     * @return array an array of all uids
418
+     *
419
+     * Get a list of all users.
420
+     * @param string $search
421
+     * @param integer $limit
422
+     * @param integer $offset
423
+     */
424
+    public static function getUsers($search = '', $limit = null, $offset = null) {
425
+        $users = \OC::$server->getUserManager()->search($search, $limit, $offset);
426
+        $uids = array();
427
+        foreach ($users as $user) {
428
+            $uids[] = $user->getUID();
429
+        }
430
+        return $uids;
431
+    }
432
+
433
+    /**
434
+     * Get a list of all users display name
435
+     *
436
+     * @param string $search
437
+     * @param int $limit
438
+     * @param int $offset
439
+     * @return array associative array with all display names (value) and corresponding uids (key)
440
+     *
441
+     * Get a list of all display names and user ids.
442
+     * @deprecated Use \OC::$server->getUserManager->searchDisplayName($search, $limit, $offset) instead.
443
+     */
444
+    public static function getDisplayNames($search = '', $limit = null, $offset = null) {
445
+        $displayNames = array();
446
+        $users = \OC::$server->getUserManager()->searchDisplayName($search, $limit, $offset);
447
+        foreach ($users as $user) {
448
+            $displayNames[$user->getUID()] = $user->getDisplayName();
449
+        }
450
+        return $displayNames;
451
+    }
452
+
453
+    /**
454
+     * check if a user exists
455
+     *
456
+     * @param string $uid the username
457
+     * @return boolean
458
+     */
459
+    public static function userExists($uid) {
460
+        return \OC::$server->getUserManager()->userExists($uid);
461
+    }
462
+
463
+    /**
464
+     * checks if a user is enabled
465
+     *
466
+     * @param string $uid
467
+     * @return bool
468
+     */
469
+    public static function isEnabled($uid) {
470
+        $user = \OC::$server->getUserManager()->get($uid);
471
+        if ($user) {
472
+            return $user->isEnabled();
473
+        } else {
474
+            return false;
475
+        }
476
+    }
477
+
478
+    /**
479
+     * Returns the first active backend from self::$_usedBackends.
480
+     *
481
+     * @return OCP\Authentication\IApacheBackend|null if no backend active, otherwise OCP\Authentication\IApacheBackend
482
+     */
483
+    private static function findFirstActiveUsedBackend() {
484
+        foreach (self::$_usedBackends as $backend) {
485
+            if ($backend instanceof OCP\Authentication\IApacheBackend) {
486
+                if ($backend->isSessionActive()) {
487
+                    return $backend;
488
+                }
489
+            }
490
+        }
491
+
492
+        return null;
493
+    }
494 494
 }
Please login to merge, or discard this patch.