2010-04-22 19:03:54 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2011-04-15 17:14:02 +02:00
|
|
|
* ownCloud
|
|
|
|
*
|
|
|
|
* @author Frank Karlitschek
|
2012-05-26 19:14:24 +02:00
|
|
|
* @copyright 2012 Frank Karlitschek frank@owncloud.org
|
2011-04-15 17:14:02 +02:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2010-04-24 12:40:20 +02:00
|
|
|
|
2010-04-22 19:03:54 +02:00
|
|
|
/**
|
2012-07-20 13:09:09 +02:00
|
|
|
* This class provides wrapper methods for user management. Multiple backends are
|
2012-06-01 17:34:09 +02:00
|
|
|
* supported. User management operations are delegated to the configured backend for
|
|
|
|
* execution.
|
2011-04-18 11:39:29 +02:00
|
|
|
*
|
|
|
|
* Hooks provided:
|
|
|
|
* pre_createUser(&run, uid, password)
|
|
|
|
* post_createUser(uid, password)
|
|
|
|
* pre_deleteUser(&run, uid)
|
|
|
|
* post_deleteUser(uid)
|
2013-05-29 01:05:49 +02:00
|
|
|
* pre_setPassword(&run, uid, password, recoveryPassword)
|
|
|
|
* post_setPassword(uid, password, recoveryPassword)
|
2013-05-19 13:33:33 +02:00
|
|
|
* pre_login(&run, uid, password)
|
2011-04-18 11:39:29 +02:00
|
|
|
* post_login(uid)
|
|
|
|
* logout()
|
2010-04-22 19:03:54 +02:00
|
|
|
*/
|
2011-07-29 21:36:03 +02:00
|
|
|
class OC_User {
|
2013-05-29 00:32:10 +02:00
|
|
|
public static $userSession = null;
|
2012-09-07 14:05:51 +02:00
|
|
|
|
2013-07-10 00:06:22 +02:00
|
|
|
public static function getUserSession() {
|
2013-05-29 00:32:10 +02:00
|
|
|
if (!self::$userSession) {
|
|
|
|
$manager = new \OC\User\Manager();
|
|
|
|
self::$userSession = new \OC\User\Session($manager, \OC::$session);
|
2013-05-29 01:05:49 +02:00
|
|
|
self::$userSession->listen('\OC\User', 'preCreateUser', function ($uid, $password) {
|
2013-05-31 22:51:16 +02:00
|
|
|
\OC_Hook::emit('OC_User', 'pre_createUser', array('run' => true, 'uid' => $uid, 'password' => $password));
|
2013-05-29 01:05:49 +02:00
|
|
|
});
|
|
|
|
self::$userSession->listen('\OC\User', 'postCreateUser', function ($user, $password) {
|
2013-06-01 00:49:06 +02:00
|
|
|
/** @var $user \OC\User\User */
|
2013-05-31 22:51:16 +02:00
|
|
|
\OC_Hook::emit('OC_User', 'post_createUser', array('uid' => $user->getUID(), 'password' => $password));
|
2013-05-29 01:05:49 +02:00
|
|
|
});
|
|
|
|
self::$userSession->listen('\OC\User', 'preDelete', function ($user) {
|
2013-06-01 00:49:06 +02:00
|
|
|
/** @var $user \OC\User\User */
|
2013-05-31 22:51:16 +02:00
|
|
|
\OC_Hook::emit('OC_User', 'pre_deleteUser', array('run' => true, 'uid' => $user->getUID()));
|
2013-05-29 01:05:49 +02:00
|
|
|
});
|
|
|
|
self::$userSession->listen('\OC\User', 'postDelete', function ($user) {
|
2013-06-01 00:49:06 +02:00
|
|
|
/** @var $user \OC\User\User */
|
2013-05-31 22:51:16 +02:00
|
|
|
\OC_Hook::emit('OC_User', 'post_deleteUser', array('uid' => $user->getUID()));
|
2013-05-29 01:05:49 +02:00
|
|
|
});
|
|
|
|
self::$userSession->listen('\OC\User', 'preSetPassword', function ($user, $password, $recoveryPassword) {
|
2013-06-01 00:49:06 +02:00
|
|
|
/** @var $user \OC\User\User */
|
2013-06-01 00:34:46 +02:00
|
|
|
OC_Hook::emit('OC_User', 'pre_setPassword', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword));
|
2013-05-29 01:05:49 +02:00
|
|
|
});
|
|
|
|
self::$userSession->listen('\OC\User', 'postSetPassword', function ($user, $password, $recoveryPassword) {
|
2013-06-01 00:49:06 +02:00
|
|
|
/** @var $user \OC\User\User */
|
2013-06-01 00:34:46 +02:00
|
|
|
OC_Hook::emit('OC_User', 'post_setPassword', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword));
|
2013-05-29 01:05:49 +02:00
|
|
|
});
|
|
|
|
self::$userSession->listen('\OC\User', 'preLogin', function ($uid, $password) {
|
2013-05-31 22:51:16 +02:00
|
|
|
\OC_Hook::emit('OC_User', 'pre_login', array('run' => true, 'uid' => $uid, 'password' => $password));
|
2013-05-29 01:05:49 +02:00
|
|
|
});
|
2013-05-31 19:58:31 +02:00
|
|
|
self::$userSession->listen('\OC\User', 'postLogin', function ($user, $password) {
|
2013-06-01 00:49:06 +02:00
|
|
|
/** @var $user \OC\User\User */
|
2013-05-31 22:51:16 +02:00
|
|
|
\OC_Hook::emit('OC_User', 'post_login', array('run' => true, 'uid' => $user->getUID(), 'password' => $password));
|
2013-05-29 01:05:49 +02:00
|
|
|
});
|
|
|
|
self::$userSession->listen('\OC\User', 'logout', function () {
|
|
|
|
\OC_Hook::emit('OC_User', 'logout', array());
|
|
|
|
});
|
2013-05-29 00:32:10 +02:00
|
|
|
}
|
|
|
|
return self::$userSession;
|
|
|
|
}
|
|
|
|
|
2013-06-01 00:49:06 +02:00
|
|
|
/**
|
|
|
|
* @return \OC\User\Manager
|
|
|
|
*/
|
2013-07-10 00:06:22 +02:00
|
|
|
public static function getManager() {
|
2013-05-29 00:32:10 +02:00
|
|
|
return self::getUserSession()->getManager();
|
|
|
|
}
|
2011-04-15 17:14:02 +02:00
|
|
|
|
|
|
|
private static $_backends = array();
|
2010-07-21 17:53:51 +02:00
|
|
|
|
2013-05-29 00:32:10 +02:00
|
|
|
private static $_usedBackends = array();
|
|
|
|
|
|
|
|
private static $_setupedBackends = array();
|
|
|
|
|
2010-07-21 17:53:51 +02:00
|
|
|
/**
|
2011-04-15 17:14:02 +02:00
|
|
|
* @brief registers backend
|
2013-05-29 00:32:10 +02:00
|
|
|
* @param string $backend name of the backend
|
|
|
|
* @deprecated Add classes by calling useBackend with a class instance instead
|
|
|
|
* @return bool
|
2010-07-22 23:42:18 +02:00
|
|
|
*
|
2011-04-15 17:14:02 +02:00
|
|
|
* Makes a list of backends that can be used by other modules
|
2010-07-22 23:42:18 +02:00
|
|
|
*/
|
2013-05-29 00:32:10 +02:00
|
|
|
public static function registerBackend($backend) {
|
2012-07-19 16:31:55 +02:00
|
|
|
self::$_backends[] = $backend;
|
2011-04-15 17:14:02 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief gets available backends
|
2013-05-29 00:32:10 +02:00
|
|
|
* @deprecated
|
2011-04-15 17:14:02 +02:00
|
|
|
* @returns array of backends
|
|
|
|
*
|
|
|
|
* Returns the names of all backends.
|
|
|
|
*/
|
2012-09-07 15:22:01 +02:00
|
|
|
public static function getBackends() {
|
2011-04-15 17:14:02 +02:00
|
|
|
return self::$_backends;
|
|
|
|
}
|
2011-06-23 15:24:09 +02:00
|
|
|
|
2011-06-21 19:28:46 +02:00
|
|
|
/**
|
|
|
|
* @brief gets used backends
|
2013-05-29 00:32:10 +02:00
|
|
|
* @deprecated
|
2011-06-21 19:28:46 +02:00
|
|
|
* @returns array of backends
|
|
|
|
*
|
|
|
|
* Returns the names of all used backends.
|
|
|
|
*/
|
2012-09-07 15:22:01 +02:00
|
|
|
public static function getUsedBackends() {
|
2011-06-21 19:28:46 +02:00
|
|
|
return array_keys(self::$_usedBackends);
|
|
|
|
}
|
2011-04-15 17:14:02 +02:00
|
|
|
|
|
|
|
/**
|
2011-06-21 19:28:46 +02:00
|
|
|
* @brief Adds the backend to the list of used backends
|
2013-05-29 00:32:10 +02:00
|
|
|
* @param string | OC_User_Backend $backend default: database The backend to use for user management
|
|
|
|
* @return bool
|
2011-04-15 17:14:02 +02:00
|
|
|
*
|
|
|
|
* Set the User Authentication Module
|
|
|
|
*/
|
2013-05-29 00:32:10 +02: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 14:56:04 +01:00
|
|
|
} else {
|
2012-07-19 16:31:55 +02:00
|
|
|
// You'll never know what happens
|
2013-05-29 00:32:10 +02:00
|
|
|
if (null === $backend OR !is_string($backend)) {
|
2012-07-19 16:31:55 +02:00
|
|
|
$backend = 'database';
|
|
|
|
}
|
2010-07-21 17:53:51 +02:00
|
|
|
|
2012-07-19 16:31:55 +02:00
|
|
|
// Load backend
|
2013-05-29 00:32:10 +02:00
|
|
|
switch ($backend) {
|
2012-07-19 16:31:55 +02:00
|
|
|
case 'database':
|
|
|
|
case 'mysql':
|
|
|
|
case 'sqlite':
|
2013-05-29 00:32:10 +02:00
|
|
|
OC_Log::write('core', 'Adding user backend ' . $backend . '.', OC_Log::DEBUG);
|
2012-07-19 16:31:55 +02:00
|
|
|
self::$_usedBackends[$backend] = new OC_User_Database();
|
2013-05-29 00:32:10 +02:00
|
|
|
self::getManager()->registerBackend(self::$_usedBackends[$backend]);
|
2012-07-19 16:31:55 +02:00
|
|
|
break;
|
|
|
|
default:
|
2013-05-29 00:32:10 +02:00
|
|
|
OC_Log::write('core', 'Adding default user backend ' . $backend . '.', OC_Log::DEBUG);
|
2012-07-19 16:31:55 +02:00
|
|
|
$className = 'OC_USER_' . strToUpper($backend);
|
|
|
|
self::$_usedBackends[$backend] = new $className();
|
2013-05-29 00:32:10 +02:00
|
|
|
self::getManager()->registerBackend(self::$_usedBackends[$backend]);
|
2012-07-19 16:31:55 +02:00
|
|
|
break;
|
|
|
|
}
|
2010-07-21 17:53:51 +02:00
|
|
|
}
|
2012-12-11 14:56:04 +01:00
|
|
|
return true;
|
2010-07-21 17:53:51 +02:00
|
|
|
}
|
2010-07-19 18:52:49 +02:00
|
|
|
|
2012-07-23 22:31:48 +02:00
|
|
|
/**
|
|
|
|
* remove all used backends
|
|
|
|
*/
|
2012-09-07 15:22:01 +02:00
|
|
|
public static function clearBackends() {
|
2013-05-29 00:32:10 +02:00
|
|
|
self::$_usedBackends = array();
|
|
|
|
self::getManager()->clearBackends();
|
2012-07-23 22:31:48 +02:00
|
|
|
}
|
|
|
|
|
2012-09-01 02:48:54 +02:00
|
|
|
/**
|
|
|
|
* setup the configured backends in config.php
|
|
|
|
*/
|
2012-09-07 15:22:01 +02:00
|
|
|
public static function setupBackends() {
|
2013-05-29 00:32:10 +02: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 14:56:04 +01:00
|
|
|
// make a reflection object
|
|
|
|
$reflectionObj = new ReflectionClass($class);
|
2012-09-01 02:48:54 +02:00
|
|
|
|
2012-12-11 14:56:04 +01:00
|
|
|
// use Reflection to create a new instance, using the $args
|
|
|
|
$backend = $reflectionObj->newInstanceArgs($arguments);
|
|
|
|
self::useBackend($backend);
|
2013-06-18 20:47:47 +02:00
|
|
|
self::$_setupedBackends[] = $i;
|
2012-12-11 14:56:04 +01:00
|
|
|
} else {
|
2013-05-29 00:32:10 +02:00
|
|
|
OC_Log::write('core', 'User backend ' . $class . ' already initialized.', OC_Log::DEBUG);
|
2012-12-11 14:56:04 +01:00
|
|
|
}
|
|
|
|
} else {
|
2013-05-29 00:32:10 +02:00
|
|
|
OC_Log::write('core', 'User backend ' . $class . ' not found.', OC_Log::ERROR);
|
2012-09-01 02:48:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-22 19:03:54 +02:00
|
|
|
/**
|
2011-04-18 10:41:01 +02:00
|
|
|
* @brief Create a new user
|
2013-05-29 00:32:10 +02: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 10:41:01 +02:00
|
|
|
*
|
2011-07-29 21:36:03 +02:00
|
|
|
* Creates a new user. Basic checking of username is done in OC_User
|
2011-04-18 11:39:29 +02:00
|
|
|
* itself, not in its subclasses.
|
|
|
|
*
|
|
|
|
* Allowed characters in the username are: "a-z", "A-Z", "0-9" and "_.@-"
|
2010-07-22 23:42:18 +02:00
|
|
|
*/
|
2013-05-29 00:32:10 +02:00
|
|
|
public static function createUser($uid, $password) {
|
2013-06-11 13:48:17 +02:00
|
|
|
return self::getManager()->createUser($uid, $password);
|
2010-07-21 17:53:51 +02:00
|
|
|
}
|
2010-07-19 18:52:49 +02:00
|
|
|
|
2011-04-17 01:04:23 +02:00
|
|
|
/**
|
2011-04-18 10:41:01 +02:00
|
|
|
* @brief delete a user
|
2013-05-29 00:32:10 +02:00
|
|
|
* @param string $uid The username of the user to delete
|
|
|
|
* @return bool
|
2011-04-18 10:41:01 +02:00
|
|
|
*
|
|
|
|
* Deletes a user
|
2011-04-17 01:04:23 +02:00
|
|
|
*/
|
2013-05-29 00:32:10 +02:00
|
|
|
public static function deleteUser($uid) {
|
|
|
|
$user = self::getManager()->get($uid);
|
|
|
|
if ($user) {
|
|
|
|
$user->delete();
|
|
|
|
|
2011-04-18 11:48:52 +02:00
|
|
|
// We have to delete the user from all groups
|
2013-05-29 00:32:10 +02:00
|
|
|
foreach (OC_Group::getUserGroups($uid) as $i) {
|
|
|
|
OC_Group::removeFromGroup($uid, $i);
|
2011-04-18 11:48:52 +02:00
|
|
|
}
|
2012-01-04 21:19:16 +01:00
|
|
|
// Delete the user's keys in preferences
|
|
|
|
OC_Preferences::deleteUser($uid);
|
2012-10-16 22:05:06 +02:00
|
|
|
|
|
|
|
// Delete user files in /data/
|
2013-05-31 22:51:16 +02:00
|
|
|
OC_Helper::rmdirr(OC_Config::getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid . '/');
|
2011-04-18 11:39:29 +02:00
|
|
|
}
|
2011-04-17 01:04:23 +02:00
|
|
|
}
|
|
|
|
|
2010-04-22 19:03:54 +02:00
|
|
|
/**
|
2011-04-18 10:41:01 +02:00
|
|
|
* @brief Try to login a user
|
|
|
|
* @param $uid The username of the user to log in
|
2011-04-15 17:14:02 +02:00
|
|
|
* @param $password The password of the user
|
2013-05-29 00:32:10 +02:00
|
|
|
* @return bool
|
2011-04-18 10:41:01 +02:00
|
|
|
*
|
2012-03-13 16:00:53 +01:00
|
|
|
* Log in a user and regenerate a new session - if the password is ok
|
2010-07-22 23:42:18 +02:00
|
|
|
*/
|
2013-05-29 00:32:10 +02:00
|
|
|
public static function login($uid, $password) {
|
|
|
|
return self::getUserSession()->login($uid, $password);
|
2011-09-18 15:05:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sets user id for session and triggers emit
|
|
|
|
*/
|
|
|
|
public static function setUserId($uid) {
|
2013-05-29 00:47:55 +02:00
|
|
|
OC::$session->set('user_id', $uid);
|
2010-07-21 17:53:51 +02:00
|
|
|
}
|
2010-07-19 18:52:49 +02:00
|
|
|
|
2013-01-29 20:42:21 +01:00
|
|
|
/**
|
|
|
|
* @brief Sets user display name for session
|
|
|
|
*/
|
2013-01-28 14:09:11 +01:00
|
|
|
public static function setDisplayName($uid, $displayName = null) {
|
2013-05-29 00:32:10 +02:00
|
|
|
if (is_null($displayName)) {
|
|
|
|
$displayName = $uid;
|
2013-01-28 14:09:11 +01:00
|
|
|
}
|
2013-05-29 00:32:10 +02:00
|
|
|
$user = self::getManager()->get($uid);
|
|
|
|
if ($user) {
|
|
|
|
return $user->setDisplayName($displayName);
|
|
|
|
} else {
|
|
|
|
return false;
|
2013-01-29 22:33:46 +01:00
|
|
|
}
|
2013-01-24 13:07:59 +01:00
|
|
|
}
|
2013-01-29 20:42:21 +01:00
|
|
|
|
2011-03-01 23:20:16 +01:00
|
|
|
/**
|
2012-05-01 21:07:08 +02:00
|
|
|
* @brief Logs the current user out and kills all the session data
|
2011-04-18 10:41:01 +02:00
|
|
|
*
|
|
|
|
* Logout, destroys session
|
2011-03-01 23:20:16 +01:00
|
|
|
*/
|
2012-09-07 15:22:01 +02:00
|
|
|
public static function logout() {
|
2013-05-29 00:32:10 +02:00
|
|
|
self::getUserSession()->logout();
|
2011-03-01 23:20:16 +01:00
|
|
|
}
|
|
|
|
|
2010-04-22 19:03:54 +02:00
|
|
|
/**
|
2011-04-15 17:14:02 +02:00
|
|
|
* @brief Check if the user is logged in
|
2013-06-01 00:49:06 +02:00
|
|
|
* @returns bool
|
2011-04-18 10:41:01 +02:00
|
|
|
*
|
|
|
|
* Checks if the user is logged in
|
2010-07-22 23:42:18 +02:00
|
|
|
*/
|
2012-09-07 15:22:01 +02:00
|
|
|
public static function isLoggedIn() {
|
2013-05-29 00:32:10 +02:00
|
|
|
if (\OC::$session->get('user_id')) {
|
2012-06-04 21:31:35 +02:00
|
|
|
OC_App::loadApps(array('authentication'));
|
2012-09-01 02:48:54 +02:00
|
|
|
self::setupBackends();
|
2013-05-29 00:32:10 +02:00
|
|
|
return self::userExists(\OC::$session->get('user_id'));
|
2011-06-21 19:28:46 +02:00
|
|
|
}
|
2012-07-05 15:55:46 +02:00
|
|
|
return false;
|
2010-07-21 17:53:51 +02:00
|
|
|
}
|
2010-07-19 18:52:49 +02:00
|
|
|
|
2013-01-14 19:45:17 +01:00
|
|
|
/**
|
|
|
|
* @brief Check if the user is an admin user
|
2013-05-29 00:32:10 +02:00
|
|
|
* @param string $uid uid of the admin
|
|
|
|
* @return bool
|
2013-01-14 19:45:17 +01:00
|
|
|
*/
|
|
|
|
public static function isAdminUser($uid) {
|
2013-05-29 00:32:10 +02:00
|
|
|
if (OC_Group::inGroup($uid, 'admin')) {
|
2013-01-14 19:45:17 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-22 12:50:57 +02:00
|
|
|
/**
|
2012-04-30 13:08:08 +02:00
|
|
|
* @brief get the user id of the user currently logged in.
|
2011-06-22 12:50:57 +02:00
|
|
|
* @return string uid or false
|
|
|
|
*/
|
2012-09-07 15:22:01 +02:00
|
|
|
public static function getUser() {
|
2013-07-16 22:37:32 +02:00
|
|
|
$uid = OC::$session ? OC::$session->get('user_id') : null;
|
2013-05-29 00:47:55 +02:00
|
|
|
if (!is_null($uid)) {
|
|
|
|
return $uid;
|
2013-05-29 00:32:10 +02:00
|
|
|
} else {
|
2011-06-22 12:50:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 20:42:21 +01:00
|
|
|
/**
|
|
|
|
* @brief get the display name of the user currently logged in.
|
2013-06-03 13:15:42 +02:00
|
|
|
* @param string $uid
|
2013-05-31 18:18:13 +02:00
|
|
|
* @return string uid or false
|
2013-01-29 20:42:21 +01:00
|
|
|
*/
|
2013-06-03 13:15:42 +02:00
|
|
|
public static function getDisplayName($uid = null) {
|
|
|
|
if ($uid) {
|
|
|
|
$user = self::getManager()->get($uid);
|
|
|
|
if ($user) {
|
|
|
|
return $user->getDisplayName();
|
|
|
|
} else {
|
|
|
|
return $uid;
|
|
|
|
}
|
2013-05-29 00:32:10 +02:00
|
|
|
} else {
|
|
|
|
$user = self::getUserSession()->getUser();
|
|
|
|
if ($user) {
|
|
|
|
return $user->getDisplayName();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-29 20:42:21 +01:00
|
|
|
}
|
2013-01-24 13:07:59 +01:00
|
|
|
}
|
2013-01-29 20:42:21 +01:00
|
|
|
|
2010-04-22 19:03:54 +02:00
|
|
|
/**
|
2011-04-18 10:41:01 +02:00
|
|
|
* @brief Autogenerate a password
|
2013-05-29 00:32:10 +02:00
|
|
|
* @return string
|
2011-04-18 10:41:01 +02:00
|
|
|
*
|
|
|
|
* generates a password
|
2010-07-22 23:42:18 +02:00
|
|
|
*/
|
2012-09-07 15:22:01 +02:00
|
|
|
public static function generatePassword() {
|
2013-04-04 00:27:13 +02:00
|
|
|
return OC_Util::generate_random_bytes(30);
|
2010-07-21 17:53:51 +02:00
|
|
|
}
|
2010-07-19 18:52:49 +02:00
|
|
|
|
2010-04-22 19:03:54 +02:00
|
|
|
/**
|
2011-04-18 10:41:01 +02:00
|
|
|
* @brief Set password
|
2013-05-29 00:32:10 +02: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 10:41:01 +02:00
|
|
|
*
|
|
|
|
* Change the password of a user
|
2010-07-22 23:42:18 +02:00
|
|
|
*/
|
2013-05-29 00:32:10 +02: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 11:39:29 +02:00
|
|
|
return false;
|
|
|
|
}
|
2010-07-21 17:53:51 +02:00
|
|
|
}
|
2010-07-19 18:52:49 +02:00
|
|
|
|
2013-02-05 14:58:35 +01:00
|
|
|
/**
|
|
|
|
* @brief Check whether user can change his password
|
2013-05-29 00:32:10 +02:00
|
|
|
* @param string $uid The username
|
|
|
|
* @return bool
|
2013-02-05 14:58:35 +01:00
|
|
|
*
|
|
|
|
* Check whether a specified user can change his password
|
|
|
|
*/
|
|
|
|
public static function canUserChangePassword($uid) {
|
2013-05-29 00:32:10 +02:00
|
|
|
$user = self::getManager()->get($uid);
|
|
|
|
if ($user) {
|
|
|
|
return $user->canChangePassword();
|
|
|
|
} else {
|
|
|
|
return false;
|
2013-02-05 14:58:35 +01:00
|
|
|
}
|
|
|
|
}
|
2013-02-22 17:21:57 +01:00
|
|
|
|
2013-02-06 11:38:03 +01:00
|
|
|
/**
|
|
|
|
* @brief Check whether user can change his display name
|
2013-05-29 00:32:10 +02:00
|
|
|
* @param string $uid The username
|
|
|
|
* @return bool
|
2013-02-06 11:38:03 +01:00
|
|
|
*
|
|
|
|
* Check whether a specified user can change his display name
|
|
|
|
*/
|
|
|
|
public static function canUserChangeDisplayName($uid) {
|
2013-05-29 00:32:10 +02:00
|
|
|
$user = self::getManager()->get($uid);
|
|
|
|
if ($user) {
|
|
|
|
return $user->canChangeDisplayName();
|
|
|
|
} else {
|
|
|
|
return false;
|
2013-02-06 11:38:03 +01:00
|
|
|
}
|
|
|
|
}
|
2013-02-05 14:58:35 +01:00
|
|
|
|
2010-04-22 19:03:54 +02:00
|
|
|
/**
|
2011-04-18 10:41:01 +02:00
|
|
|
* @brief Check if the password is correct
|
2013-05-29 00:32:10 +02:00
|
|
|
* @param string $uid The username
|
|
|
|
* @param string $password The password
|
|
|
|
* @return bool
|
2011-04-18 10:41:01 +02:00
|
|
|
*
|
|
|
|
* Check if the password is correct without logging in the user
|
2012-05-17 00:57:43 +02:00
|
|
|
* returns the user id or false
|
2010-07-22 23:42:18 +02:00
|
|
|
*/
|
2013-05-29 00:32:10 +02:00
|
|
|
public static function checkPassword($uid, $password) {
|
|
|
|
$user = self::getManager()->get($uid);
|
|
|
|
if ($user) {
|
|
|
|
if ($user->checkPassword($password)) {
|
|
|
|
return $user->getUID();
|
|
|
|
} else {
|
|
|
|
return false;
|
2011-06-21 19:28:46 +02:00
|
|
|
}
|
2013-05-29 00:32:10 +02:00
|
|
|
} else {
|
|
|
|
return false;
|
2011-06-21 19:28:46 +02:00
|
|
|
}
|
2010-07-21 17:53:51 +02:00
|
|
|
}
|
2010-07-15 19:10:20 +02:00
|
|
|
|
2012-08-26 16:24:25 +02:00
|
|
|
/**
|
2013-01-28 19:08:03 +01:00
|
|
|
* @param string $uid The username
|
2013-05-29 00:32:10 +02:00
|
|
|
* @return string
|
2012-08-26 16:24:25 +02:00
|
|
|
*
|
2012-10-27 15:23:35 +02:00
|
|
|
* returns the path to the users home directory
|
2012-08-26 16:24:25 +02:00
|
|
|
*/
|
2012-09-07 15:22:01 +02:00
|
|
|
public static function getHome($uid) {
|
2013-05-29 00:32:10 +02:00
|
|
|
$user = self::getManager()->get($uid);
|
|
|
|
if ($user) {
|
|
|
|
return $user->getHome();
|
|
|
|
} else {
|
2013-05-31 22:51:16 +02:00
|
|
|
return OC_Config::getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid;
|
2012-08-26 16:24:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-15 18:24:14 +02:00
|
|
|
/**
|
2011-04-18 10:41:01 +02:00
|
|
|
* @brief Get a list of all users
|
|
|
|
* @returns array with all uids
|
|
|
|
*
|
|
|
|
* Get a list of all users.
|
2010-09-15 18:24:14 +02:00
|
|
|
*/
|
2012-08-25 00:05:07 +02:00
|
|
|
public static function getUsers($search = '', $limit = null, $offset = null) {
|
2013-05-29 00:32:10 +02:00
|
|
|
$users = self::getManager()->search($search, $limit, $offset);
|
|
|
|
$uids = array();
|
|
|
|
foreach ($users as $user) {
|
|
|
|
$uids[] = $user->getUID();
|
2011-06-21 19:28:46 +02:00
|
|
|
}
|
2013-05-29 00:32:10 +02:00
|
|
|
return $uids;
|
2011-06-21 19:28:46 +02:00
|
|
|
}
|
|
|
|
|
2013-01-29 20:42:21 +01:00
|
|
|
/**
|
|
|
|
* @brief Get a list of all users display name
|
2013-05-29 00:32:10 +02: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 20:42:21 +01:00
|
|
|
*
|
|
|
|
* Get a list of all display names and user ids.
|
|
|
|
*/
|
|
|
|
public static function getDisplayNames($search = '', $limit = null, $offset = null) {
|
|
|
|
$displayNames = array();
|
2013-05-29 00:32:10 +02:00
|
|
|
$users = self::getManager()->searchDisplayName($search, $limit, $offset);
|
|
|
|
foreach ($users as $user) {
|
|
|
|
$displayNames[$user->getUID()] = $user->getDisplayName();
|
2013-01-29 20:42:21 +01:00
|
|
|
}
|
|
|
|
return $displayNames;
|
2013-01-25 11:05:00 +01:00
|
|
|
}
|
2011-06-21 19:28:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief check if a user exists
|
|
|
|
* @param string $uid the username
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2013-05-29 00:32:10 +02:00
|
|
|
public static function userExists($uid) {
|
|
|
|
return self::getManager()->userExists($uid);
|
2010-09-15 18:24:14 +02:00
|
|
|
}
|
2012-08-29 08:38:33 +02:00
|
|
|
|
2012-07-29 18:07:51 +02:00
|
|
|
/**
|
|
|
|
* disables a user
|
2013-05-29 00:32:10 +02:00
|
|
|
*
|
2013-06-03 13:17:03 +02:00
|
|
|
* @param string $uid the user to disable
|
2012-07-29 18:07:51 +02:00
|
|
|
*/
|
2013-06-03 13:17:03 +02:00
|
|
|
public static function disableUser($uid) {
|
|
|
|
$user = self::getManager()->get($uid);
|
2013-05-29 00:32:10 +02:00
|
|
|
if ($user) {
|
|
|
|
$user->setEnabled(false);
|
2012-09-12 12:47:18 +02:00
|
|
|
}
|
2012-07-29 18:07:51 +02:00
|
|
|
}
|
2012-08-29 08:38:33 +02:00
|
|
|
|
2012-07-29 18:07:51 +02:00
|
|
|
/**
|
|
|
|
* enable a user
|
2013-05-29 00:32:10 +02:00
|
|
|
*
|
2013-06-03 13:17:03 +02:00
|
|
|
* @param string $uid
|
2012-07-29 18:07:51 +02:00
|
|
|
*/
|
2013-06-03 13:17:03 +02:00
|
|
|
public static function enableUser($uid) {
|
|
|
|
$user = self::getManager()->get($uid);
|
2013-05-29 00:32:10 +02:00
|
|
|
if ($user) {
|
|
|
|
$user->setEnabled(true);
|
2012-09-12 12:47:18 +02:00
|
|
|
}
|
2012-07-29 18:07:51 +02:00
|
|
|
}
|
2012-08-29 08:38:33 +02:00
|
|
|
|
2012-07-29 18:07:51 +02:00
|
|
|
/**
|
|
|
|
* checks if a user is enabled
|
2013-05-29 00:32:10 +02:00
|
|
|
*
|
2013-06-03 13:17:03 +02:00
|
|
|
* @param string $uid
|
2012-07-29 18:07:51 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2013-06-03 13:17:03 +02:00
|
|
|
public static function isEnabled($uid) {
|
|
|
|
$user = self::getManager()->get($uid);
|
2013-05-29 00:32:10 +02:00
|
|
|
if ($user) {
|
|
|
|
return $user->isEnabled();
|
2012-09-12 12:47:18 +02:00
|
|
|
} else {
|
2013-05-29 00:32:10 +02:00
|
|
|
return false;
|
2012-09-12 12:47:18 +02:00
|
|
|
}
|
2012-07-29 18:07:51 +02:00
|
|
|
}
|
2011-07-20 15:04:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set cookie value to use in next page load
|
|
|
|
* @param string $username username to be set
|
2013-05-29 00:32:10 +02:00
|
|
|
* @param string $token
|
2011-07-20 15:04:14 +02:00
|
|
|
*/
|
2012-09-07 15:22:01 +02:00
|
|
|
public static function setMagicInCookie($username, $token) {
|
2013-05-29 00:32:10 +02:00
|
|
|
self::getUserSession()->setMagicInCookie($username, $token);
|
2011-07-20 15:04:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Remove cookie for "remember username"
|
|
|
|
*/
|
2012-09-07 15:22:01 +02:00
|
|
|
public static function unsetMagicInCookie() {
|
2013-05-29 00:32:10 +02:00
|
|
|
self::getUserSession()->unsetMagicInCookie();
|
2011-07-20 15:04:14 +02:00
|
|
|
}
|
2010-04-22 19:03:54 +02:00
|
|
|
}
|