Completed
Pull Request — master (#6177)
by Lukas
15:30
created
lib/private/legacy/user.php 1 patch
Indentation   +433 added lines, -433 removed lines patch added patch discarded remove patch
@@ -57,437 +57,437 @@
 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
-	 * Returns the current logout URL valid for the currently logged-in user
285
-	 *
286
-	 * @param \OCP\IURLGenerator $urlGenerator
287
-	 * @return string
288
-	 */
289
-	public static function getLogoutUrl(\OCP\IURLGenerator $urlGenerator) {
290
-		$backend = self::findFirstActiveUsedBackend();
291
-		if ($backend) {
292
-			return $backend->getLogoutUrl();
293
-		}
294
-
295
-		$logoutUrl = $urlGenerator->linkToRouteAbsolute(
296
-			'core.login.logout',
297
-			[
298
-				'requesttoken' => \OCP\Util::callRegister(),
299
-			]
300
-		);
301
-
302
-		return $logoutUrl;
303
-	}
304
-
305
-	/**
306
-	 * Check if the user is an admin user
307
-	 *
308
-	 * @param string $uid uid of the admin
309
-	 * @return bool
310
-	 */
311
-	public static function isAdminUser($uid) {
312
-		$group = \OC::$server->getGroupManager()->get('admin');
313
-		$user = \OC::$server->getUserManager()->get($uid);
314
-		if ($group && $user && $group->inGroup($user) && self::$incognitoMode === false) {
315
-			return true;
316
-		}
317
-		return false;
318
-	}
319
-
320
-
321
-	/**
322
-	 * get the user id of the user currently logged in.
323
-	 *
324
-	 * @return string|bool uid or false
325
-	 */
326
-	public static function getUser() {
327
-		$uid = \OC::$server->getSession() ? \OC::$server->getSession()->get('user_id') : null;
328
-		if (!is_null($uid) && self::$incognitoMode === false) {
329
-			return $uid;
330
-		} else {
331
-			return false;
332
-		}
333
-	}
334
-
335
-	/**
336
-	 * get the display name of the user currently logged in.
337
-	 *
338
-	 * @param string $uid
339
-	 * @return string|bool uid or false
340
-	 */
341
-	public static function getDisplayName($uid = null) {
342
-		if ($uid) {
343
-			$user = \OC::$server->getUserManager()->get($uid);
344
-			if ($user) {
345
-				return $user->getDisplayName();
346
-			} else {
347
-				return $uid;
348
-			}
349
-		} else {
350
-			$user = self::getUserSession()->getUser();
351
-			if ($user) {
352
-				return $user->getDisplayName();
353
-			} else {
354
-				return false;
355
-			}
356
-		}
357
-	}
358
-
359
-	/**
360
-	 * Set password
361
-	 *
362
-	 * @param string $uid The username
363
-	 * @param string $password The new password
364
-	 * @param string $recoveryPassword for the encryption app to reset encryption keys
365
-	 * @return bool
366
-	 *
367
-	 * Change the password of a user
368
-	 */
369
-	public static function setPassword($uid, $password, $recoveryPassword = null) {
370
-		$user = \OC::$server->getUserManager()->get($uid);
371
-		if ($user) {
372
-			return $user->setPassword($password, $recoveryPassword);
373
-		} else {
374
-			return false;
375
-		}
376
-	}
377
-
378
-	/**
379
-	 * Check if the password is correct
380
-	 *
381
-	 * @param string $uid The username
382
-	 * @param string $password The password
383
-	 * @return string|false user id a string on success, false otherwise
384
-	 *
385
-	 * Check if the password is correct without logging in the user
386
-	 * returns the user id or false
387
-	 */
388
-	public static function checkPassword($uid, $password) {
389
-		$manager = \OC::$server->getUserManager();
390
-		$username = $manager->checkPassword($uid, $password);
391
-		if ($username !== false) {
392
-			return $username->getUID();
393
-		}
394
-		return false;
395
-	}
396
-
397
-	/**
398
-	 * @param string $uid The username
399
-	 * @return string
400
-	 *
401
-	 * returns the path to the users home directory
402
-	 * @deprecated Use \OC::$server->getUserManager->getHome()
403
-	 */
404
-	public static function getHome($uid) {
405
-		$user = \OC::$server->getUserManager()->get($uid);
406
-		if ($user) {
407
-			return $user->getHome();
408
-		} else {
409
-			return \OC::$server->getSystemConfig()->getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid;
410
-		}
411
-	}
412
-
413
-	/**
414
-	 * Get a list of all users
415
-	 *
416
-	 * @return array an array of all uids
417
-	 *
418
-	 * Get a list of all users.
419
-	 * @param string $search
420
-	 * @param integer $limit
421
-	 * @param integer $offset
422
-	 */
423
-	public static function getUsers($search = '', $limit = null, $offset = null) {
424
-		$users = \OC::$server->getUserManager()->search($search, $limit, $offset);
425
-		$uids = array();
426
-		foreach ($users as $user) {
427
-			$uids[] = $user->getUID();
428
-		}
429
-		return $uids;
430
-	}
431
-
432
-	/**
433
-	 * Get a list of all users display name
434
-	 *
435
-	 * @param string $search
436
-	 * @param int $limit
437
-	 * @param int $offset
438
-	 * @return array associative array with all display names (value) and corresponding uids (key)
439
-	 *
440
-	 * Get a list of all display names and user ids.
441
-	 * @deprecated Use \OC::$server->getUserManager->searchDisplayName($search, $limit, $offset) instead.
442
-	 */
443
-	public static function getDisplayNames($search = '', $limit = null, $offset = null) {
444
-		$displayNames = array();
445
-		$users = \OC::$server->getUserManager()->searchDisplayName($search, $limit, $offset);
446
-		foreach ($users as $user) {
447
-			$displayNames[$user->getUID()] = $user->getDisplayName();
448
-		}
449
-		return $displayNames;
450
-	}
451
-
452
-	/**
453
-	 * check if a user exists
454
-	 *
455
-	 * @param string $uid the username
456
-	 * @return boolean
457
-	 */
458
-	public static function userExists($uid) {
459
-		return \OC::$server->getUserManager()->userExists($uid);
460
-	}
461
-
462
-	/**
463
-	 * checks if a user is enabled
464
-	 *
465
-	 * @param string $uid
466
-	 * @return bool
467
-	 */
468
-	public static function isEnabled($uid) {
469
-		$user = \OC::$server->getUserManager()->get($uid);
470
-		if ($user) {
471
-			return $user->isEnabled();
472
-		} else {
473
-			return false;
474
-		}
475
-	}
476
-
477
-	/**
478
-	 * Returns the first active backend from self::$_usedBackends.
479
-	 *
480
-	 * @return OCP\Authentication\IApacheBackend|null if no backend active, otherwise OCP\Authentication\IApacheBackend
481
-	 */
482
-	private static function findFirstActiveUsedBackend() {
483
-		foreach (self::$_usedBackends as $backend) {
484
-			if ($backend instanceof OCP\Authentication\IApacheBackend) {
485
-				if ($backend->isSessionActive()) {
486
-					return $backend;
487
-				}
488
-			}
489
-		}
490
-
491
-		return null;
492
-	}
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
+     * Returns the current logout URL valid for the currently logged-in user
285
+     *
286
+     * @param \OCP\IURLGenerator $urlGenerator
287
+     * @return string
288
+     */
289
+    public static function getLogoutUrl(\OCP\IURLGenerator $urlGenerator) {
290
+        $backend = self::findFirstActiveUsedBackend();
291
+        if ($backend) {
292
+            return $backend->getLogoutUrl();
293
+        }
294
+
295
+        $logoutUrl = $urlGenerator->linkToRouteAbsolute(
296
+            'core.login.logout',
297
+            [
298
+                'requesttoken' => \OCP\Util::callRegister(),
299
+            ]
300
+        );
301
+
302
+        return $logoutUrl;
303
+    }
304
+
305
+    /**
306
+     * Check if the user is an admin user
307
+     *
308
+     * @param string $uid uid of the admin
309
+     * @return bool
310
+     */
311
+    public static function isAdminUser($uid) {
312
+        $group = \OC::$server->getGroupManager()->get('admin');
313
+        $user = \OC::$server->getUserManager()->get($uid);
314
+        if ($group && $user && $group->inGroup($user) && self::$incognitoMode === false) {
315
+            return true;
316
+        }
317
+        return false;
318
+    }
319
+
320
+
321
+    /**
322
+     * get the user id of the user currently logged in.
323
+     *
324
+     * @return string|bool uid or false
325
+     */
326
+    public static function getUser() {
327
+        $uid = \OC::$server->getSession() ? \OC::$server->getSession()->get('user_id') : null;
328
+        if (!is_null($uid) && self::$incognitoMode === false) {
329
+            return $uid;
330
+        } else {
331
+            return false;
332
+        }
333
+    }
334
+
335
+    /**
336
+     * get the display name of the user currently logged in.
337
+     *
338
+     * @param string $uid
339
+     * @return string|bool uid or false
340
+     */
341
+    public static function getDisplayName($uid = null) {
342
+        if ($uid) {
343
+            $user = \OC::$server->getUserManager()->get($uid);
344
+            if ($user) {
345
+                return $user->getDisplayName();
346
+            } else {
347
+                return $uid;
348
+            }
349
+        } else {
350
+            $user = self::getUserSession()->getUser();
351
+            if ($user) {
352
+                return $user->getDisplayName();
353
+            } else {
354
+                return false;
355
+            }
356
+        }
357
+    }
358
+
359
+    /**
360
+     * Set password
361
+     *
362
+     * @param string $uid The username
363
+     * @param string $password The new password
364
+     * @param string $recoveryPassword for the encryption app to reset encryption keys
365
+     * @return bool
366
+     *
367
+     * Change the password of a user
368
+     */
369
+    public static function setPassword($uid, $password, $recoveryPassword = null) {
370
+        $user = \OC::$server->getUserManager()->get($uid);
371
+        if ($user) {
372
+            return $user->setPassword($password, $recoveryPassword);
373
+        } else {
374
+            return false;
375
+        }
376
+    }
377
+
378
+    /**
379
+     * Check if the password is correct
380
+     *
381
+     * @param string $uid The username
382
+     * @param string $password The password
383
+     * @return string|false user id a string on success, false otherwise
384
+     *
385
+     * Check if the password is correct without logging in the user
386
+     * returns the user id or false
387
+     */
388
+    public static function checkPassword($uid, $password) {
389
+        $manager = \OC::$server->getUserManager();
390
+        $username = $manager->checkPassword($uid, $password);
391
+        if ($username !== false) {
392
+            return $username->getUID();
393
+        }
394
+        return false;
395
+    }
396
+
397
+    /**
398
+     * @param string $uid The username
399
+     * @return string
400
+     *
401
+     * returns the path to the users home directory
402
+     * @deprecated Use \OC::$server->getUserManager->getHome()
403
+     */
404
+    public static function getHome($uid) {
405
+        $user = \OC::$server->getUserManager()->get($uid);
406
+        if ($user) {
407
+            return $user->getHome();
408
+        } else {
409
+            return \OC::$server->getSystemConfig()->getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid;
410
+        }
411
+    }
412
+
413
+    /**
414
+     * Get a list of all users
415
+     *
416
+     * @return array an array of all uids
417
+     *
418
+     * Get a list of all users.
419
+     * @param string $search
420
+     * @param integer $limit
421
+     * @param integer $offset
422
+     */
423
+    public static function getUsers($search = '', $limit = null, $offset = null) {
424
+        $users = \OC::$server->getUserManager()->search($search, $limit, $offset);
425
+        $uids = array();
426
+        foreach ($users as $user) {
427
+            $uids[] = $user->getUID();
428
+        }
429
+        return $uids;
430
+    }
431
+
432
+    /**
433
+     * Get a list of all users display name
434
+     *
435
+     * @param string $search
436
+     * @param int $limit
437
+     * @param int $offset
438
+     * @return array associative array with all display names (value) and corresponding uids (key)
439
+     *
440
+     * Get a list of all display names and user ids.
441
+     * @deprecated Use \OC::$server->getUserManager->searchDisplayName($search, $limit, $offset) instead.
442
+     */
443
+    public static function getDisplayNames($search = '', $limit = null, $offset = null) {
444
+        $displayNames = array();
445
+        $users = \OC::$server->getUserManager()->searchDisplayName($search, $limit, $offset);
446
+        foreach ($users as $user) {
447
+            $displayNames[$user->getUID()] = $user->getDisplayName();
448
+        }
449
+        return $displayNames;
450
+    }
451
+
452
+    /**
453
+     * check if a user exists
454
+     *
455
+     * @param string $uid the username
456
+     * @return boolean
457
+     */
458
+    public static function userExists($uid) {
459
+        return \OC::$server->getUserManager()->userExists($uid);
460
+    }
461
+
462
+    /**
463
+     * checks if a user is enabled
464
+     *
465
+     * @param string $uid
466
+     * @return bool
467
+     */
468
+    public static function isEnabled($uid) {
469
+        $user = \OC::$server->getUserManager()->get($uid);
470
+        if ($user) {
471
+            return $user->isEnabled();
472
+        } else {
473
+            return false;
474
+        }
475
+    }
476
+
477
+    /**
478
+     * Returns the first active backend from self::$_usedBackends.
479
+     *
480
+     * @return OCP\Authentication\IApacheBackend|null if no backend active, otherwise OCP\Authentication\IApacheBackend
481
+     */
482
+    private static function findFirstActiveUsedBackend() {
483
+        foreach (self::$_usedBackends as $backend) {
484
+            if ($backend instanceof OCP\Authentication\IApacheBackend) {
485
+                if ($backend->isSessionActive()) {
486
+                    return $backend;
487
+                }
488
+            }
489
+        }
490
+
491
+        return null;
492
+    }
493 493
 }
Please login to merge, or discard this patch.
lib/private/NavigationManager.php 2 patches
Indentation   +226 added lines, -226 removed lines patch added patch discarded remove patch
@@ -41,252 +41,252 @@
 block discarded – undo
41 41
  */
42 42
 
43 43
 class NavigationManager implements INavigationManager {
44
-	protected $entries = [];
45
-	protected $closureEntries = [];
46
-	protected $activeEntry;
47
-	/** @var bool */
48
-	protected $init = false;
49
-	/** @var IAppManager|AppManager */
50
-	protected $appManager;
51
-	/** @var IURLGenerator */
52
-	private $urlGenerator;
53
-	/** @var IFactory */
54
-	private $l10nFac;
55
-	/** @var IUserSession */
56
-	private $userSession;
57
-	/** @var IGroupManager|Manager */
58
-	private $groupManager;
59
-	/** @var IConfig */
60
-	private $config;
44
+    protected $entries = [];
45
+    protected $closureEntries = [];
46
+    protected $activeEntry;
47
+    /** @var bool */
48
+    protected $init = false;
49
+    /** @var IAppManager|AppManager */
50
+    protected $appManager;
51
+    /** @var IURLGenerator */
52
+    private $urlGenerator;
53
+    /** @var IFactory */
54
+    private $l10nFac;
55
+    /** @var IUserSession */
56
+    private $userSession;
57
+    /** @var IGroupManager|Manager */
58
+    private $groupManager;
59
+    /** @var IConfig */
60
+    private $config;
61 61
 
62
-	public function __construct(IAppManager $appManager,
63
-						 IURLGenerator $urlGenerator,
64
-						 IFactory $l10nFac,
65
-						 IUserSession $userSession,
66
-						 IGroupManager $groupManager,
67
-						 IConfig $config) {
68
-		$this->appManager = $appManager;
69
-		$this->urlGenerator = $urlGenerator;
70
-		$this->l10nFac = $l10nFac;
71
-		$this->userSession = $userSession;
72
-		$this->groupManager = $groupManager;
73
-		$this->config = $config;
74
-	}
62
+    public function __construct(IAppManager $appManager,
63
+                            IURLGenerator $urlGenerator,
64
+                            IFactory $l10nFac,
65
+                            IUserSession $userSession,
66
+                            IGroupManager $groupManager,
67
+                            IConfig $config) {
68
+        $this->appManager = $appManager;
69
+        $this->urlGenerator = $urlGenerator;
70
+        $this->l10nFac = $l10nFac;
71
+        $this->userSession = $userSession;
72
+        $this->groupManager = $groupManager;
73
+        $this->config = $config;
74
+    }
75 75
 
76
-	/**
77
-	 * Creates a new navigation entry
78
-	 *
79
-	 * @param array|\Closure $entry Array containing: id, name, order, icon and href key
80
-	 *					The use of a closure is preferred, because it will avoid
81
-	 * 					loading the routing of your app, unless required.
82
-	 * @return void
83
-	 */
84
-	public function add($entry) {
85
-		if ($entry instanceof \Closure) {
86
-			$this->closureEntries[] = $entry;
87
-			return;
88
-		}
76
+    /**
77
+     * Creates a new navigation entry
78
+     *
79
+     * @param array|\Closure $entry Array containing: id, name, order, icon and href key
80
+     *					The use of a closure is preferred, because it will avoid
81
+     * 					loading the routing of your app, unless required.
82
+     * @return void
83
+     */
84
+    public function add($entry) {
85
+        if ($entry instanceof \Closure) {
86
+            $this->closureEntries[] = $entry;
87
+            return;
88
+        }
89 89
 
90
-		$entry['active'] = false;
91
-		if(!isset($entry['icon'])) {
92
-			$entry['icon'] = '';
93
-		}
94
-		if(!isset($entry['type'])) {
95
-			$entry['type'] = 'link';
96
-		}
97
-		$this->entries[] = $entry;
98
-	}
90
+        $entry['active'] = false;
91
+        if(!isset($entry['icon'])) {
92
+            $entry['icon'] = '';
93
+        }
94
+        if(!isset($entry['type'])) {
95
+            $entry['type'] = 'link';
96
+        }
97
+        $this->entries[] = $entry;
98
+    }
99 99
 
100
-	/**
101
-	 * returns all the added Menu entries
102
-	 * @param string $type
103
-	 * @return array an array of the added entries
104
-	 */
105
-	public function getAll($type = 'link') {
106
-		$this->init();
107
-		foreach ($this->closureEntries as $c) {
108
-			$this->add($c());
109
-		}
110
-		$this->closureEntries = array();
100
+    /**
101
+     * returns all the added Menu entries
102
+     * @param string $type
103
+     * @return array an array of the added entries
104
+     */
105
+    public function getAll($type = 'link') {
106
+        $this->init();
107
+        foreach ($this->closureEntries as $c) {
108
+            $this->add($c());
109
+        }
110
+        $this->closureEntries = array();
111 111
 
112
-		if ($type === 'all') {
113
-			return $this->entries;
114
-		}
112
+        if ($type === 'all') {
113
+            return $this->entries;
114
+        }
115 115
 
116
-		return array_filter($this->entries, function($entry) use ($type) {
117
-			return $entry['type'] === $type;
118
-		});
119
-	}
116
+        return array_filter($this->entries, function($entry) use ($type) {
117
+            return $entry['type'] === $type;
118
+        });
119
+    }
120 120
 
121
-	/**
122
-	 * removes all the entries
123
-	 */
124
-	public function clear($loadDefaultLinks = true) {
125
-		$this->entries = [];
126
-		$this->closureEntries = [];
127
-		$this->init = !$loadDefaultLinks;
128
-	}
121
+    /**
122
+     * removes all the entries
123
+     */
124
+    public function clear($loadDefaultLinks = true) {
125
+        $this->entries = [];
126
+        $this->closureEntries = [];
127
+        $this->init = !$loadDefaultLinks;
128
+    }
129 129
 
130
-	/**
131
-	 * Sets the current navigation entry of the currently running app
132
-	 * @param string $id of the app entry to activate (from added $entry)
133
-	 */
134
-	public function setActiveEntry($id) {
135
-		$this->activeEntry = $id;
136
-	}
130
+    /**
131
+     * Sets the current navigation entry of the currently running app
132
+     * @param string $id of the app entry to activate (from added $entry)
133
+     */
134
+    public function setActiveEntry($id) {
135
+        $this->activeEntry = $id;
136
+    }
137 137
 
138
-	/**
139
-	 * gets the active Menu entry
140
-	 * @return string id or empty string
141
-	 *
142
-	 * This function returns the id of the active navigation entry (set by
143
-	 * setActiveEntry
144
-	 */
145
-	public function getActiveEntry() {
146
-		return $this->activeEntry;
147
-	}
138
+    /**
139
+     * gets the active Menu entry
140
+     * @return string id or empty string
141
+     *
142
+     * This function returns the id of the active navigation entry (set by
143
+     * setActiveEntry
144
+     */
145
+    public function getActiveEntry() {
146
+        return $this->activeEntry;
147
+    }
148 148
 
149
-	private function init() {
150
-		if ($this->init) {
151
-			return;
152
-		}
153
-		$this->init = true;
149
+    private function init() {
150
+        if ($this->init) {
151
+            return;
152
+        }
153
+        $this->init = true;
154 154
 
155
-		$l = $this->l10nFac->get('lib');
156
-		if ($this->config->getSystemValue('knowledgebaseenabled', true)) {
157
-			$this->add([
158
-				'type' => 'settings',
159
-				'id' => 'help',
160
-				'order' => 5,
161
-				'href' => $this->urlGenerator->linkToRoute('settings_help'),
162
-				'name' => $l->t('Help'),
163
-				'icon' => $this->urlGenerator->imagePath('settings', 'help.svg'),
164
-			]);
165
-		}
155
+        $l = $this->l10nFac->get('lib');
156
+        if ($this->config->getSystemValue('knowledgebaseenabled', true)) {
157
+            $this->add([
158
+                'type' => 'settings',
159
+                'id' => 'help',
160
+                'order' => 5,
161
+                'href' => $this->urlGenerator->linkToRoute('settings_help'),
162
+                'name' => $l->t('Help'),
163
+                'icon' => $this->urlGenerator->imagePath('settings', 'help.svg'),
164
+            ]);
165
+        }
166 166
 
167
-		if ($this->userSession->isLoggedIn()) {
168
-			if ($this->isAdmin()) {
169
-				// App management
170
-				$this->add([
171
-					'type' => 'settings',
172
-					'id' => 'core_apps',
173
-					'order' => 3,
174
-					'href' => $this->urlGenerator->linkToRoute('settings.AppSettings.viewApps'),
175
-					'icon' => $this->urlGenerator->imagePath('settings', 'apps.svg'),
176
-					'name' => $l->t('Apps'),
177
-				]);
178
-			}
167
+        if ($this->userSession->isLoggedIn()) {
168
+            if ($this->isAdmin()) {
169
+                // App management
170
+                $this->add([
171
+                    'type' => 'settings',
172
+                    'id' => 'core_apps',
173
+                    'order' => 3,
174
+                    'href' => $this->urlGenerator->linkToRoute('settings.AppSettings.viewApps'),
175
+                    'icon' => $this->urlGenerator->imagePath('settings', 'apps.svg'),
176
+                    'name' => $l->t('Apps'),
177
+                ]);
178
+            }
179 179
 
180
-			// Personal and (if applicable) admin settings
181
-			$this->add([
182
-				'type' => 'settings',
183
-				'id' => 'settings',
184
-				'order' => 1,
185
-				'href' => $this->urlGenerator->linkToRoute('settings.PersonalSettings.index'),
186
-				'name' => $l->t('Settings'),
187
-				'icon' => $this->urlGenerator->imagePath('settings', 'admin.svg'),
188
-			]);
180
+            // Personal and (if applicable) admin settings
181
+            $this->add([
182
+                'type' => 'settings',
183
+                'id' => 'settings',
184
+                'order' => 1,
185
+                'href' => $this->urlGenerator->linkToRoute('settings.PersonalSettings.index'),
186
+                'name' => $l->t('Settings'),
187
+                'icon' => $this->urlGenerator->imagePath('settings', 'admin.svg'),
188
+            ]);
189 189
 
190
-			$logoutUrl = \OC_User::getLogoutUrl($this->urlGenerator);
191
-			if($logoutUrl !== '') {
192
-				// Logout
193
-				$this->add([
194
-					'type' => 'settings',
195
-					'id' => 'logout',
196
-					'order' => 99999,
197
-					'href' => $logoutUrl,
198
-					'name' => $l->t('Log out'),
199
-					'icon' => $this->urlGenerator->imagePath('core', 'actions/logout.svg'),
200
-				]);
201
-			}
190
+            $logoutUrl = \OC_User::getLogoutUrl($this->urlGenerator);
191
+            if($logoutUrl !== '') {
192
+                // Logout
193
+                $this->add([
194
+                    'type' => 'settings',
195
+                    'id' => 'logout',
196
+                    'order' => 99999,
197
+                    'href' => $logoutUrl,
198
+                    'name' => $l->t('Log out'),
199
+                    'icon' => $this->urlGenerator->imagePath('core', 'actions/logout.svg'),
200
+                ]);
201
+            }
202 202
 
203
-			if ($this->isSubadmin()) {
204
-				// User management
205
-				$this->add([
206
-					'type' => 'settings',
207
-					'id' => 'core_users',
208
-					'order' => 4,
209
-					'href' => $this->urlGenerator->linkToRoute('settings_users'),
210
-					'name' => $l->t('Users'),
211
-					'icon' => $this->urlGenerator->imagePath('settings', 'users.svg'),
212
-				]);
213
-			}
214
-		}
203
+            if ($this->isSubadmin()) {
204
+                // User management
205
+                $this->add([
206
+                    'type' => 'settings',
207
+                    'id' => 'core_users',
208
+                    'order' => 4,
209
+                    'href' => $this->urlGenerator->linkToRoute('settings_users'),
210
+                    'name' => $l->t('Users'),
211
+                    'icon' => $this->urlGenerator->imagePath('settings', 'users.svg'),
212
+                ]);
213
+            }
214
+        }
215 215
 
216
-		if ($this->appManager === 'null') {
217
-			return;
218
-		}
216
+        if ($this->appManager === 'null') {
217
+            return;
218
+        }
219 219
 
220
-		if ($this->userSession->isLoggedIn()) {
221
-			$apps = $this->appManager->getEnabledAppsForUser($this->userSession->getUser());
222
-		} else {
223
-			$apps = $this->appManager->getInstalledApps();
224
-		}
220
+        if ($this->userSession->isLoggedIn()) {
221
+            $apps = $this->appManager->getEnabledAppsForUser($this->userSession->getUser());
222
+        } else {
223
+            $apps = $this->appManager->getInstalledApps();
224
+        }
225 225
 
226
-		foreach ($apps as $app) {
227
-			if (!$this->userSession->isLoggedIn() && !$this->appManager->isEnabledForUser($app, $this->userSession->getUser())) {
228
-				continue;
229
-			}
226
+        foreach ($apps as $app) {
227
+            if (!$this->userSession->isLoggedIn() && !$this->appManager->isEnabledForUser($app, $this->userSession->getUser())) {
228
+                continue;
229
+            }
230 230
 
231
-			// load plugins and collections from info.xml
232
-			$info = $this->appManager->getAppInfo($app);
233
-			if (empty($info['navigations'])) {
234
-				continue;
235
-			}
236
-			foreach ($info['navigations'] as $nav) {
237
-				if (!isset($nav['name'])) {
238
-					continue;
239
-				}
240
-				if (!isset($nav['route'])) {
241
-					continue;
242
-				}
243
-				$role = isset($nav['@attributes']['role']) ? $nav['@attributes']['role'] : 'all';
244
-				if ($role === 'admin' && !$this->isAdmin()) {
245
-					continue;
246
-				}
247
-				$l = $this->l10nFac->get($app);
248
-				$id = isset($nav['id']) ? $nav['id'] : $app;
249
-				$order = isset($nav['order']) ? $nav['order'] : 100;
250
-				$type = isset($nav['type']) ? $nav['type'] : 'link';
251
-				$route = $this->urlGenerator->linkToRoute($nav['route']);
252
-				$icon = isset($nav['icon']) ? $nav['icon'] : 'app.svg';
253
-				foreach ([$icon, "$app.svg"] as $i) {
254
-					try {
255
-						$icon = $this->urlGenerator->imagePath($app, $i);
256
-						break;
257
-					} catch (\RuntimeException $ex) {
258
-						// no icon? - ignore it then
259
-					}
260
-				}
261
-				if ($icon === null) {
262
-					$icon = $this->urlGenerator->imagePath('core', 'default-app-icon');
263
-				}
231
+            // load plugins and collections from info.xml
232
+            $info = $this->appManager->getAppInfo($app);
233
+            if (empty($info['navigations'])) {
234
+                continue;
235
+            }
236
+            foreach ($info['navigations'] as $nav) {
237
+                if (!isset($nav['name'])) {
238
+                    continue;
239
+                }
240
+                if (!isset($nav['route'])) {
241
+                    continue;
242
+                }
243
+                $role = isset($nav['@attributes']['role']) ? $nav['@attributes']['role'] : 'all';
244
+                if ($role === 'admin' && !$this->isAdmin()) {
245
+                    continue;
246
+                }
247
+                $l = $this->l10nFac->get($app);
248
+                $id = isset($nav['id']) ? $nav['id'] : $app;
249
+                $order = isset($nav['order']) ? $nav['order'] : 100;
250
+                $type = isset($nav['type']) ? $nav['type'] : 'link';
251
+                $route = $this->urlGenerator->linkToRoute($nav['route']);
252
+                $icon = isset($nav['icon']) ? $nav['icon'] : 'app.svg';
253
+                foreach ([$icon, "$app.svg"] as $i) {
254
+                    try {
255
+                        $icon = $this->urlGenerator->imagePath($app, $i);
256
+                        break;
257
+                    } catch (\RuntimeException $ex) {
258
+                        // no icon? - ignore it then
259
+                    }
260
+                }
261
+                if ($icon === null) {
262
+                    $icon = $this->urlGenerator->imagePath('core', 'default-app-icon');
263
+                }
264 264
 
265
-				$this->add([
266
-					'id' => $id,
267
-					'order' => $order,
268
-					'href' => $route,
269
-					'icon' => $icon,
270
-					'type' => $type,
271
-					'name' => $l->t($nav['name']),
272
-				]);
273
-			}
274
-		}
275
-	}
265
+                $this->add([
266
+                    'id' => $id,
267
+                    'order' => $order,
268
+                    'href' => $route,
269
+                    'icon' => $icon,
270
+                    'type' => $type,
271
+                    'name' => $l->t($nav['name']),
272
+                ]);
273
+            }
274
+        }
275
+    }
276 276
 
277
-	private function isAdmin() {
278
-		$user = $this->userSession->getUser();
279
-		if ($user !== null) {
280
-			return $this->groupManager->isAdmin($user->getUID());
281
-		}
282
-		return false;
283
-	}
277
+    private function isAdmin() {
278
+        $user = $this->userSession->getUser();
279
+        if ($user !== null) {
280
+            return $this->groupManager->isAdmin($user->getUID());
281
+        }
282
+        return false;
283
+    }
284 284
 
285
-	private function isSubadmin() {
286
-		$user = $this->userSession->getUser();
287
-		if ($user !== null) {
288
-			return $this->groupManager->getSubAdmin()->isSubAdmin($user);
289
-		}
290
-		return false;
291
-	}
285
+    private function isSubadmin() {
286
+        $user = $this->userSession->getUser();
287
+        if ($user !== null) {
288
+            return $this->groupManager->getSubAdmin()->isSubAdmin($user);
289
+        }
290
+        return false;
291
+    }
292 292
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -88,10 +88,10 @@  discard block
 block discarded – undo
88 88
 		}
89 89
 
90 90
 		$entry['active'] = false;
91
-		if(!isset($entry['icon'])) {
91
+		if (!isset($entry['icon'])) {
92 92
 			$entry['icon'] = '';
93 93
 		}
94
-		if(!isset($entry['type'])) {
94
+		if (!isset($entry['type'])) {
95 95
 			$entry['type'] = 'link';
96 96
 		}
97 97
 		$this->entries[] = $entry;
@@ -188,7 +188,7 @@  discard block
 block discarded – undo
188 188
 			]);
189 189
 
190 190
 			$logoutUrl = \OC_User::getLogoutUrl($this->urlGenerator);
191
-			if($logoutUrl !== '') {
191
+			if ($logoutUrl !== '') {
192 192
 				// Logout
193 193
 				$this->add([
194 194
 					'type' => 'settings',
Please login to merge, or discard this patch.
lib/public/Authentication/IApacheBackend.php 1 patch
Indentation   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -38,27 +38,27 @@
 block discarded – undo
38 38
  */
39 39
 interface IApacheBackend {
40 40
 
41
-	/**
42
-	 * In case the user has been authenticated by a module true is returned.
43
-	 *
44
-	 * @return boolean whether the module reports a user as currently logged in.
45
-	 * @since 6.0.0
46
-	 */
47
-	public function isSessionActive();
41
+    /**
42
+     * In case the user has been authenticated by a module true is returned.
43
+     *
44
+     * @return boolean whether the module reports a user as currently logged in.
45
+     * @since 6.0.0
46
+     */
47
+    public function isSessionActive();
48 48
 
49
-	/**
50
-	 * Gets the current logout URL
51
-	 *
52
-	 * @return string
53
-	 * @since 12.0.3
54
-	 */
55
-	public function getLogoutUrl();
49
+    /**
50
+     * Gets the current logout URL
51
+     *
52
+     * @return string
53
+     * @since 12.0.3
54
+     */
55
+    public function getLogoutUrl();
56 56
 
57
-	/**
58
-	 * Return the id of the current user
59
-	 * @return string
60
-	 * @since 6.0.0
61
-	 */
62
-	public function getCurrentUserId();
57
+    /**
58
+     * Return the id of the current user
59
+     * @return string
60
+     * @since 6.0.0
61
+     */
62
+    public function getCurrentUserId();
63 63
 
64 64
 }
Please login to merge, or discard this patch.
core/templates/twofactorselectchallenge.php 1 patch
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -7,11 +7,11 @@  discard block
 block discarded – undo
7 7
 				<li>
8 8
 					<a class="button two-factor-provider"
9 9
 					   href="<?php p(\OC::$server->getURLGenerator()->linkToRoute('core.TwoFactorChallenge.showChallenge',
10
-										[
11
-											'challengeProviderId' => $provider->getId(),
12
-											'redirect_url' => $_['redirect_url'],
13
-										]
14
-									)) ?>">
10
+                                        [
11
+                                            'challengeProviderId' => $provider->getId(),
12
+                                            'redirect_url' => $_['redirect_url'],
13
+                                        ]
14
+                                    )) ?>">
15 15
 						<?php p($provider->getDescription()) ?>
16 16
 					</a>
17 17
 				</li>
@@ -22,11 +22,11 @@  discard block
 block discarded – undo
22 22
 		<a class="button" href="<?php print_unescaped($_['logout_url']); ?>"><?php p($l->t('Cancel log in')) ?></a>
23 23
 		<?php if (!is_null($_['backupProvider'])): ?>
24 24
 		<a class="button" href="<?php p(\OC::$server->getURLGenerator()->linkToRoute('core.TwoFactorChallenge.showChallenge',
25
-												[
26
-													'challengeProviderId' => $_['backupProvider']->getId(),
27
-													'redirect_url' => $_['redirect_url'],
28
-												]
29
-											)) ?>"><?php p($l->t('Use backup code')) ?></a>
25
+                                                [
26
+                                                    'challengeProviderId' => $_['backupProvider']->getId(),
27
+                                                    'redirect_url' => $_['redirect_url'],
28
+                                                ]
29
+                                            )) ?>"><?php p($l->t('Use backup code')) ?></a>
30 30
 		<?php endif; ?>
31 31
 	</p>
32 32
 </div>
Please login to merge, or discard this patch.
core/templates/twofactorshowchallenge.php 1 patch
Indentation   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -25,11 +25,11 @@
 block discarded – undo
25 25
 		<a class="button" href="<?php print_unescaped($_['logout_url']); ?>"><?php p($l->t('Cancel log in')) ?></a>
26 26
 		<?php if (!is_null($_['backupProvider'])): ?>
27 27
 		<a class="button" href="<?php p(\OC::$server->getURLGenerator()->linkToRoute('core.TwoFactorChallenge.showChallenge',
28
-												[
29
-													'challengeProviderId' => $_['backupProvider']->getId(),
30
-													'redirect_url' => $_['redirect_url'],
31
-												]
32
-											)) ?>"><?php p($l->t('Use backup code')) ?></a>
28
+                                                [
29
+                                                    'challengeProviderId' => $_['backupProvider']->getId(),
30
+                                                    'redirect_url' => $_['redirect_url'],
31
+                                                ]
32
+                                            )) ?>"><?php p($l->t('Use backup code')) ?></a>
33 33
 		<?php endif; ?>
34 34
 	</p>
35 35
 </div>
Please login to merge, or discard this patch.
core/Controller/TwoFactorChallengeController.php 1 patch
Indentation   +138 added lines, -138 removed lines patch added patch discarded remove patch
@@ -37,147 +37,147 @@
 block discarded – undo
37 37
 
38 38
 class TwoFactorChallengeController extends Controller {
39 39
 
40
-	/** @var Manager */
41
-	private $twoFactorManager;
42
-
43
-	/** @var IUserSession */
44
-	private $userSession;
45
-
46
-	/** @var ISession */
47
-	private $session;
48
-
49
-	/** @var IURLGenerator */
50
-	private $urlGenerator;
51
-
52
-	/**
53
-	 * @param string $appName
54
-	 * @param IRequest $request
55
-	 * @param Manager $twoFactorManager
56
-	 * @param IUserSession $userSession
57
-	 * @param ISession $session
58
-	 * @param IURLGenerator $urlGenerator
59
-	 */
60
-	public function __construct($appName, IRequest $request, Manager $twoFactorManager, IUserSession $userSession,
61
-		ISession $session, IURLGenerator $urlGenerator) {
62
-		parent::__construct($appName, $request);
63
-		$this->twoFactorManager = $twoFactorManager;
64
-		$this->userSession = $userSession;
65
-		$this->session = $session;
66
-		$this->urlGenerator = $urlGenerator;
67
-	}
68
-
69
-	/**
70
-	 * @return string
71
-	 */
72
-	protected function getLogoutUrl() {
73
-		return OC_User::getLogoutUrl($this->urlGenerator);
74
-	}
75
-
76
-	/**
77
-	 * @NoAdminRequired
78
-	 * @NoCSRFRequired
79
-	 *
80
-	 * @param string $redirect_url
81
-	 * @return TemplateResponse
82
-	 */
83
-	public function selectChallenge($redirect_url) {
84
-		$user = $this->userSession->getUser();
85
-		$providers = $this->twoFactorManager->getProviders($user);
86
-		$backupProvider = $this->twoFactorManager->getBackupProvider($user);
87
-
88
-		$data = [
89
-			'providers' => $providers,
90
-			'backupProvider' => $backupProvider,
91
-			'redirect_url' => $redirect_url,
92
-			'logout_url' => $this->getLogoutUrl(),
93
-		];
94
-		return new TemplateResponse($this->appName, 'twofactorselectchallenge', $data, 'guest');
95
-	}
96
-
97
-	/**
98
-	 * @NoAdminRequired
99
-	 * @NoCSRFRequired
100
-	 * @UseSession
101
-	 *
102
-	 * @param string $challengeProviderId
103
-	 * @param string $redirect_url
104
-	 * @return TemplateResponse|RedirectResponse
105
-	 */
106
-	public function showChallenge($challengeProviderId, $redirect_url) {
107
-		$user = $this->userSession->getUser();
108
-		$provider = $this->twoFactorManager->getProvider($user, $challengeProviderId);
109
-		if (is_null($provider)) {
110
-			return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.selectChallenge'));
111
-		}
112
-
113
-		$backupProvider = $this->twoFactorManager->getBackupProvider($user);
114
-		if (!is_null($backupProvider) && $backupProvider->getId() === $provider->getId()) {
115
-			// Don't show the backup provider link if we're already showing that provider's challenge
116
-			$backupProvider = null;
117
-		}
118
-
119
-		$errorMessage = '';
120
-		$error = false;
121
-		if ($this->session->exists('two_factor_auth_error')) {
122
-			$this->session->remove('two_factor_auth_error');
123
-			$error = true;
124
-			$errorMessage = $this->session->get("two_factor_auth_error_message");
125
-			$this->session->remove('two_factor_auth_error_message');
126
-		}
127
-		$tmpl = $provider->getTemplate($user);
128
-		$tmpl->assign('redirect_url', $redirect_url);
129
-		$data = [
130
-			'error' => $error,
131
-			'error_message' => $errorMessage,
132
-			'provider' => $provider,
133
-			'backupProvider' => $backupProvider,
134
-			'logout_url' => $this->getLogoutUrl(),
135
-			'redirect_url' => $redirect_url,
136
-			'template' => $tmpl->fetchPage(),
137
-		];
138
-		return new TemplateResponse($this->appName, 'twofactorshowchallenge', $data, 'guest');
139
-	}
140
-
141
-	/**
142
-	 * @NoAdminRequired
143
-	 * @NoCSRFRequired
144
-	 * @UseSession
145
-	 *
146
-	 * @UserRateThrottle(limit=5, period=100)
147
-	 *
148
-	 * @param string $challengeProviderId
149
-	 * @param string $challenge
150
-	 * @param string $redirect_url
151
-	 * @return RedirectResponse
152
-	 */
153
-	public function solveChallenge($challengeProviderId, $challenge, $redirect_url = null) {
154
-		$user = $this->userSession->getUser();
155
-		$provider = $this->twoFactorManager->getProvider($user, $challengeProviderId);
156
-		if (is_null($provider)) {
157
-			return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.selectChallenge'));
158
-		}
159
-
160
-		try {
161
-			if ($this->twoFactorManager->verifyChallenge($challengeProviderId, $user, $challenge)) {
162
-				if (!is_null($redirect_url)) {
163
-					return new RedirectResponse($this->urlGenerator->getAbsoluteURL(urldecode($redirect_url)));
164
-				}
165
-				return new RedirectResponse(OC_Util::getDefaultPageUrl());
166
-			}
167
-		} catch (TwoFactorException $e) {
168
-			/*
40
+    /** @var Manager */
41
+    private $twoFactorManager;
42
+
43
+    /** @var IUserSession */
44
+    private $userSession;
45
+
46
+    /** @var ISession */
47
+    private $session;
48
+
49
+    /** @var IURLGenerator */
50
+    private $urlGenerator;
51
+
52
+    /**
53
+     * @param string $appName
54
+     * @param IRequest $request
55
+     * @param Manager $twoFactorManager
56
+     * @param IUserSession $userSession
57
+     * @param ISession $session
58
+     * @param IURLGenerator $urlGenerator
59
+     */
60
+    public function __construct($appName, IRequest $request, Manager $twoFactorManager, IUserSession $userSession,
61
+        ISession $session, IURLGenerator $urlGenerator) {
62
+        parent::__construct($appName, $request);
63
+        $this->twoFactorManager = $twoFactorManager;
64
+        $this->userSession = $userSession;
65
+        $this->session = $session;
66
+        $this->urlGenerator = $urlGenerator;
67
+    }
68
+
69
+    /**
70
+     * @return string
71
+     */
72
+    protected function getLogoutUrl() {
73
+        return OC_User::getLogoutUrl($this->urlGenerator);
74
+    }
75
+
76
+    /**
77
+     * @NoAdminRequired
78
+     * @NoCSRFRequired
79
+     *
80
+     * @param string $redirect_url
81
+     * @return TemplateResponse
82
+     */
83
+    public function selectChallenge($redirect_url) {
84
+        $user = $this->userSession->getUser();
85
+        $providers = $this->twoFactorManager->getProviders($user);
86
+        $backupProvider = $this->twoFactorManager->getBackupProvider($user);
87
+
88
+        $data = [
89
+            'providers' => $providers,
90
+            'backupProvider' => $backupProvider,
91
+            'redirect_url' => $redirect_url,
92
+            'logout_url' => $this->getLogoutUrl(),
93
+        ];
94
+        return new TemplateResponse($this->appName, 'twofactorselectchallenge', $data, 'guest');
95
+    }
96
+
97
+    /**
98
+     * @NoAdminRequired
99
+     * @NoCSRFRequired
100
+     * @UseSession
101
+     *
102
+     * @param string $challengeProviderId
103
+     * @param string $redirect_url
104
+     * @return TemplateResponse|RedirectResponse
105
+     */
106
+    public function showChallenge($challengeProviderId, $redirect_url) {
107
+        $user = $this->userSession->getUser();
108
+        $provider = $this->twoFactorManager->getProvider($user, $challengeProviderId);
109
+        if (is_null($provider)) {
110
+            return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.selectChallenge'));
111
+        }
112
+
113
+        $backupProvider = $this->twoFactorManager->getBackupProvider($user);
114
+        if (!is_null($backupProvider) && $backupProvider->getId() === $provider->getId()) {
115
+            // Don't show the backup provider link if we're already showing that provider's challenge
116
+            $backupProvider = null;
117
+        }
118
+
119
+        $errorMessage = '';
120
+        $error = false;
121
+        if ($this->session->exists('two_factor_auth_error')) {
122
+            $this->session->remove('two_factor_auth_error');
123
+            $error = true;
124
+            $errorMessage = $this->session->get("two_factor_auth_error_message");
125
+            $this->session->remove('two_factor_auth_error_message');
126
+        }
127
+        $tmpl = $provider->getTemplate($user);
128
+        $tmpl->assign('redirect_url', $redirect_url);
129
+        $data = [
130
+            'error' => $error,
131
+            'error_message' => $errorMessage,
132
+            'provider' => $provider,
133
+            'backupProvider' => $backupProvider,
134
+            'logout_url' => $this->getLogoutUrl(),
135
+            'redirect_url' => $redirect_url,
136
+            'template' => $tmpl->fetchPage(),
137
+        ];
138
+        return new TemplateResponse($this->appName, 'twofactorshowchallenge', $data, 'guest');
139
+    }
140
+
141
+    /**
142
+     * @NoAdminRequired
143
+     * @NoCSRFRequired
144
+     * @UseSession
145
+     *
146
+     * @UserRateThrottle(limit=5, period=100)
147
+     *
148
+     * @param string $challengeProviderId
149
+     * @param string $challenge
150
+     * @param string $redirect_url
151
+     * @return RedirectResponse
152
+     */
153
+    public function solveChallenge($challengeProviderId, $challenge, $redirect_url = null) {
154
+        $user = $this->userSession->getUser();
155
+        $provider = $this->twoFactorManager->getProvider($user, $challengeProviderId);
156
+        if (is_null($provider)) {
157
+            return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.selectChallenge'));
158
+        }
159
+
160
+        try {
161
+            if ($this->twoFactorManager->verifyChallenge($challengeProviderId, $user, $challenge)) {
162
+                if (!is_null($redirect_url)) {
163
+                    return new RedirectResponse($this->urlGenerator->getAbsoluteURL(urldecode($redirect_url)));
164
+                }
165
+                return new RedirectResponse(OC_Util::getDefaultPageUrl());
166
+            }
167
+        } catch (TwoFactorException $e) {
168
+            /*
169 169
 			 * The 2FA App threw an TwoFactorException. Now we display more
170 170
 			 * information to the user. The exception text is stored in the
171 171
 			 * session to be used in showChallenge()
172 172
 			 */
173
-			$this->session->set('two_factor_auth_error_message', $e->getMessage());
174
-		}
175
-
176
-		$this->session->set('two_factor_auth_error', true);
177
-		return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.showChallenge', [
178
-			'challengeProviderId' => $provider->getId(),
179
-			'redirect_url' => $redirect_url,
180
-		]));
181
-	}
173
+            $this->session->set('two_factor_auth_error_message', $e->getMessage());
174
+        }
175
+
176
+        $this->session->set('two_factor_auth_error', true);
177
+        return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.showChallenge', [
178
+            'challengeProviderId' => $provider->getId(),
179
+            'redirect_url' => $redirect_url,
180
+        ]));
181
+    }
182 182
 
183 183
 }
Please login to merge, or discard this patch.