2010-04-22 17:03:54 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Aldo "xoen" Giambelluca <xoen@xoen.org>
|
|
|
|
* @author Andreas Fischer <bantu@owncloud.com>
|
|
|
|
* @author Arthur Schiwon <blizzz@owncloud.com>
|
|
|
|
* @author Bartek Przybylski <bart.p.pl@gmail.com>
|
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @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>
|
|
|
|
* @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>
|
2011-04-15 15:14:02 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
2011-04-15 15:14:02 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* 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.
|
2011-04-15 15:14:02 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2011-04-15 15:14:02 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 10:44:34 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2011-04-15 15:14:02 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* 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/>
|
2011-04-15 15:14:02 +00:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class provides wrapper methods for user management. Multiple backends are
|
|
|
|
* supported. User management operations are delegated to the configured backend for
|
|
|
|
* execution.
|
|
|
|
*
|
|
|
|
* 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()
|
|
|
|
*/
|
2011-07-29 19:36:03 +00:00
|
|
|
class OC_User {
|
2014-04-21 13:44:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \OC\User\Session
|
|
|
|
*/
|
2013-07-09 22:06:22 +00:00
|
|
|
public static function getUserSession() {
|
2013-09-20 10:45:56 +00:00
|
|
|
return OC::$server->getUserSession();
|
2013-05-28 22:32:10 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 22:49:06 +00:00
|
|
|
/**
|
|
|
|
* @return \OC\User\Manager
|
2014-12-04 13:15:55 +00:00
|
|
|
* @deprecated Use \OC::$server->getUserManager()
|
2013-05-31 22:49:06 +00:00
|
|
|
*/
|
2013-07-09 22:06:22 +00:00
|
|
|
public static function getManager() {
|
2013-09-20 10:45:56 +00:00
|
|
|
return OC::$server->getUserManager();
|
2013-05-28 22:32:10 +00:00
|
|
|
}
|
2011-04-15 15:14:02 +00:00
|
|
|
|
|
|
|
private static $_backends = array();
|
2010-07-21 15:53:51 +00:00
|
|
|
|
2013-05-28 22:32:10 +00:00
|
|
|
private static $_usedBackends = array();
|
|
|
|
|
|
|
|
private static $_setupedBackends = array();
|
|
|
|
|
2013-11-22 12:55:38 +00:00
|
|
|
// bool, stores if a user want to access a resource anonymously, e.g if he opens a public link
|
|
|
|
private static $incognitoMode = false;
|
|
|
|
|
2010-07-21 15:53:51 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* registers backend
|
2015-02-02 15:09:30 +00:00
|
|
|
*
|
2013-05-28 22:32:10 +00:00
|
|
|
* @param string $backend name of the backend
|
2014-08-14 10:22:34 +00:00
|
|
|
* @deprecated Add classes by calling OC_User::useBackend() with a class instance instead
|
2013-05-28 22:32:10 +00:00
|
|
|
* @return bool
|
2010-07-22 21:42:18 +00:00
|
|
|
*
|
2011-04-15 15:14:02 +00:00
|
|
|
* Makes a list of backends that can be used by other modules
|
2010-07-22 21:42:18 +00:00
|
|
|
*/
|
2013-05-28 22:32:10 +00:00
|
|
|
public static function registerBackend($backend) {
|
2012-07-19 14:31:55 +00:00
|
|
|
self::$_backends[] = $backend;
|
2011-04-15 15:14:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* gets available backends
|
2015-02-02 15:09:30 +00:00
|
|
|
*
|
2013-05-28 22:32:10 +00:00
|
|
|
* @deprecated
|
2014-05-11 17:13:51 +00:00
|
|
|
* @return array an array of backends
|
2011-04-15 15:14:02 +00:00
|
|
|
*
|
|
|
|
* Returns the names of all backends.
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function getBackends() {
|
2011-04-15 15:14:02 +00:00
|
|
|
return self::$_backends;
|
|
|
|
}
|
2011-06-23 13:24:09 +00:00
|
|
|
|
2011-06-21 17:28:46 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* gets used backends
|
2015-02-02 15:09:30 +00:00
|
|
|
*
|
2013-05-28 22:32:10 +00:00
|
|
|
* @deprecated
|
2014-05-11 17:13:51 +00:00
|
|
|
* @return array an array of backends
|
2011-06-21 17:28:46 +00:00
|
|
|
*
|
|
|
|
* Returns the names of all used backends.
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function getUsedBackends() {
|
2011-06-21 17:28:46 +00:00
|
|
|
return array_keys(self::$_usedBackends);
|
|
|
|
}
|
2011-04-15 15:14:02 +00:00
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Adds the backend to the list of used backends
|
2015-02-02 15:09:30 +00:00
|
|
|
*
|
2014-05-11 21:32:26 +00:00
|
|
|
* @param string|OC_User_Interface $backend default: database The backend to use for user management
|
2013-05-28 22:32:10 +00:00
|
|
|
* @return bool
|
2011-04-15 15:14:02 +00:00
|
|
|
*
|
|
|
|
* Set the User Authentication Module
|
|
|
|
*/
|
2013-05-28 22:32:10 +00:00
|
|
|
public static function useBackend($backend = 'database') {
|
|
|
|
if ($backend instanceof OC_User_Interface) {
|
|
|
|
self::$_usedBackends[get_class($backend)] = $backend;
|
|
|
|
self::getManager()->registerBackend($backend);
|
2012-12-11 13:56:04 +00:00
|
|
|
} else {
|
2012-07-19 14:31:55 +00:00
|
|
|
// You'll never know what happens
|
2013-05-28 22:32:10 +00:00
|
|
|
if (null === $backend OR !is_string($backend)) {
|
2012-07-19 14:31:55 +00:00
|
|
|
$backend = 'database';
|
|
|
|
}
|
2010-07-21 15:53:51 +00:00
|
|
|
|
2012-07-19 14:31:55 +00:00
|
|
|
// Load backend
|
2013-05-28 22:32:10 +00:00
|
|
|
switch ($backend) {
|
2012-07-19 14:31:55 +00:00
|
|
|
case 'database':
|
|
|
|
case 'mysql':
|
|
|
|
case 'sqlite':
|
2013-05-28 22:32:10 +00:00
|
|
|
OC_Log::write('core', 'Adding user backend ' . $backend . '.', OC_Log::DEBUG);
|
2012-07-19 14:31:55 +00:00
|
|
|
self::$_usedBackends[$backend] = new OC_User_Database();
|
2013-05-28 22:32:10 +00:00
|
|
|
self::getManager()->registerBackend(self::$_usedBackends[$backend]);
|
2012-07-19 14:31:55 +00:00
|
|
|
break;
|
|
|
|
default:
|
2013-05-28 22:32:10 +00:00
|
|
|
OC_Log::write('core', 'Adding default user backend ' . $backend . '.', OC_Log::DEBUG);
|
2012-07-19 14:31:55 +00:00
|
|
|
$className = 'OC_USER_' . strToUpper($backend);
|
|
|
|
self::$_usedBackends[$backend] = new $className();
|
2013-05-28 22:32:10 +00:00
|
|
|
self::getManager()->registerBackend(self::$_usedBackends[$backend]);
|
2012-07-19 14:31:55 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-07-21 15:53:51 +00:00
|
|
|
}
|
2012-12-11 13:56:04 +00:00
|
|
|
return true;
|
2010-07-21 15:53:51 +00:00
|
|
|
}
|
2010-07-19 16:52:49 +00:00
|
|
|
|
2012-07-23 20:31:48 +00:00
|
|
|
/**
|
|
|
|
* remove all used backends
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function clearBackends() {
|
2013-05-28 22:32:10 +00:00
|
|
|
self::$_usedBackends = array();
|
|
|
|
self::getManager()->clearBackends();
|
2012-07-23 20:31:48 +00:00
|
|
|
}
|
|
|
|
|
2012-09-01 00:48:54 +00:00
|
|
|
/**
|
|
|
|
* setup the configured backends in config.php
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function setupBackends() {
|
2013-09-26 11:34:47 +00:00
|
|
|
OC_App::loadApps(array('prelogin'));
|
2013-05-28 22:32:10 +00:00
|
|
|
$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) {
|
2012-12-11 13:56:04 +00:00
|
|
|
// make a reflection object
|
|
|
|
$reflectionObj = new ReflectionClass($class);
|
2012-09-01 00:48:54 +00:00
|
|
|
|
2012-12-11 13:56:04 +00:00
|
|
|
// use Reflection to create a new instance, using the $args
|
|
|
|
$backend = $reflectionObj->newInstanceArgs($arguments);
|
|
|
|
self::useBackend($backend);
|
2013-06-18 18:47:47 +00:00
|
|
|
self::$_setupedBackends[] = $i;
|
2012-12-11 13:56:04 +00:00
|
|
|
} else {
|
2013-05-28 22:32:10 +00:00
|
|
|
OC_Log::write('core', 'User backend ' . $class . ' already initialized.', OC_Log::DEBUG);
|
2012-12-11 13:56:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-05-28 22:32:10 +00:00
|
|
|
OC_Log::write('core', 'User backend ' . $class . ' not found.', OC_Log::ERROR);
|
2012-09-01 00:48:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-22 17:03:54 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Create a new user
|
2015-02-02 15:09:30 +00:00
|
|
|
*
|
2013-05-28 22:32:10 +00:00
|
|
|
* @param string $uid The username of the user to create
|
|
|
|
* @param string $password The password of the new user
|
|
|
|
* @throws Exception
|
|
|
|
* @return bool true/false
|
2011-04-18 08:41:01 +00:00
|
|
|
*
|
2011-07-29 19:36:03 +00:00
|
|
|
* Creates a new user. Basic checking of username is done in OC_User
|
2011-04-18 09:39:29 +00:00
|
|
|
* itself, not in its subclasses.
|
|
|
|
*
|
|
|
|
* Allowed characters in the username are: "a-z", "A-Z", "0-9" and "_.@-"
|
2015-02-21 19:51:50 +00:00
|
|
|
* @deprecated Use \OC::$server->getUserManager()->createUser($uid, $password)
|
2010-07-22 21:42:18 +00:00
|
|
|
*/
|
2013-05-28 22:32:10 +00:00
|
|
|
public static function createUser($uid, $password) {
|
2013-06-11 11:48:17 +00:00
|
|
|
return self::getManager()->createUser($uid, $password);
|
2010-07-21 15:53:51 +00:00
|
|
|
}
|
2010-07-19 16:52:49 +00:00
|
|
|
|
2011-04-16 23:04:23 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* delete a user
|
2015-02-02 15:09:30 +00:00
|
|
|
*
|
2013-05-28 22:32:10 +00:00
|
|
|
* @param string $uid The username of the user to delete
|
|
|
|
* @return bool
|
2011-04-18 08:41:01 +00:00
|
|
|
*
|
|
|
|
* Deletes a user
|
2015-04-13 14:34:10 +00:00
|
|
|
* @deprecated Use \OC::$server->getUserManager()->get() and then run delete() on the return
|
2011-04-16 23:04:23 +00:00
|
|
|
*/
|
2013-05-28 22:32:10 +00:00
|
|
|
public static function deleteUser($uid) {
|
|
|
|
$user = self::getManager()->get($uid);
|
|
|
|
if ($user) {
|
2014-12-04 13:15:55 +00:00
|
|
|
return $user->delete();
|
2013-10-07 19:24:25 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
2011-04-18 09:39:29 +00:00
|
|
|
}
|
2011-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
2010-04-22 17:03:54 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Try to login a user
|
2015-02-02 15:09:30 +00:00
|
|
|
*
|
2014-10-22 11:36:57 +00:00
|
|
|
* @param string $loginname The login name of the user to log in
|
2014-04-21 13:44:54 +00:00
|
|
|
* @param string $password The password of the user
|
2014-02-06 15:30:58 +00:00
|
|
|
* @return boolean|null
|
2011-04-18 08:41:01 +00:00
|
|
|
*
|
2012-03-13 15:00:53 +00:00
|
|
|
* Log in a user and regenerate a new session - if the password is ok
|
2010-07-22 21:42:18 +00:00
|
|
|
*/
|
2014-10-22 11:36:57 +00:00
|
|
|
public static function login($loginname, $password) {
|
2014-02-21 07:11:07 +00:00
|
|
|
session_regenerate_id(true);
|
2014-10-22 11:36:57 +00:00
|
|
|
$result = self::getUserSession()->login($loginname, $password);
|
2014-08-28 14:59:56 +00:00
|
|
|
if ($result) {
|
2014-10-22 11:36:57 +00:00
|
|
|
//we need to pass the user name, which may differ from login name
|
2014-10-22 11:28:08 +00:00
|
|
|
OC_Util::setupFS(self::getUserSession()->getUser()->getUID());
|
2014-08-28 14:59:56 +00:00
|
|
|
}
|
|
|
|
return $result;
|
2011-09-18 13:05:53 +00:00
|
|
|
}
|
|
|
|
|
2014-05-21 16:03:37 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2013-10-01 11:25:58 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Try to login a user, assuming authentication
|
2013-10-02 13:04:42 +00:00
|
|
|
* has already happened (e.g. via Single Sign On).
|
2013-10-01 11:25:58 +00:00
|
|
|
*
|
|
|
|
* Log in a user and regenerate a new session.
|
2013-10-02 13:04:42 +00:00
|
|
|
*
|
2013-10-02 13:11:49 +00:00
|
|
|
* @param \OCP\Authentication\IApacheBackend $backend
|
2013-10-02 13:04:42 +00:00
|
|
|
* @return bool
|
2013-10-01 11:25:58 +00:00
|
|
|
*/
|
2013-10-02 13:11:49 +00:00
|
|
|
public static function loginWithApache(\OCP\Authentication\IApacheBackend $backend) {
|
2013-10-01 11:25:58 +00:00
|
|
|
|
2013-10-01 20:56:47 +00:00
|
|
|
$uid = $backend->getCurrentUserId();
|
2013-10-01 11:25:58 +00:00
|
|
|
$run = true;
|
2015-02-02 15:09:30 +00:00
|
|
|
OC_Hook::emit("OC_User", "pre_login", array("run" => &$run, "uid" => $uid));
|
2013-10-01 11:25:58 +00:00
|
|
|
|
2015-02-02 15:09:30 +00:00
|
|
|
if ($uid) {
|
2013-10-01 11:25:58 +00:00
|
|
|
self::setUserId($uid);
|
|
|
|
self::setDisplayName($uid);
|
2014-01-09 09:28:24 +00:00
|
|
|
self::getUserSession()->setLoginName($uid);
|
|
|
|
|
2015-02-02 15:09:30 +00:00
|
|
|
OC_Hook::emit("OC_User", "post_login", array("uid" => $uid, 'password' => ''));
|
2013-10-01 11:25:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Verify with Apache whether user is authenticated.
|
2013-10-01 11:25:58 +00:00
|
|
|
*
|
2013-10-02 13:04:42 +00:00
|
|
|
* @return boolean|null
|
|
|
|
* true: authenticated
|
|
|
|
* false: not authenticated
|
|
|
|
* null: not handled / no backend available
|
2013-10-01 11:25:58 +00:00
|
|
|
*/
|
2013-10-01 22:55:35 +00:00
|
|
|
public static function handleApacheAuth() {
|
2013-10-02 13:31:46 +00:00
|
|
|
$backend = self::findFirstActiveUsedBackend();
|
|
|
|
if ($backend) {
|
|
|
|
OC_App::loadApps();
|
2013-10-01 11:25:58 +00:00
|
|
|
|
2013-10-02 13:31:46 +00:00
|
|
|
//setup extra user backends
|
|
|
|
self::setupBackends();
|
|
|
|
self::unsetMagicInCookie();
|
2013-10-01 11:25:58 +00:00
|
|
|
|
2013-10-02 13:31:46 +00:00
|
|
|
return self::loginWithApache($backend);
|
2013-10-01 11:25:58 +00:00
|
|
|
}
|
|
|
|
|
2013-10-01 22:55:35 +00:00
|
|
|
return null;
|
2013-10-01 11:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-18 13:05:53 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Sets user id for session and triggers emit
|
2011-09-18 13:05:53 +00:00
|
|
|
*/
|
|
|
|
public static function setUserId($uid) {
|
2015-02-02 15:09:30 +00:00
|
|
|
$userSession = \OC::$server->getUserSession();
|
|
|
|
$userManager = \OC::$server->getUserManager();
|
|
|
|
if ($user = $userManager->get($uid)) {
|
|
|
|
$userSession->setUser($user);
|
|
|
|
} else {
|
|
|
|
\OC::$server->getSession()->set('user_id', $uid);
|
|
|
|
}
|
2010-07-21 15:53:51 +00:00
|
|
|
}
|
2010-07-19 16:52:49 +00:00
|
|
|
|
2013-01-29 19:42:21 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Sets user display name for session
|
2015-02-02 15:09:30 +00:00
|
|
|
*
|
2014-02-06 15:30:58 +00:00
|
|
|
* @param string $uid
|
2014-04-21 13:44:54 +00:00
|
|
|
* @param null $displayName
|
|
|
|
* @return bool Whether the display name could get set
|
2013-01-29 19:42:21 +00:00
|
|
|
*/
|
2013-01-28 13:09:11 +00:00
|
|
|
public static function setDisplayName($uid, $displayName = null) {
|
2013-05-28 22:32:10 +00:00
|
|
|
if (is_null($displayName)) {
|
|
|
|
$displayName = $uid;
|
2013-01-28 13:09:11 +00:00
|
|
|
}
|
2013-05-28 22:32:10 +00:00
|
|
|
$user = self::getManager()->get($uid);
|
|
|
|
if ($user) {
|
|
|
|
return $user->setDisplayName($displayName);
|
|
|
|
} else {
|
|
|
|
return false;
|
2013-01-29 21:33:46 +00:00
|
|
|
}
|
2013-01-24 12:07:59 +00:00
|
|
|
}
|
2013-01-29 19:42:21 +00:00
|
|
|
|
2011-03-01 22:20:16 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Logs the current user out and kills all the session data
|
2011-04-18 08:41:01 +00:00
|
|
|
*
|
|
|
|
* Logout, destroys session
|
2011-03-01 22:20:16 +00:00
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function logout() {
|
2013-05-28 22:32:10 +00:00
|
|
|
self::getUserSession()->logout();
|
2011-03-01 22:20:16 +00:00
|
|
|
}
|
|
|
|
|
2014-12-17 19:12:14 +00:00
|
|
|
/**
|
|
|
|
* Tries to login the user with HTTP Basic Authentication
|
|
|
|
*/
|
|
|
|
public static function tryBasicAuthLogin() {
|
2015-02-02 15:09:30 +00:00
|
|
|
if (!empty($_SERVER['PHP_AUTH_USER']) && !empty($_SERVER['PHP_AUTH_PW'])) {
|
2014-12-17 19:12:14 +00:00
|
|
|
\OC_User::login($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-22 17:03:54 +00:00
|
|
|
/**
|
2014-09-18 14:02:18 +00:00
|
|
|
* Check if the user is logged in, considers also the HTTP basic credentials
|
2015-02-02 15:09:30 +00:00
|
|
|
*
|
2014-05-11 17:05:28 +00:00
|
|
|
* @return bool
|
2010-07-22 21:42:18 +00:00
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function isLoggedIn() {
|
2014-07-16 17:40:22 +00:00
|
|
|
if (\OC::$server->getSession()->get('user_id') !== null && self::$incognitoMode === false) {
|
|
|
|
return self::userExists(\OC::$server->getSession()->get('user_id'));
|
2011-06-21 17:28:46 +00:00
|
|
|
}
|
2014-09-18 14:02:18 +00:00
|
|
|
|
2012-07-05 13:55:46 +00:00
|
|
|
return false;
|
2010-07-21 15:53:51 +00:00
|
|
|
}
|
2010-07-19 16:52:49 +00:00
|
|
|
|
2013-11-22 12:55:38 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* set incognito mode, e.g. if a user wants to open a public link
|
2015-02-02 15:09:30 +00:00
|
|
|
*
|
2013-11-22 12:55:38 +00:00
|
|
|
* @param bool $status
|
|
|
|
*/
|
|
|
|
public static function setIncognitoMode($status) {
|
|
|
|
self::$incognitoMode = $status;
|
2014-12-17 20:53:43 +00:00
|
|
|
}
|
2013-11-22 12:55:38 +00:00
|
|
|
|
2014-12-17 20:53:43 +00:00
|
|
|
/**
|
|
|
|
* get incognito mode status
|
2015-02-02 15:09:30 +00:00
|
|
|
*
|
2014-12-17 20:53:43 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isIncognitoMode() {
|
|
|
|
return self::$incognitoMode;
|
2013-11-22 12:55:38 +00:00
|
|
|
}
|
|
|
|
|
2013-10-01 11:25:58 +00:00
|
|
|
/**
|
2013-10-02 13:04:42 +00:00
|
|
|
* Supplies an attribute to the logout hyperlink. The default behaviour
|
2013-10-01 11:25:58 +00:00
|
|
|
* is to return an href with '?logout=true' appended. However, it can
|
|
|
|
* supply any attribute(s) which are valid for <a>.
|
|
|
|
*
|
2013-10-02 13:04:42 +00:00
|
|
|
* @return string with one or more HTML attributes.
|
2013-10-01 11:25:58 +00:00
|
|
|
*/
|
|
|
|
public static function getLogoutAttribute() {
|
2013-10-02 13:31:46 +00:00
|
|
|
$backend = self::findFirstActiveUsedBackend();
|
|
|
|
if ($backend) {
|
|
|
|
return $backend->getLogoutAttribute();
|
2013-10-01 11:25:58 +00:00
|
|
|
}
|
|
|
|
|
2015-02-13 10:35:12 +00:00
|
|
|
return 'href="' . link_to('', 'index.php') . '?logout=true&requesttoken=' . urlencode(OC_Util::callRegister()) . '"';
|
2013-10-01 11:25:58 +00:00
|
|
|
}
|
|
|
|
|
2013-01-14 18:45:17 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Check if the user is an admin user
|
2015-02-02 15:09:30 +00:00
|
|
|
*
|
2013-05-28 22:32:10 +00:00
|
|
|
* @param string $uid uid of the admin
|
|
|
|
* @return bool
|
2013-01-14 18:45:17 +00:00
|
|
|
*/
|
|
|
|
public static function isAdminUser($uid) {
|
2013-11-27 15:52:30 +00:00
|
|
|
if (OC_Group::inGroup($uid, 'admin') && self::$incognitoMode === false) {
|
2013-01-14 18:45:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-22 10:50:57 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* get the user id of the user currently logged in.
|
2015-02-02 15:09:30 +00:00
|
|
|
*
|
2011-06-22 10:50:57 +00:00
|
|
|
* @return string uid or false
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function getUser() {
|
2014-07-16 17:40:22 +00:00
|
|
|
$uid = \OC::$server->getSession() ? \OC::$server->getSession()->get('user_id') : null;
|
2013-11-22 12:55:38 +00:00
|
|
|
if (!is_null($uid) && self::$incognitoMode === false) {
|
2013-05-28 22:47:55 +00:00
|
|
|
return $uid;
|
2013-05-28 22:32:10 +00:00
|
|
|
} else {
|
2011-06-22 10:50:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 19:42:21 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* get the display name of the user currently logged in.
|
2015-02-02 15:09:30 +00:00
|
|
|
*
|
2013-06-03 11:15:42 +00:00
|
|
|
* @param string $uid
|
2013-05-31 16:18:13 +00:00
|
|
|
* @return string uid or false
|
2013-01-29 19:42:21 +00:00
|
|
|
*/
|
2013-06-03 11:15:42 +00:00
|
|
|
public static function getDisplayName($uid = null) {
|
|
|
|
if ($uid) {
|
|
|
|
$user = self::getManager()->get($uid);
|
|
|
|
if ($user) {
|
|
|
|
return $user->getDisplayName();
|
|
|
|
} else {
|
|
|
|
return $uid;
|
|
|
|
}
|
2013-05-28 22:32:10 +00:00
|
|
|
} else {
|
|
|
|
$user = self::getUserSession()->getUser();
|
|
|
|
if ($user) {
|
|
|
|
return $user->getDisplayName();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-29 19:42:21 +00:00
|
|
|
}
|
2013-01-24 12:07:59 +00:00
|
|
|
}
|
2013-01-29 19:42:21 +00:00
|
|
|
|
2010-04-22 17:03:54 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Autogenerate a password
|
2015-02-02 15:09:30 +00:00
|
|
|
*
|
2013-05-28 22:32:10 +00:00
|
|
|
* @return string
|
2011-04-18 08:41:01 +00:00
|
|
|
*
|
|
|
|
* generates a password
|
2010-07-22 21:42:18 +00:00
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function generatePassword() {
|
2014-09-03 15:46:48 +00:00
|
|
|
return \OC::$server->getSecureRandom()->getMediumStrengthGenerator()->generate(30);
|
2010-07-21 15:53:51 +00:00
|
|
|
}
|
2010-07-19 16:52:49 +00:00
|
|
|
|
2010-04-22 17:03:54 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Set password
|
2015-02-02 15:09:30 +00:00
|
|
|
*
|
2013-05-28 22:32:10 +00:00
|
|
|
* @param string $uid The username
|
|
|
|
* @param string $password The new password
|
|
|
|
* @param string $recoveryPassword for the encryption app to reset encryption keys
|
|
|
|
* @return bool
|
2011-04-18 08:41:01 +00:00
|
|
|
*
|
|
|
|
* Change the password of a user
|
2010-07-22 21:42:18 +00:00
|
|
|
*/
|
2013-05-28 22:32:10 +00:00
|
|
|
public static function setPassword($uid, $password, $recoveryPassword = null) {
|
|
|
|
$user = self::getManager()->get($uid);
|
|
|
|
if ($user) {
|
|
|
|
return $user->setPassword($password, $recoveryPassword);
|
|
|
|
} else {
|
2011-04-18 09:39:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-07-21 15:53:51 +00:00
|
|
|
}
|
2010-07-19 16:52:49 +00:00
|
|
|
|
2013-11-22 12:24:11 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Check whether user can change his avatar
|
2015-02-02 15:09:30 +00:00
|
|
|
*
|
2013-11-22 12:24:11 +00:00
|
|
|
* @param string $uid The username
|
|
|
|
* @return bool
|
|
|
|
*
|
|
|
|
* Check whether a specified user can change his avatar
|
|
|
|
*/
|
|
|
|
public static function canUserChangeAvatar($uid) {
|
|
|
|
$user = self::getManager()->get($uid);
|
|
|
|
if ($user) {
|
|
|
|
return $user->canChangeAvatar();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-05 13:58:35 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Check whether user can change his password
|
2015-02-02 15:09:30 +00:00
|
|
|
*
|
2013-05-28 22:32:10 +00:00
|
|
|
* @param string $uid The username
|
|
|
|
* @return bool
|
2013-02-05 13:58:35 +00:00
|
|
|
*
|
|
|
|
* Check whether a specified user can change his password
|
|
|
|
*/
|
|
|
|
public static function canUserChangePassword($uid) {
|
2013-05-28 22:32:10 +00:00
|
|
|
$user = self::getManager()->get($uid);
|
|
|
|
if ($user) {
|
|
|
|
return $user->canChangePassword();
|
|
|
|
} else {
|
|
|
|
return false;
|
2013-02-05 13:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-22 16:21:57 +00:00
|
|
|
|
2013-02-06 10:38:03 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Check whether user can change his display name
|
2015-02-02 15:09:30 +00:00
|
|
|
*
|
2013-05-28 22:32:10 +00:00
|
|
|
* @param string $uid The username
|
|
|
|
* @return bool
|
2013-02-06 10:38:03 +00:00
|
|
|
*
|
|
|
|
* Check whether a specified user can change his display name
|
|
|
|
*/
|
|
|
|
public static function canUserChangeDisplayName($uid) {
|
2013-05-28 22:32:10 +00:00
|
|
|
$user = self::getManager()->get($uid);
|
|
|
|
if ($user) {
|
|
|
|
return $user->canChangeDisplayName();
|
|
|
|
} else {
|
|
|
|
return false;
|
2013-02-06 10:38:03 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-05 13:58:35 +00:00
|
|
|
|
2010-04-22 17:03:54 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Check if the password is correct
|
2015-02-02 15:09:30 +00:00
|
|
|
*
|
2013-05-28 22:32:10 +00:00
|
|
|
* @param string $uid The username
|
|
|
|
* @param string $password The password
|
2014-02-06 15:30:58 +00:00
|
|
|
* @return string|false user id a string on success, false otherwise
|
2011-04-18 08:41:01 +00:00
|
|
|
*
|
|
|
|
* Check if the password is correct without logging in the user
|
2012-05-16 22:57:43 +00:00
|
|
|
* returns the user id or false
|
2010-07-22 21:42:18 +00:00
|
|
|
*/
|
2013-05-28 22:32:10 +00:00
|
|
|
public static function checkPassword($uid, $password) {
|
2013-09-16 12:15:35 +00:00
|
|
|
$manager = self::getManager();
|
|
|
|
$username = $manager->checkPassword($uid, $password);
|
|
|
|
if ($username !== false) {
|
2013-09-25 13:03:22 +00:00
|
|
|
return $username->getUID();
|
2011-06-21 17:28:46 +00:00
|
|
|
}
|
2013-09-16 12:15:35 +00:00
|
|
|
return false;
|
2010-07-21 15:53:51 +00:00
|
|
|
}
|
2010-07-15 17:10:20 +00:00
|
|
|
|
2012-08-26 14:24:25 +00:00
|
|
|
/**
|
2013-01-28 18:08:03 +00:00
|
|
|
* @param string $uid The username
|
2013-05-28 22:32:10 +00:00
|
|
|
* @return string
|
2012-08-26 14:24:25 +00:00
|
|
|
*
|
2012-10-27 13:23:35 +00:00
|
|
|
* returns the path to the users home directory
|
2014-12-04 13:15:55 +00:00
|
|
|
* @deprecated Use \OC::$server->getUserManager->getHome()
|
2012-08-26 14:24:25 +00:00
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function getHome($uid) {
|
2013-05-28 22:32:10 +00:00
|
|
|
$user = self::getManager()->get($uid);
|
|
|
|
if ($user) {
|
|
|
|
return $user->getHome();
|
|
|
|
} else {
|
2013-05-31 20:51:16 +00:00
|
|
|
return OC_Config::getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid;
|
2012-08-26 14:24:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-15 16:24:14 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Get a list of all users
|
2015-02-02 15:09:30 +00:00
|
|
|
*
|
2014-05-11 17:13:51 +00:00
|
|
|
* @return array an array of all uids
|
2011-04-18 08:41:01 +00:00
|
|
|
*
|
|
|
|
* Get a list of all users.
|
2014-04-21 13:44:54 +00:00
|
|
|
* @param string $search
|
2014-02-19 08:31:54 +00:00
|
|
|
* @param integer $limit
|
|
|
|
* @param integer $offset
|
2010-09-15 16:24:14 +00:00
|
|
|
*/
|
2012-08-24 22:05:07 +00:00
|
|
|
public static function getUsers($search = '', $limit = null, $offset = null) {
|
2013-05-28 22:32:10 +00:00
|
|
|
$users = self::getManager()->search($search, $limit, $offset);
|
|
|
|
$uids = array();
|
|
|
|
foreach ($users as $user) {
|
|
|
|
$uids[] = $user->getUID();
|
2011-06-21 17:28:46 +00:00
|
|
|
}
|
2013-05-28 22:32:10 +00:00
|
|
|
return $uids;
|
2011-06-21 17:28:46 +00:00
|
|
|
}
|
|
|
|
|
2013-01-29 19:42:21 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Get a list of all users display name
|
2015-02-02 15:09:30 +00:00
|
|
|
*
|
2013-05-28 22:32:10 +00:00
|
|
|
* @param string $search
|
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
|
|
|
* @return array associative array with all display names (value) and corresponding uids (key)
|
2013-01-29 19:42:21 +00:00
|
|
|
*
|
|
|
|
* Get a list of all display names and user ids.
|
2014-12-09 17:36:40 +00:00
|
|
|
* @deprecated Use \OC::$server->getUserManager->searchDisplayName($search, $limit, $offset) instead.
|
2013-01-29 19:42:21 +00:00
|
|
|
*/
|
|
|
|
public static function getDisplayNames($search = '', $limit = null, $offset = null) {
|
|
|
|
$displayNames = array();
|
2013-05-28 22:32:10 +00:00
|
|
|
$users = self::getManager()->searchDisplayName($search, $limit, $offset);
|
|
|
|
foreach ($users as $user) {
|
|
|
|
$displayNames[$user->getUID()] = $user->getDisplayName();
|
2013-01-29 19:42:21 +00:00
|
|
|
}
|
|
|
|
return $displayNames;
|
2013-01-25 10:05:00 +00:00
|
|
|
}
|
2011-06-21 17:28:46 +00:00
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* check if a user exists
|
2015-02-02 15:09:30 +00:00
|
|
|
*
|
2011-06-21 17:28:46 +00:00
|
|
|
* @param string $uid the username
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2013-05-28 22:32:10 +00:00
|
|
|
public static function userExists($uid) {
|
|
|
|
return self::getManager()->userExists($uid);
|
2010-09-15 16:24:14 +00:00
|
|
|
}
|
2012-08-29 06:38:33 +00:00
|
|
|
|
2012-07-29 16:07:51 +00:00
|
|
|
/**
|
|
|
|
* disables a user
|
2013-05-28 22:32:10 +00:00
|
|
|
*
|
2013-06-03 11:17:03 +00:00
|
|
|
* @param string $uid the user to disable
|
2012-07-29 16:07:51 +00:00
|
|
|
*/
|
2013-06-03 11:17:03 +00:00
|
|
|
public static function disableUser($uid) {
|
|
|
|
$user = self::getManager()->get($uid);
|
2013-05-28 22:32:10 +00:00
|
|
|
if ($user) {
|
|
|
|
$user->setEnabled(false);
|
2012-09-12 10:47:18 +00:00
|
|
|
}
|
2012-07-29 16:07:51 +00:00
|
|
|
}
|
2012-08-29 06:38:33 +00:00
|
|
|
|
2012-07-29 16:07:51 +00:00
|
|
|
/**
|
|
|
|
* enable a user
|
2013-05-28 22:32:10 +00:00
|
|
|
*
|
2013-06-03 11:17:03 +00:00
|
|
|
* @param string $uid
|
2012-07-29 16:07:51 +00:00
|
|
|
*/
|
2013-06-03 11:17:03 +00:00
|
|
|
public static function enableUser($uid) {
|
|
|
|
$user = self::getManager()->get($uid);
|
2013-05-28 22:32:10 +00:00
|
|
|
if ($user) {
|
|
|
|
$user->setEnabled(true);
|
2012-09-12 10:47:18 +00:00
|
|
|
}
|
2012-07-29 16:07:51 +00:00
|
|
|
}
|
2012-08-29 06:38:33 +00:00
|
|
|
|
2012-07-29 16:07:51 +00:00
|
|
|
/**
|
|
|
|
* checks if a user is enabled
|
2013-05-28 22:32:10 +00:00
|
|
|
*
|
2013-06-03 11:17:03 +00:00
|
|
|
* @param string $uid
|
2012-07-29 16:07:51 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2013-06-03 11:17:03 +00:00
|
|
|
public static function isEnabled($uid) {
|
|
|
|
$user = self::getManager()->get($uid);
|
2013-05-28 22:32:10 +00:00
|
|
|
if ($user) {
|
|
|
|
return $user->isEnabled();
|
2012-09-12 10:47:18 +00:00
|
|
|
} else {
|
2013-05-28 22:32:10 +00:00
|
|
|
return false;
|
2012-09-12 10:47:18 +00:00
|
|
|
}
|
2012-07-29 16:07:51 +00:00
|
|
|
}
|
2011-07-20 13:04:14 +00:00
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Set cookie value to use in next page load
|
2015-02-02 15:09:30 +00:00
|
|
|
*
|
2011-07-20 13:04:14 +00:00
|
|
|
* @param string $username username to be set
|
2013-05-28 22:32:10 +00:00
|
|
|
* @param string $token
|
2011-07-20 13:04:14 +00:00
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function setMagicInCookie($username, $token) {
|
2013-05-28 22:32:10 +00:00
|
|
|
self::getUserSession()->setMagicInCookie($username, $token);
|
2011-07-20 13:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Remove cookie for "remember username"
|
2011-07-20 13:04:14 +00:00
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function unsetMagicInCookie() {
|
2013-05-28 22:32:10 +00:00
|
|
|
self::getUserSession()->unsetMagicInCookie();
|
2011-07-20 13:04:14 +00:00
|
|
|
}
|
2013-10-02 13:31:46 +00:00
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Returns the first active backend from self::$_usedBackends.
|
2015-02-02 15:09:30 +00:00
|
|
|
*
|
2014-02-06 15:30:58 +00:00
|
|
|
* @return OCP\Authentication\IApacheBackend|null if no backend active, otherwise OCP\Authentication\IApacheBackend
|
2013-10-02 13:31:46 +00:00
|
|
|
*/
|
|
|
|
private static function findFirstActiveUsedBackend() {
|
|
|
|
foreach (self::$_usedBackends as $backend) {
|
|
|
|
if ($backend instanceof OCP\Authentication\IApacheBackend) {
|
|
|
|
if ($backend->isSessionActive()) {
|
|
|
|
return $backend;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2010-04-22 17:03:54 +00:00
|
|
|
}
|