user.php 16.8 KB
Newer Older
Robin's avatar
Robin committed
1
2
<?php
/**
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
3
4
5
6
 * @author Aldo "xoen" Giambelluca <xoen@xoen.org>
 * @author Andreas Fischer <bantu@owncloud.com>
 * @author Arthur Schiwon <blizzz@owncloud.com>
 * @author Bart Visscher <bartv@thisnet.nl>
Lukas Reschke's avatar
Lukas Reschke committed
7
 * @author Bartek Przybylski <bart.p.pl@gmail.com>
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
8
9
10
11
12
 * @author Björn Schießle <schiessle@owncloud.com>
 * @author Dominik Schmidt <dev@dominik-schmidt.de>
 * @author Florian Preinstorfer <nblock@archlinux.us>
 * @author Georg Ehrke <georg@owncloud.com>
 * @author Jakob Sack <mail@jakobsack.de>
13
 * @author Joas Schilling <nickvergessen@owncloud.com>
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
14
15
16
17
18
19
20
21
22
23
 * @author Jörn Friedrich Dreyer <jfd@butonic.de>
 * @author Lukas Reschke <lukas@owncloud.com>
 * @author Morris Jobke <hey@morrisjobke.de>
 * @author Robin Appelman <icewind@owncloud.com>
 * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
 * @author Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>
 * @author shkdee <louis.traynard@m4x.org>
 * @author Thomas Müller <thomas.mueller@tmit.eu>
 * @author Tom Needham <tom@owncloud.com>
 * @author Victor Dubiniuk <dubiniuk@owncloud.com>
24
 *
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
25
26
 * @copyright Copyright (c) 2015, ownCloud, Inc.
 * @license AGPL-3.0
27
 *
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
28
29
30
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
31
 *
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
32
 * This program is distributed in the hope that it will be useful,
33
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
34
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
36
 *
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
37
38
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
39
40
 *
 */
41
42
43
44
45
46

/**
 * This class provides wrapper methods for user management. Multiple backends are
 * supported. User management operations are delegated to the configured backend for
 * execution.
 *
47
48
 * Note that &run is deprecated and won't work anymore.
 *
49
50
51
52
53
54
55
56
57
58
59
 * Hooks provided:
 *   pre_createUser(&run, uid, password)
 *   post_createUser(uid, password)
 *   pre_deleteUser(&run, uid)
 *   post_deleteUser(uid)
 *   pre_setPassword(&run, uid, password, recoveryPassword)
 *   post_setPassword(uid, password, recoveryPassword)
 *   pre_login(&run, uid, password)
 *   post_login(uid)
 *   logout()
 */
Jakob Sack's avatar
Jakob Sack committed
60
class OC_User {
Lukas Reschke's avatar
Lukas Reschke committed
61
62
63
64

	/**
	 * @return \OC\User\Session
	 */
Robin Appelman's avatar
Robin Appelman committed
65
	public static function getUserSession() {
66
		return OC::$server->getUserSession();
67
68
	}

69
	private static $_backends = array();
70

71
72
73
74
	private static $_usedBackends = array();

	private static $_setupedBackends = array();

75
76
77
	// bool, stores if a user want to access a resource anonymously, e.g if he opens a public link
	private static $incognitoMode = false;

78
	/**
79
	 * Adds the backend to the list of used backends
80
	 *
81
	 * @param string|OC_User_Interface $backend default: database The backend to use for user management
82
	 * @return bool
83
84
85
	 *
	 * Set the User Authentication Module
	 */
86
87
88
	public static function useBackend($backend = 'database') {
		if ($backend instanceof OC_User_Interface) {
			self::$_usedBackends[get_class($backend)] = $backend;
89
			\OC::$server->getUserManager()->registerBackend($backend);
90
		} else {
91
			// You'll never know what happens
92
			if (null === $backend OR !is_string($backend)) {
93
94
				$backend = 'database';
			}
95

96
			// Load backend
97
			switch ($backend) {
98
99
100
				case 'database':
				case 'mysql':
				case 'sqlite':
Thomas Müller's avatar
Thomas Müller committed
101
					\OCP\Util::writeLog('core', 'Adding user backend ' . $backend . '.', \OCP\Util::DEBUG);
102
					self::$_usedBackends[$backend] = new OC_User_Database();
103
					\OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
104
					break;
105
106
				case 'dummy':
					self::$_usedBackends[$backend] = new \Test\Util\User\Dummy();
107
					\OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
108
					break;
109
				default:
Thomas Müller's avatar
Thomas Müller committed
110
					\OCP\Util::writeLog('core', 'Adding default user backend ' . $backend . '.', \OCP\Util::DEBUG);
111
112
					$className = 'OC_USER_' . strToUpper($backend);
					self::$_usedBackends[$backend] = new $className();
113
					\OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
114
115
					break;
			}
116
		}
117
		return true;
118
	}
119

120
121
122
	/**
	 * remove all used backends
	 */
Thomas Mueller's avatar
Thomas Mueller committed
123
	public static function clearBackends() {
124
		self::$_usedBackends = array();
125
		\OC::$server->getUserManager()->clearBackends();
126
127
	}

128
129
130
	/**
	 * setup the configured backends in config.php
	 */
Thomas Mueller's avatar
Thomas Mueller committed
131
	public static function setupBackends() {
132
		OC_App::loadApps(array('prelogin'));
133
134
135
136
137
138
		$backends = OC_Config::getValue('user_backends', array());
		foreach ($backends as $i => $config) {
			$class = $config['class'];
			$arguments = $config['arguments'];
			if (class_exists($class)) {
				if (array_search($i, self::$_setupedBackends) === false) {
139
140
					// make a reflection object
					$reflectionObj = new ReflectionClass($class);
141

142
143
144
					// use Reflection to create a new instance, using the $args
					$backend = $reflectionObj->newInstanceArgs($arguments);
					self::useBackend($backend);
shkdee's avatar
shkdee committed
145
					self::$_setupedBackends[] = $i;
146
				} else {
Thomas Müller's avatar
Thomas Müller committed
147
					\OCP\Util::writeLog('core', 'User backend ' . $class . ' already initialized.', \OCP\Util::DEBUG);
148
149
				}
			} else {
Thomas Müller's avatar
Thomas Müller committed
150
				\OCP\Util::writeLog('core', 'User backend ' . $class . ' not found.', \OCP\Util::ERROR);
151
152
153
154
			}
		}
	}

155
	/**
156
	 * delete a user
157
	 *
158
159
	 * @param string $uid The username of the user to delete
	 * @return bool
Jakob Sack's avatar
Jakob Sack committed
160
161
	 *
	 * Deletes a user
162
	 * @deprecated Use \OC::$server->getUserManager()->get() and then run delete() on the return
163
	 */
164
	public static function deleteUser($uid) {
165
		$user = \OC::$server->getUserManager()->get($uid);
166
		if ($user) {
167
			return $user->delete();
168
169
		} else {
			return false;
170
		}
171
172
	}

Robin's avatar
Robin committed
173
	/**
174
	 * Try to login a user
175
	 *
Arthur Schiwon's avatar
Arthur Schiwon committed
176
	 * @param string $loginname The login name of the user to log in
Lukas Reschke's avatar
Lukas Reschke committed
177
	 * @param string $password The password of the user
178
	 * @return boolean|null
Jakob Sack's avatar
Jakob Sack committed
179
	 *
180
	 * Log in a user and regenerate a new session - if the password is ok
181
	 */
Arthur Schiwon's avatar
Arthur Schiwon committed
182
	public static function login($loginname, $password) {
183
		session_regenerate_id(true);
Arthur Schiwon's avatar
Arthur Schiwon committed
184
		$result = self::getUserSession()->login($loginname, $password);
185
		if ($result) {
Arthur Schiwon's avatar
Arthur Schiwon committed
186
			//we need to pass the user name, which may differ from login name
187
188
189
190
			$user = self::getUserSession()->getUser()->getUID();
			OC_Util::setupFS($user);
			//trigger creation of user home and /files folder
			\OC::$server->getUserFolder($user);
191
192
		}
		return $result;
193
194
	}

195
196
197
198
199
200
201
202
203
204
205
	/**
	 * Try to login a user using the magic cookie (remember login)
	 *
	 * @param string $uid The username of the user to log in
	 * @param string $token
	 * @return bool
	 */
	public static function loginWithCookie($uid, $token) {
		return self::getUserSession()->loginWithCookie($uid, $token);
	}

206
	/**
207
	 * Try to login a user, assuming authentication
208
	 * has already happened (e.g. via Single Sign On).
209
210
	 *
	 * Log in a user and regenerate a new session.
211
	 *
212
	 * @param \OCP\Authentication\IApacheBackend $backend
213
	 * @return bool
214
	 */
215
	public static function loginWithApache(\OCP\Authentication\IApacheBackend $backend) {
216

217
		$uid = $backend->getCurrentUserId();
218
		$run = true;
219
		OC_Hook::emit("OC_User", "pre_login", array("run" => &$run, "uid" => $uid));
220

221
		if ($uid) {
222
223
224
225
			if (self::getUser() !== $uid) {
				self::setUserId($uid);
				self::setDisplayName($uid);
				self::getUserSession()->setLoginName($uid);
226

227
228
				OC_Hook::emit("OC_User", "post_login", array("uid" => $uid, 'password' => ''));
			}
229
230
231
232
233
234
			return true;
		}
		return false;
	}

	/**
235
	 * Verify with Apache whether user is authenticated.
236
	 *
237
238
239
240
	 * @return boolean|null
	 *          true: authenticated
	 *          false: not authenticated
	 *          null: not handled / no backend available
241
	 */
242
	public static function handleApacheAuth() {
243
244
245
		$backend = self::findFirstActiveUsedBackend();
		if ($backend) {
			OC_App::loadApps();
246

247
248
249
			//setup extra user backends
			self::setupBackends();
			self::unsetMagicInCookie();
250

251
			return self::loginWithApache($backend);
252
253
		}

254
		return null;
255
256
257
	}


258
	/**
259
	 * Sets user id for session and triggers emit
260
261
	 */
	public static function setUserId($uid) {
262
263
264
265
266
267
268
		$userSession = \OC::$server->getUserSession();
		$userManager = \OC::$server->getUserManager();
		if ($user = $userManager->get($uid)) {
			$userSession->setUser($user);
		} else {
			\OC::$server->getSession()->set('user_id', $uid);
		}
269
	}
270

Arthur Schiwon's avatar
Typo    
Arthur Schiwon committed
271
	/**
272
	 * Sets user display name for session
273
	 *
274
	 * @param string $uid
Lukas Reschke's avatar
Lukas Reschke committed
275
276
	 * @param null $displayName
	 * @return bool Whether the display name could get set
Arthur Schiwon's avatar
Typo    
Arthur Schiwon committed
277
	 */
278
	public static function setDisplayName($uid, $displayName = null) {
279
280
		if (is_null($displayName)) {
			$displayName = $uid;
281
		}
282
		$user = \OC::$server->getUserManager()->get($uid);
283
284
285
286
		if ($user) {
			return $user->setDisplayName($displayName);
		} else {
			return false;
287
		}
288
	}
Arthur Schiwon's avatar
Typo    
Arthur Schiwon committed
289

290
	/**
291
	 * Logs the current user out and kills all the session data
Jakob Sack's avatar
Jakob Sack committed
292
293
	 *
	 * Logout, destroys session
294
	 */
Thomas Mueller's avatar
Thomas Mueller committed
295
	public static function logout() {
296
		self::getUserSession()->logout();
297
298
	}

299
300
301
302
	/**
	 * Tries to login the user with HTTP Basic Authentication
	 */
	public static function tryBasicAuthLogin() {
303
		if (!empty($_SERVER['PHP_AUTH_USER']) && !empty($_SERVER['PHP_AUTH_PW'])) {
304
305
306
307
			\OC_User::login($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
		}
	}

Robin's avatar
Robin committed
308
	/**
309
	 * Check if the user is logged in, considers also the HTTP basic credentials
310
	 *
311
	 * @return bool
312
	 */
Thomas Mueller's avatar
Thomas Mueller committed
313
	public static function isLoggedIn() {
Jörn Friedrich Dreyer's avatar
Jörn Friedrich Dreyer committed
314
315
		if (\OC::$server->getSession()->get('user_id') !== null && self::$incognitoMode === false) {
			return self::userExists(\OC::$server->getSession()->get('user_id'));
316
		}
317

318
		return false;
319
	}
320

321
	/**
322
	 * set incognito mode, e.g. if a user wants to open a public link
323
	 *
324
325
326
327
	 * @param bool $status
	 */
	public static function setIncognitoMode($status) {
		self::$incognitoMode = $status;
328
	}
329

330
331
	/**
	 * get incognito mode status
332
	 *
333
334
335
336
	 * @return bool
	 */
	public static function isIncognitoMode() {
		return self::$incognitoMode;
337
338
	}

339
	/**
340
	 * Supplies an attribute to the logout hyperlink. The default behaviour
341
342
343
	 * is to return an href with '?logout=true' appended. However, it can
	 * supply any attribute(s) which are valid for <a>.
	 *
344
	 * @return string with one or more HTML attributes.
345
346
	 */
	public static function getLogoutAttribute() {
347
348
349
		$backend = self::findFirstActiveUsedBackend();
		if ($backend) {
			return $backend->getLogoutAttribute();
350
351
		}

Morris Jobke's avatar
Morris Jobke committed
352
		return 'href="' . link_to('', 'index.php') . '?logout=true&amp;requesttoken=' . urlencode(OC_Util::callRegister()) . '"';
353
354
	}

Lukas Reschke's avatar
Lukas Reschke committed
355
	/**
356
	 * Check if the user is an admin user
357
	 *
358
359
	 * @param string $uid uid of the admin
	 * @return bool
Lukas Reschke's avatar
Lukas Reschke committed
360
361
	 */
	public static function isAdminUser($uid) {
362
		if (OC_Group::inGroup($uid, 'admin') && self::$incognitoMode === false) {
Lukas Reschke's avatar
Lukas Reschke committed
363
364
365
366
367
368
			return true;
		}
		return false;
	}


369
	/**
370
	 * get the user id of the user currently logged in.
371
	 *
Lukas Reschke's avatar
Lukas Reschke committed
372
	 * @return string|bool uid or false
373
	 */
Thomas Mueller's avatar
Thomas Mueller committed
374
	public static function getUser() {
Jörn Friedrich Dreyer's avatar
Jörn Friedrich Dreyer committed
375
		$uid = \OC::$server->getSession() ? \OC::$server->getSession()->get('user_id') : null;
376
		if (!is_null($uid) && self::$incognitoMode === false) {
377
			return $uid;
378
		} else {
379
380
381
382
			return false;
		}
	}

Arthur Schiwon's avatar
Typo    
Arthur Schiwon committed
383
	/**
384
	 * get the display name of the user currently logged in.
385
	 *
386
	 * @param string $uid
Robin Appelman's avatar
Robin Appelman committed
387
	 * @return string uid or false
Arthur Schiwon's avatar
Typo    
Arthur Schiwon committed
388
	 */
389
390
	public static function getDisplayName($uid = null) {
		if ($uid) {
391
			$user = \OC::$server->getUserManager()->get($uid);
392
393
394
395
396
			if ($user) {
				return $user->getDisplayName();
			} else {
				return $uid;
			}
397
398
399
400
401
402
403
		} else {
			$user = self::getUserSession()->getUser();
			if ($user) {
				return $user->getDisplayName();
			} else {
				return false;
			}
Arthur Schiwon's avatar
Typo    
Arthur Schiwon committed
404
		}
405
	}
Arthur Schiwon's avatar
Typo    
Arthur Schiwon committed
406

Robin's avatar
Robin committed
407
	/**
408
	 * Autogenerate a password
409
	 *
410
	 * @return string
Jakob Sack's avatar
Jakob Sack committed
411
412
	 *
	 * generates a password
413
	 */
Thomas Mueller's avatar
Thomas Mueller committed
414
	public static function generatePassword() {
Lukas Reschke's avatar
Lukas Reschke committed
415
		return \OC::$server->getSecureRandom()->getMediumStrengthGenerator()->generate(30);
416
	}
417

Robin's avatar
Robin committed
418
	/**
419
	 * Set password
420
	 *
421
422
423
424
	 * @param string $uid The username
	 * @param string $password The new password
	 * @param string $recoveryPassword for the encryption app to reset encryption keys
	 * @return bool
Jakob Sack's avatar
Jakob Sack committed
425
426
	 *
	 * Change the password of a user
427
	 */
428
	public static function setPassword($uid, $password, $recoveryPassword = null) {
429
		$user = \OC::$server->getUserManager()->get($uid);
430
431
432
		if ($user) {
			return $user->setPassword($password, $recoveryPassword);
		} else {
433
434
			return false;
		}
435
	}
436

437
	/**
438
	 * Check whether user can change his avatar
439
	 *
440
441
442
443
444
445
	 * @param string $uid The username
	 * @return bool
	 *
	 * Check whether a specified user can change his avatar
	 */
	public static function canUserChangeAvatar($uid) {
446
		$user = \OC::$server->getUserManager()->get($uid);
447
448
449
450
451
452
453
		if ($user) {
			return $user->canChangeAvatar();
		} else {
			return false;
		}
	}

454
	/**
455
	 * Check whether user can change his password
456
	 *
457
458
	 * @param string $uid The username
	 * @return bool
459
460
461
462
	 *
	 * Check whether a specified user can change his password
	 */
	public static function canUserChangePassword($uid) {
463
		$user = \OC::$server->getUserManager()->get($uid);
464
465
466
467
		if ($user) {
			return $user->canChangePassword();
		} else {
			return false;
468
469
		}
	}
Bart Visscher's avatar
Bart Visscher committed
470

471
	/**
472
	 * Check whether user can change his display name
473
	 *
474
475
	 * @param string $uid The username
	 * @return bool
476
477
478
479
	 *
	 * Check whether a specified user can change his display name
	 */
	public static function canUserChangeDisplayName($uid) {
480
		$user = \OC::$server->getUserManager()->get($uid);
481
482
483
484
		if ($user) {
			return $user->canChangeDisplayName();
		} else {
			return false;
485
486
		}
	}
487

Robin's avatar
Robin committed
488
	/**
489
	 * Check if the password is correct
490
	 *
491
492
	 * @param string $uid The username
	 * @param string $password The password
493
	 * @return string|false user id a string on success, false otherwise
Jakob Sack's avatar
Jakob Sack committed
494
495
	 *
	 * Check if the password is correct without logging in the user
496
	 * returns the user id or false
497
	 */
498
	public static function checkPassword($uid, $password) {
499
		$manager = \OC::$server->getUserManager();
500
501
		$username = $manager->checkPassword($uid, $password);
		if ($username !== false) {
502
			return $username->getUID();
503
		}
504
		return false;
505
	}
Aldo "xoen" Giambelluca's avatar
Aldo "xoen" Giambelluca committed
506

Georg Ehrke's avatar
Georg Ehrke committed
507
	/**
Robin Appelman's avatar
Robin Appelman committed
508
	 * @param string $uid The username
509
	 * @return string
Georg Ehrke's avatar
Georg Ehrke committed
510
	 *
Björn Schießle's avatar
Björn Schießle committed
511
	 * returns the path to the users home directory
512
	 * @deprecated Use \OC::$server->getUserManager->getHome()
Georg Ehrke's avatar
Georg Ehrke committed
513
	 */
Thomas Mueller's avatar
Thomas Mueller committed
514
	public static function getHome($uid) {
515
		$user = \OC::$server->getUserManager()->get($uid);
516
517
518
		if ($user) {
			return $user->getHome();
		} else {
Robin Appelman's avatar
Robin Appelman committed
519
			return OC_Config::getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid;
Georg Ehrke's avatar
Georg Ehrke committed
520
521
522
		}
	}

523
	/**
524
	 * Get a list of all users
525
	 *
526
	 * @return array an array of all uids
Jakob Sack's avatar
Jakob Sack committed
527
528
	 *
	 * Get a list of all users.
Lukas Reschke's avatar
Lukas Reschke committed
529
	 * @param string $search
530
531
	 * @param integer $limit
	 * @param integer $offset
532
	 */
533
	public static function getUsers($search = '', $limit = null, $offset = null) {
534
		$users = \OC::$server->getUserManager()->search($search, $limit, $offset);
535
536
537
		$uids = array();
		foreach ($users as $user) {
			$uids[] = $user->getUID();
538
		}
539
		return $uids;
540
541
	}

Arthur Schiwon's avatar
Typo    
Arthur Schiwon committed
542
	/**
543
	 * Get a list of all users display name
544
	 *
545
546
547
548
	 * @param string $search
	 * @param int $limit
	 * @param int $offset
	 * @return array associative array with all display names (value) and corresponding uids (key)
Arthur Schiwon's avatar
Typo    
Arthur Schiwon committed
549
550
	 *
	 * Get a list of all display names and user ids.
551
	 * @deprecated Use \OC::$server->getUserManager->searchDisplayName($search, $limit, $offset) instead.
Arthur Schiwon's avatar
Typo    
Arthur Schiwon committed
552
553
554
	 */
	public static function getDisplayNames($search = '', $limit = null, $offset = null) {
		$displayNames = array();
555
		$users = \OC::$server->getUserManager()->searchDisplayName($search, $limit, $offset);
556
557
		foreach ($users as $user) {
			$displayNames[$user->getUID()] = $user->getDisplayName();
Arthur Schiwon's avatar
Typo    
Arthur Schiwon committed
558
559
		}
		return $displayNames;
Björn Schießle's avatar
Björn Schießle committed
560
	}
561
562

	/**
563
	 * check if a user exists
564
	 *
565
566
567
	 * @param string $uid the username
	 * @return boolean
	 */
568
	public static function userExists($uid) {
569
		return \OC::$server->getUserManager()->userExists($uid);
570
	}
Bart Visscher's avatar
Bart Visscher committed
571

572
573
	/**
	 * disables a user
574
	 *
575
	 * @param string $uid the user to disable
576
	 */
577
	public static function disableUser($uid) {
578
		$user = \OC::$server->getUserManager()->get($uid);
579
580
		if ($user) {
			$user->setEnabled(false);
581
		}
582
	}
Bart Visscher's avatar
Bart Visscher committed
583

584
585
	/**
	 * enable a user
586
	 *
587
	 * @param string $uid
588
	 */
589
	public static function enableUser($uid) {
590
		$user = \OC::$server->getUserManager()->get($uid);
591
592
		if ($user) {
			$user->setEnabled(true);
593
		}
594
	}
Bart Visscher's avatar
Bart Visscher committed
595

596
597
	/**
	 * checks if a user is enabled
598
	 *
599
	 * @param string $uid
600
601
	 * @return bool
	 */
602
	public static function isEnabled($uid) {
603
		$user = \OC::$server->getUserManager()->get($uid);
604
605
		if ($user) {
			return $user->isEnabled();
606
		} else {
607
			return false;
608
		}
609
	}
Bartek Przybylski's avatar
Bartek Przybylski committed
610
611

	/**
612
	 * Set cookie value to use in next page load
613
	 *
Bartek Przybylski's avatar
Bartek Przybylski committed
614
	 * @param string $username username to be set
615
	 * @param string $token
Bartek Przybylski's avatar
Bartek Przybylski committed
616
	 */
Thomas Mueller's avatar
Thomas Mueller committed
617
	public static function setMagicInCookie($username, $token) {
618
		self::getUserSession()->setMagicInCookie($username, $token);
Bartek Przybylski's avatar
Bartek Przybylski committed
619
620
621
	}

	/**
622
	 * Remove cookie for "remember username"
Bartek Przybylski's avatar
Bartek Przybylski committed
623
	 */
Thomas Mueller's avatar
Thomas Mueller committed
624
	public static function unsetMagicInCookie() {
625
		self::getUserSession()->unsetMagicInCookie();
Bartek Przybylski's avatar
Bartek Przybylski committed
626
	}
627
628

	/**
629
	 * Returns the first active backend from self::$_usedBackends.
630
	 *
631
	 * @return OCP\Authentication\IApacheBackend|null if no backend active, otherwise OCP\Authentication\IApacheBackend
632
633
634
635
636
637
638
639
640
641
642
643
	 */
	private static function findFirstActiveUsedBackend() {
		foreach (self::$_usedBackends as $backend) {
			if ($backend instanceof OCP\Authentication\IApacheBackend) {
				if ($backend->isSessionActive()) {
					return $backend;
				}
			}
		}

		return null;
	}
Robin's avatar
Robin committed
644
}