Passed
Push — master ( 9e7341...e1b91f )
by Maxence
15:46 queued 13s
created
lib/public/User/Events/BeforeUserLoggedInEvent.php 1 patch
Indentation   +37 added lines, -37 removed lines patch added patch discarded remove patch
@@ -34,45 +34,45 @@
 block discarded – undo
34 34
  * @since 18.0.0
35 35
  */
36 36
 class BeforeUserLoggedInEvent extends Event {
37
-	private string $username;
38
-	private ?string $password;
39
-	private ?IApacheBackend $backend;
37
+    private string $username;
38
+    private ?string $password;
39
+    private ?IApacheBackend $backend;
40 40
 
41
-	/**
42
-	 * @since 18.0.0
43
-	 * @since 26.0.0 password can be null
44
-	 */
45
-	public function __construct(string $username, ?string $password, ?IApacheBackend $backend = null) {
46
-		parent::__construct();
47
-		$this->username = $username;
48
-		$this->password = $password;
49
-		$this->backend = $backend;
50
-	}
41
+    /**
42
+     * @since 18.0.0
43
+     * @since 26.0.0 password can be null
44
+     */
45
+    public function __construct(string $username, ?string $password, ?IApacheBackend $backend = null) {
46
+        parent::__construct();
47
+        $this->username = $username;
48
+        $this->password = $password;
49
+        $this->backend = $backend;
50
+    }
51 51
 
52
-	/**
53
-	 * returns the login name, which must not necessarily match to a user ID
54
-	 *
55
-	 * @since 18.0.0
56
-	 */
57
-	public function getUsername(): string {
58
-		return $this->username;
59
-	}
52
+    /**
53
+     * returns the login name, which must not necessarily match to a user ID
54
+     *
55
+     * @since 18.0.0
56
+     */
57
+    public function getUsername(): string {
58
+        return $this->username;
59
+    }
60 60
 
61
-	/**
62
-	 * @since 18.0.0
63
-	 * @since 26.0.0 value can be null
64
-	 */
65
-	public function getPassword(): ?string {
66
-		return $this->password;
67
-	}
61
+    /**
62
+     * @since 18.0.0
63
+     * @since 26.0.0 value can be null
64
+     */
65
+    public function getPassword(): ?string {
66
+        return $this->password;
67
+    }
68 68
 
69
-	/**
70
-	 * return backend if available (or null)
71
-	 *
72
-	 * @return IApacheBackend|null
73
-	 * @since 26.0.0
74
-	 */
75
-	public function getBackend(): ?IApacheBackend {
76
-		return $this->backend;
77
-	}
69
+    /**
70
+     * return backend if available (or null)
71
+     *
72
+     * @return IApacheBackend|null
73
+     * @since 26.0.0
74
+     */
75
+    public function getBackend(): ?IApacheBackend {
76
+        return $this->backend;
77
+    }
78 78
 }
Please login to merge, or discard this patch.
lib/private/legacy/OC_User.php 1 patch
Indentation   +355 added lines, -355 removed lines patch added patch discarded remove patch
@@ -62,359 +62,359 @@
 block discarded – undo
62 62
  *   logout()
63 63
  */
64 64
 class OC_User {
65
-	private static $_usedBackends = [];
66
-
67
-	private static $_setupedBackends = [];
68
-
69
-	// bool, stores if a user want to access a resource anonymously, e.g if they open a public link
70
-	private static $incognitoMode = false;
71
-
72
-	/**
73
-	 * Adds the backend to the list of used backends
74
-	 *
75
-	 * @param string|\OCP\UserInterface $backend default: database The backend to use for user management
76
-	 * @return bool
77
-	 *
78
-	 * Set the User Authentication Module
79
-	 * @suppress PhanDeprecatedFunction
80
-	 */
81
-	public static function useBackend($backend = 'database') {
82
-		if ($backend instanceof \OCP\UserInterface) {
83
-			self::$_usedBackends[get_class($backend)] = $backend;
84
-			\OC::$server->getUserManager()->registerBackend($backend);
85
-		} else {
86
-			// You'll never know what happens
87
-			if (null === $backend or !is_string($backend)) {
88
-				$backend = 'database';
89
-			}
90
-
91
-			// Load backend
92
-			switch ($backend) {
93
-				case 'database':
94
-				case 'mysql':
95
-				case 'sqlite':
96
-					\OCP\Util::writeLog('core', 'Adding user backend ' . $backend . '.', ILogger::DEBUG);
97
-					self::$_usedBackends[$backend] = new \OC\User\Database();
98
-					\OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
99
-					break;
100
-				case 'dummy':
101
-					self::$_usedBackends[$backend] = new \Test\Util\User\Dummy();
102
-					\OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
103
-					break;
104
-				default:
105
-					\OCP\Util::writeLog('core', 'Adding default user backend ' . $backend . '.', ILogger::DEBUG);
106
-					$className = 'OC_USER_' . strtoupper($backend);
107
-					self::$_usedBackends[$backend] = new $className();
108
-					\OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
109
-					break;
110
-			}
111
-		}
112
-		return true;
113
-	}
114
-
115
-	/**
116
-	 * remove all used backends
117
-	 */
118
-	public static function clearBackends() {
119
-		self::$_usedBackends = [];
120
-		\OC::$server->getUserManager()->clearBackends();
121
-	}
122
-
123
-	/**
124
-	 * setup the configured backends in config.php
125
-	 * @suppress PhanDeprecatedFunction
126
-	 */
127
-	public static function setupBackends() {
128
-		OC_App::loadApps(['prelogin']);
129
-		$backends = \OC::$server->getSystemConfig()->getValue('user_backends', []);
130
-		if (isset($backends['default']) && !$backends['default']) {
131
-			// clear default backends
132
-			self::clearBackends();
133
-		}
134
-		foreach ($backends as $i => $config) {
135
-			if (!is_array($config)) {
136
-				continue;
137
-			}
138
-			$class = $config['class'];
139
-			$arguments = $config['arguments'];
140
-			if (class_exists($class)) {
141
-				if (array_search($i, self::$_setupedBackends) === false) {
142
-					// make a reflection object
143
-					$reflectionObj = new ReflectionClass($class);
144
-
145
-					// use Reflection to create a new instance, using the $args
146
-					$backend = $reflectionObj->newInstanceArgs($arguments);
147
-					self::useBackend($backend);
148
-					self::$_setupedBackends[] = $i;
149
-				} else {
150
-					\OCP\Util::writeLog('core', 'User backend ' . $class . ' already initialized.', ILogger::DEBUG);
151
-				}
152
-			} else {
153
-				\OCP\Util::writeLog('core', 'User backend ' . $class . ' not found.', ILogger::ERROR);
154
-			}
155
-		}
156
-	}
157
-
158
-	/**
159
-	 * Try to login a user, assuming authentication
160
-	 * has already happened (e.g. via Single Sign On).
161
-	 *
162
-	 * Log in a user and regenerate a new session.
163
-	 *
164
-	 * @param \OCP\Authentication\IApacheBackend $backend
165
-	 * @return bool
166
-	 */
167
-	public static function loginWithApache(\OCP\Authentication\IApacheBackend $backend) {
168
-		$uid = $backend->getCurrentUserId();
169
-		$run = true;
170
-		OC_Hook::emit("OC_User", "pre_login", ["run" => &$run, "uid" => $uid, 'backend' => $backend]);
171
-
172
-		if ($uid) {
173
-			if (self::getUser() !== $uid) {
174
-				self::setUserId($uid);
175
-				$userSession = \OC::$server->getUserSession();
176
-
177
-				/** @var IEventDispatcher $dispatcher */
178
-				$dispatcher = \OC::$server->get(IEventDispatcher::class);
179
-
180
-				if ($userSession->getUser() && !$userSession->getUser()->isEnabled()) {
181
-					$message = \OC::$server->getL10N('lib')->t('User disabled');
182
-					throw new LoginException($message);
183
-				}
184
-				$userSession->setLoginName($uid);
185
-				$request = OC::$server->getRequest();
186
-				$password = null;
187
-				if ($backend instanceof \OCP\Authentication\IProvideUserSecretBackend) {
188
-					$password = $backend->getCurrentUserSecret();
189
-				}
190
-
191
-				/** @var IEventDispatcher $dispatcher */
192
-				$dispatcher->dispatchTyped(new BeforeUserLoggedInEvent($uid, $password, $backend));
193
-
194
-				$userSession->createSessionToken($request, $uid, $uid, $password);
195
-				$userSession->createRememberMeToken($userSession->getUser());
196
-				// setup the filesystem
197
-				OC_Util::setupFS($uid);
198
-				// first call the post_login hooks, the login-process needs to be
199
-				// completed before we can safely create the users folder.
200
-				// For example encryption needs to initialize the users keys first
201
-				// before we can create the user folder with the skeleton files
202
-				OC_Hook::emit(
203
-					'OC_User',
204
-					'post_login',
205
-					[
206
-						'uid' => $uid,
207
-						'password' => $password,
208
-						'isTokenLogin' => false,
209
-					]
210
-				);
211
-				$dispatcher->dispatchTyped(new UserLoggedInEvent(
212
-					\OC::$server->get(IUserManager::class)->get($uid),
213
-					$uid,
214
-					null,
215
-					false)
216
-				);
217
-
218
-				//trigger creation of user home and /files folder
219
-				\OC::$server->getUserFolder($uid);
220
-			}
221
-			return true;
222
-		}
223
-		return false;
224
-	}
225
-
226
-	/**
227
-	 * Verify with Apache whether user is authenticated.
228
-	 *
229
-	 * @return boolean|null
230
-	 *          true: authenticated
231
-	 *          false: not authenticated
232
-	 *          null: not handled / no backend available
233
-	 */
234
-	public static function handleApacheAuth() {
235
-		$backend = self::findFirstActiveUsedBackend();
236
-		if ($backend) {
237
-			OC_App::loadApps();
238
-
239
-			//setup extra user backends
240
-			self::setupBackends();
241
-			\OC::$server->getUserSession()->unsetMagicInCookie();
242
-
243
-			return self::loginWithApache($backend);
244
-		}
245
-
246
-		return null;
247
-	}
248
-
249
-
250
-	/**
251
-	 * Sets user id for session and triggers emit
252
-	 *
253
-	 * @param string $uid
254
-	 */
255
-	public static function setUserId($uid) {
256
-		$userSession = \OC::$server->getUserSession();
257
-		$userManager = \OC::$server->getUserManager();
258
-		if ($user = $userManager->get($uid)) {
259
-			$userSession->setUser($user);
260
-		} else {
261
-			\OC::$server->getSession()->set('user_id', $uid);
262
-		}
263
-	}
264
-
265
-	/**
266
-	 * Check if the user is logged in, considers also the HTTP basic credentials
267
-	 *
268
-	 * @deprecated use \OC::$server->getUserSession()->isLoggedIn()
269
-	 * @return bool
270
-	 */
271
-	public static function isLoggedIn() {
272
-		return \OC::$server->getUserSession()->isLoggedIn();
273
-	}
274
-
275
-	/**
276
-	 * set incognito mode, e.g. if a user wants to open a public link
277
-	 *
278
-	 * @param bool $status
279
-	 */
280
-	public static function setIncognitoMode($status) {
281
-		self::$incognitoMode = $status;
282
-	}
283
-
284
-	/**
285
-	 * get incognito mode status
286
-	 *
287
-	 * @return bool
288
-	 */
289
-	public static function isIncognitoMode() {
290
-		return self::$incognitoMode;
291
-	}
292
-
293
-	/**
294
-	 * Returns the current logout URL valid for the currently logged-in user
295
-	 *
296
-	 * @param \OCP\IURLGenerator $urlGenerator
297
-	 * @return string
298
-	 */
299
-	public static function getLogoutUrl(\OCP\IURLGenerator $urlGenerator) {
300
-		$backend = self::findFirstActiveUsedBackend();
301
-		if ($backend) {
302
-			return $backend->getLogoutUrl();
303
-		}
304
-
305
-		$user = \OC::$server->getUserSession()->getUser();
306
-		if ($user instanceof \OCP\IUser) {
307
-			$backend = $user->getBackend();
308
-			if ($backend instanceof \OCP\User\Backend\ICustomLogout) {
309
-				return $backend->getLogoutUrl();
310
-			}
311
-		}
312
-
313
-		$logoutUrl = $urlGenerator->linkToRoute('core.login.logout');
314
-		$logoutUrl .= '?requesttoken=' . urlencode(\OCP\Util::callRegister());
315
-
316
-		return $logoutUrl;
317
-	}
318
-
319
-	/**
320
-	 * Check if the user is an admin user
321
-	 *
322
-	 * @param string $uid uid of the admin
323
-	 * @return bool
324
-	 */
325
-	public static function isAdminUser($uid) {
326
-		$group = \OC::$server->getGroupManager()->get('admin');
327
-		$user = \OC::$server->getUserManager()->get($uid);
328
-		if ($group && $user && $group->inGroup($user) && self::$incognitoMode === false) {
329
-			return true;
330
-		}
331
-		return false;
332
-	}
333
-
334
-
335
-	/**
336
-	 * get the user id of the user currently logged in.
337
-	 *
338
-	 * @return string|false uid or false
339
-	 */
340
-	public static function getUser() {
341
-		$uid = \OC::$server->getSession() ? \OC::$server->getSession()->get('user_id') : null;
342
-		if (!is_null($uid) && self::$incognitoMode === false) {
343
-			return $uid;
344
-		} else {
345
-			return false;
346
-		}
347
-	}
348
-
349
-	/**
350
-	 * Set password
351
-	 *
352
-	 * @param string $uid The username
353
-	 * @param string $password The new password
354
-	 * @param string $recoveryPassword for the encryption app to reset encryption keys
355
-	 * @return bool
356
-	 *
357
-	 * Change the password of a user
358
-	 */
359
-	public static function setPassword($uid, $password, $recoveryPassword = null) {
360
-		$user = \OC::$server->getUserManager()->get($uid);
361
-		if ($user) {
362
-			return $user->setPassword($password, $recoveryPassword);
363
-		} else {
364
-			return false;
365
-		}
366
-	}
367
-
368
-	/**
369
-	 * @param string $uid The username
370
-	 * @return string
371
-	 *
372
-	 * returns the path to the users home directory
373
-	 * @deprecated Use \OC::$server->getUserManager->getHome()
374
-	 */
375
-	public static function getHome($uid) {
376
-		$user = \OC::$server->getUserManager()->get($uid);
377
-		if ($user) {
378
-			return $user->getHome();
379
-		} else {
380
-			return \OC::$server->getSystemConfig()->getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid;
381
-		}
382
-	}
383
-
384
-	/**
385
-	 * Get a list of all users display name
386
-	 *
387
-	 * @param string $search
388
-	 * @param int $limit
389
-	 * @param int $offset
390
-	 * @return array associative array with all display names (value) and corresponding uids (key)
391
-	 *
392
-	 * Get a list of all display names and user ids.
393
-	 * @deprecated Use \OC::$server->getUserManager->searchDisplayName($search, $limit, $offset) instead.
394
-	 */
395
-	public static function getDisplayNames($search = '', $limit = null, $offset = null) {
396
-		$displayNames = [];
397
-		$users = \OC::$server->getUserManager()->searchDisplayName($search, $limit, $offset);
398
-		foreach ($users as $user) {
399
-			$displayNames[$user->getUID()] = $user->getDisplayName();
400
-		}
401
-		return $displayNames;
402
-	}
403
-
404
-	/**
405
-	 * Returns the first active backend from self::$_usedBackends.
406
-	 *
407
-	 * @return OCP\Authentication\IApacheBackend|null if no backend active, otherwise OCP\Authentication\IApacheBackend
408
-	 */
409
-	private static function findFirstActiveUsedBackend() {
410
-		foreach (self::$_usedBackends as $backend) {
411
-			if ($backend instanceof OCP\Authentication\IApacheBackend) {
412
-				if ($backend->isSessionActive()) {
413
-					return $backend;
414
-				}
415
-			}
416
-		}
417
-
418
-		return null;
419
-	}
65
+    private static $_usedBackends = [];
66
+
67
+    private static $_setupedBackends = [];
68
+
69
+    // bool, stores if a user want to access a resource anonymously, e.g if they open a public link
70
+    private static $incognitoMode = false;
71
+
72
+    /**
73
+     * Adds the backend to the list of used backends
74
+     *
75
+     * @param string|\OCP\UserInterface $backend default: database The backend to use for user management
76
+     * @return bool
77
+     *
78
+     * Set the User Authentication Module
79
+     * @suppress PhanDeprecatedFunction
80
+     */
81
+    public static function useBackend($backend = 'database') {
82
+        if ($backend instanceof \OCP\UserInterface) {
83
+            self::$_usedBackends[get_class($backend)] = $backend;
84
+            \OC::$server->getUserManager()->registerBackend($backend);
85
+        } else {
86
+            // You'll never know what happens
87
+            if (null === $backend or !is_string($backend)) {
88
+                $backend = 'database';
89
+            }
90
+
91
+            // Load backend
92
+            switch ($backend) {
93
+                case 'database':
94
+                case 'mysql':
95
+                case 'sqlite':
96
+                    \OCP\Util::writeLog('core', 'Adding user backend ' . $backend . '.', ILogger::DEBUG);
97
+                    self::$_usedBackends[$backend] = new \OC\User\Database();
98
+                    \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
99
+                    break;
100
+                case 'dummy':
101
+                    self::$_usedBackends[$backend] = new \Test\Util\User\Dummy();
102
+                    \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
103
+                    break;
104
+                default:
105
+                    \OCP\Util::writeLog('core', 'Adding default user backend ' . $backend . '.', ILogger::DEBUG);
106
+                    $className = 'OC_USER_' . strtoupper($backend);
107
+                    self::$_usedBackends[$backend] = new $className();
108
+                    \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
109
+                    break;
110
+            }
111
+        }
112
+        return true;
113
+    }
114
+
115
+    /**
116
+     * remove all used backends
117
+     */
118
+    public static function clearBackends() {
119
+        self::$_usedBackends = [];
120
+        \OC::$server->getUserManager()->clearBackends();
121
+    }
122
+
123
+    /**
124
+     * setup the configured backends in config.php
125
+     * @suppress PhanDeprecatedFunction
126
+     */
127
+    public static function setupBackends() {
128
+        OC_App::loadApps(['prelogin']);
129
+        $backends = \OC::$server->getSystemConfig()->getValue('user_backends', []);
130
+        if (isset($backends['default']) && !$backends['default']) {
131
+            // clear default backends
132
+            self::clearBackends();
133
+        }
134
+        foreach ($backends as $i => $config) {
135
+            if (!is_array($config)) {
136
+                continue;
137
+            }
138
+            $class = $config['class'];
139
+            $arguments = $config['arguments'];
140
+            if (class_exists($class)) {
141
+                if (array_search($i, self::$_setupedBackends) === false) {
142
+                    // make a reflection object
143
+                    $reflectionObj = new ReflectionClass($class);
144
+
145
+                    // use Reflection to create a new instance, using the $args
146
+                    $backend = $reflectionObj->newInstanceArgs($arguments);
147
+                    self::useBackend($backend);
148
+                    self::$_setupedBackends[] = $i;
149
+                } else {
150
+                    \OCP\Util::writeLog('core', 'User backend ' . $class . ' already initialized.', ILogger::DEBUG);
151
+                }
152
+            } else {
153
+                \OCP\Util::writeLog('core', 'User backend ' . $class . ' not found.', ILogger::ERROR);
154
+            }
155
+        }
156
+    }
157
+
158
+    /**
159
+     * Try to login a user, assuming authentication
160
+     * has already happened (e.g. via Single Sign On).
161
+     *
162
+     * Log in a user and regenerate a new session.
163
+     *
164
+     * @param \OCP\Authentication\IApacheBackend $backend
165
+     * @return bool
166
+     */
167
+    public static function loginWithApache(\OCP\Authentication\IApacheBackend $backend) {
168
+        $uid = $backend->getCurrentUserId();
169
+        $run = true;
170
+        OC_Hook::emit("OC_User", "pre_login", ["run" => &$run, "uid" => $uid, 'backend' => $backend]);
171
+
172
+        if ($uid) {
173
+            if (self::getUser() !== $uid) {
174
+                self::setUserId($uid);
175
+                $userSession = \OC::$server->getUserSession();
176
+
177
+                /** @var IEventDispatcher $dispatcher */
178
+                $dispatcher = \OC::$server->get(IEventDispatcher::class);
179
+
180
+                if ($userSession->getUser() && !$userSession->getUser()->isEnabled()) {
181
+                    $message = \OC::$server->getL10N('lib')->t('User disabled');
182
+                    throw new LoginException($message);
183
+                }
184
+                $userSession->setLoginName($uid);
185
+                $request = OC::$server->getRequest();
186
+                $password = null;
187
+                if ($backend instanceof \OCP\Authentication\IProvideUserSecretBackend) {
188
+                    $password = $backend->getCurrentUserSecret();
189
+                }
190
+
191
+                /** @var IEventDispatcher $dispatcher */
192
+                $dispatcher->dispatchTyped(new BeforeUserLoggedInEvent($uid, $password, $backend));
193
+
194
+                $userSession->createSessionToken($request, $uid, $uid, $password);
195
+                $userSession->createRememberMeToken($userSession->getUser());
196
+                // setup the filesystem
197
+                OC_Util::setupFS($uid);
198
+                // first call the post_login hooks, the login-process needs to be
199
+                // completed before we can safely create the users folder.
200
+                // For example encryption needs to initialize the users keys first
201
+                // before we can create the user folder with the skeleton files
202
+                OC_Hook::emit(
203
+                    'OC_User',
204
+                    'post_login',
205
+                    [
206
+                        'uid' => $uid,
207
+                        'password' => $password,
208
+                        'isTokenLogin' => false,
209
+                    ]
210
+                );
211
+                $dispatcher->dispatchTyped(new UserLoggedInEvent(
212
+                    \OC::$server->get(IUserManager::class)->get($uid),
213
+                    $uid,
214
+                    null,
215
+                    false)
216
+                );
217
+
218
+                //trigger creation of user home and /files folder
219
+                \OC::$server->getUserFolder($uid);
220
+            }
221
+            return true;
222
+        }
223
+        return false;
224
+    }
225
+
226
+    /**
227
+     * Verify with Apache whether user is authenticated.
228
+     *
229
+     * @return boolean|null
230
+     *          true: authenticated
231
+     *          false: not authenticated
232
+     *          null: not handled / no backend available
233
+     */
234
+    public static function handleApacheAuth() {
235
+        $backend = self::findFirstActiveUsedBackend();
236
+        if ($backend) {
237
+            OC_App::loadApps();
238
+
239
+            //setup extra user backends
240
+            self::setupBackends();
241
+            \OC::$server->getUserSession()->unsetMagicInCookie();
242
+
243
+            return self::loginWithApache($backend);
244
+        }
245
+
246
+        return null;
247
+    }
248
+
249
+
250
+    /**
251
+     * Sets user id for session and triggers emit
252
+     *
253
+     * @param string $uid
254
+     */
255
+    public static function setUserId($uid) {
256
+        $userSession = \OC::$server->getUserSession();
257
+        $userManager = \OC::$server->getUserManager();
258
+        if ($user = $userManager->get($uid)) {
259
+            $userSession->setUser($user);
260
+        } else {
261
+            \OC::$server->getSession()->set('user_id', $uid);
262
+        }
263
+    }
264
+
265
+    /**
266
+     * Check if the user is logged in, considers also the HTTP basic credentials
267
+     *
268
+     * @deprecated use \OC::$server->getUserSession()->isLoggedIn()
269
+     * @return bool
270
+     */
271
+    public static function isLoggedIn() {
272
+        return \OC::$server->getUserSession()->isLoggedIn();
273
+    }
274
+
275
+    /**
276
+     * set incognito mode, e.g. if a user wants to open a public link
277
+     *
278
+     * @param bool $status
279
+     */
280
+    public static function setIncognitoMode($status) {
281
+        self::$incognitoMode = $status;
282
+    }
283
+
284
+    /**
285
+     * get incognito mode status
286
+     *
287
+     * @return bool
288
+     */
289
+    public static function isIncognitoMode() {
290
+        return self::$incognitoMode;
291
+    }
292
+
293
+    /**
294
+     * Returns the current logout URL valid for the currently logged-in user
295
+     *
296
+     * @param \OCP\IURLGenerator $urlGenerator
297
+     * @return string
298
+     */
299
+    public static function getLogoutUrl(\OCP\IURLGenerator $urlGenerator) {
300
+        $backend = self::findFirstActiveUsedBackend();
301
+        if ($backend) {
302
+            return $backend->getLogoutUrl();
303
+        }
304
+
305
+        $user = \OC::$server->getUserSession()->getUser();
306
+        if ($user instanceof \OCP\IUser) {
307
+            $backend = $user->getBackend();
308
+            if ($backend instanceof \OCP\User\Backend\ICustomLogout) {
309
+                return $backend->getLogoutUrl();
310
+            }
311
+        }
312
+
313
+        $logoutUrl = $urlGenerator->linkToRoute('core.login.logout');
314
+        $logoutUrl .= '?requesttoken=' . urlencode(\OCP\Util::callRegister());
315
+
316
+        return $logoutUrl;
317
+    }
318
+
319
+    /**
320
+     * Check if the user is an admin user
321
+     *
322
+     * @param string $uid uid of the admin
323
+     * @return bool
324
+     */
325
+    public static function isAdminUser($uid) {
326
+        $group = \OC::$server->getGroupManager()->get('admin');
327
+        $user = \OC::$server->getUserManager()->get($uid);
328
+        if ($group && $user && $group->inGroup($user) && self::$incognitoMode === false) {
329
+            return true;
330
+        }
331
+        return false;
332
+    }
333
+
334
+
335
+    /**
336
+     * get the user id of the user currently logged in.
337
+     *
338
+     * @return string|false uid or false
339
+     */
340
+    public static function getUser() {
341
+        $uid = \OC::$server->getSession() ? \OC::$server->getSession()->get('user_id') : null;
342
+        if (!is_null($uid) && self::$incognitoMode === false) {
343
+            return $uid;
344
+        } else {
345
+            return false;
346
+        }
347
+    }
348
+
349
+    /**
350
+     * Set password
351
+     *
352
+     * @param string $uid The username
353
+     * @param string $password The new password
354
+     * @param string $recoveryPassword for the encryption app to reset encryption keys
355
+     * @return bool
356
+     *
357
+     * Change the password of a user
358
+     */
359
+    public static function setPassword($uid, $password, $recoveryPassword = null) {
360
+        $user = \OC::$server->getUserManager()->get($uid);
361
+        if ($user) {
362
+            return $user->setPassword($password, $recoveryPassword);
363
+        } else {
364
+            return false;
365
+        }
366
+    }
367
+
368
+    /**
369
+     * @param string $uid The username
370
+     * @return string
371
+     *
372
+     * returns the path to the users home directory
373
+     * @deprecated Use \OC::$server->getUserManager->getHome()
374
+     */
375
+    public static function getHome($uid) {
376
+        $user = \OC::$server->getUserManager()->get($uid);
377
+        if ($user) {
378
+            return $user->getHome();
379
+        } else {
380
+            return \OC::$server->getSystemConfig()->getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid;
381
+        }
382
+    }
383
+
384
+    /**
385
+     * Get a list of all users display name
386
+     *
387
+     * @param string $search
388
+     * @param int $limit
389
+     * @param int $offset
390
+     * @return array associative array with all display names (value) and corresponding uids (key)
391
+     *
392
+     * Get a list of all display names and user ids.
393
+     * @deprecated Use \OC::$server->getUserManager->searchDisplayName($search, $limit, $offset) instead.
394
+     */
395
+    public static function getDisplayNames($search = '', $limit = null, $offset = null) {
396
+        $displayNames = [];
397
+        $users = \OC::$server->getUserManager()->searchDisplayName($search, $limit, $offset);
398
+        foreach ($users as $user) {
399
+            $displayNames[$user->getUID()] = $user->getDisplayName();
400
+        }
401
+        return $displayNames;
402
+    }
403
+
404
+    /**
405
+     * Returns the first active backend from self::$_usedBackends.
406
+     *
407
+     * @return OCP\Authentication\IApacheBackend|null if no backend active, otherwise OCP\Authentication\IApacheBackend
408
+     */
409
+    private static function findFirstActiveUsedBackend() {
410
+        foreach (self::$_usedBackends as $backend) {
411
+            if ($backend instanceof OCP\Authentication\IApacheBackend) {
412
+                if ($backend->isSessionActive()) {
413
+                    return $backend;
414
+                }
415
+            }
416
+        }
417
+
418
+        return null;
419
+    }
420 420
 }
Please login to merge, or discard this patch.