2010-04-22 21:03:54 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2011-04-15 19:14:02 +04:00
|
|
|
* ownCloud
|
|
|
|
*
|
|
|
|
* @author Frank Karlitschek
|
2012-05-26 21:14:24 +04:00
|
|
|
* @copyright 2012 Frank Karlitschek frank@owncloud.org
|
2011-04-15 19:14:02 +04: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 14:40:20 +04:00
|
|
|
|
2010-04-22 21:03:54 +04:00
|
|
|
/**
|
2012-07-20 15:09:09 +04:00
|
|
|
* This class provides wrapper methods for user management. Multiple backends are
|
2012-06-01 19:34:09 +04:00
|
|
|
* supported. User management operations are delegated to the configured backend for
|
|
|
|
* execution.
|
2011-04-18 13:39:29 +04:00
|
|
|
*
|
|
|
|
* Hooks provided:
|
|
|
|
* pre_createUser(&run, uid, password)
|
|
|
|
* post_createUser(uid, password)
|
|
|
|
* pre_deleteUser(&run, uid)
|
|
|
|
* post_deleteUser(uid)
|
|
|
|
* pre_setPassword(&run, uid, password)
|
|
|
|
* post_setPassword(uid, password)
|
|
|
|
* pre_login(&run, uid)
|
|
|
|
* post_login(uid)
|
|
|
|
* logout()
|
2010-04-22 21:03:54 +04:00
|
|
|
*/
|
2011-07-29 23:36:03 +04:00
|
|
|
class OC_User {
|
2010-07-21 19:53:51 +04:00
|
|
|
// The backend used for user management
|
2011-06-21 21:28:46 +04:00
|
|
|
private static $_usedBackends = array();
|
2012-09-07 16:05:51 +04:00
|
|
|
|
2012-09-01 04:48:54 +04:00
|
|
|
private static $_setupedBackends = array();
|
2011-04-15 19:14:02 +04:00
|
|
|
|
|
|
|
// Backends available (except database)
|
|
|
|
private static $_backends = array();
|
2010-07-21 19:53:51 +04:00
|
|
|
|
|
|
|
/**
|
2011-04-15 19:14:02 +04:00
|
|
|
* @brief registers backend
|
|
|
|
* @param $name name of the backend
|
|
|
|
* @returns true/false
|
2010-07-23 01:42:18 +04:00
|
|
|
*
|
2011-04-15 19:14:02 +04:00
|
|
|
* Makes a list of backends that can be used by other modules
|
2010-07-23 01:42:18 +04:00
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function registerBackend( $backend ) {
|
2012-07-19 18:31:55 +04:00
|
|
|
self::$_backends[] = $backend;
|
2011-04-15 19:14:02 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief gets available backends
|
|
|
|
* @returns array of backends
|
|
|
|
*
|
|
|
|
* Returns the names of all backends.
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function getBackends() {
|
2011-04-15 19:14:02 +04:00
|
|
|
return self::$_backends;
|
|
|
|
}
|
2011-06-23 17:24:09 +04:00
|
|
|
|
2011-06-21 21:28:46 +04:00
|
|
|
/**
|
|
|
|
* @brief gets used backends
|
|
|
|
* @returns array of backends
|
|
|
|
*
|
|
|
|
* Returns the names of all used backends.
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function getUsedBackends() {
|
2011-06-21 21:28:46 +04:00
|
|
|
return array_keys(self::$_usedBackends);
|
|
|
|
}
|
2011-04-15 19:14:02 +04:00
|
|
|
|
|
|
|
/**
|
2011-06-21 21:28:46 +04:00
|
|
|
* @brief Adds the backend to the list of used backends
|
2011-04-15 19:14:02 +04:00
|
|
|
* @param $backend default: database The backend to use for user managment
|
|
|
|
* @returns true/false
|
|
|
|
*
|
|
|
|
* Set the User Authentication Module
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function useBackend( $backend = 'database' ) {
|
|
|
|
if($backend instanceof OC_User_Interface) {
|
2012-12-11 17:56:04 +04:00
|
|
|
OC_Log::write('core', 'Adding user backend instance of '.get_class($backend).'.', OC_Log::DEBUG);
|
2012-07-19 18:31:55 +04:00
|
|
|
self::$_usedBackends[get_class($backend)]=$backend;
|
2012-12-11 17:56:04 +04:00
|
|
|
} else {
|
2012-07-19 18:31:55 +04:00
|
|
|
// You'll never know what happens
|
2012-09-07 17:22:01 +04:00
|
|
|
if( null === $backend OR !is_string( $backend )) {
|
2012-07-19 18:31:55 +04:00
|
|
|
$backend = 'database';
|
|
|
|
}
|
2010-07-21 19:53:51 +04:00
|
|
|
|
2012-07-19 18:31:55 +04:00
|
|
|
// Load backend
|
2012-09-07 17:22:01 +04:00
|
|
|
switch( $backend ) {
|
2012-07-19 18:31:55 +04:00
|
|
|
case 'database':
|
|
|
|
case 'mysql':
|
|
|
|
case 'sqlite':
|
2012-12-11 17:56:04 +04:00
|
|
|
OC_Log::write('core', 'Adding user backend '.$backend.'.', OC_Log::DEBUG);
|
2012-07-19 18:31:55 +04:00
|
|
|
self::$_usedBackends[$backend] = new OC_User_Database();
|
|
|
|
break;
|
|
|
|
default:
|
2012-12-11 17:56:04 +04:00
|
|
|
OC_Log::write('core', 'Adding default user backend '.$backend.'.', OC_Log::DEBUG);
|
2012-07-19 18:31:55 +04:00
|
|
|
$className = 'OC_USER_' . strToUpper($backend);
|
|
|
|
self::$_usedBackends[$backend] = new $className();
|
|
|
|
break;
|
|
|
|
}
|
2010-07-21 19:53:51 +04:00
|
|
|
}
|
2012-12-11 17:56:04 +04:00
|
|
|
return true;
|
2010-07-21 19:53:51 +04:00
|
|
|
}
|
2010-07-19 20:52:49 +04:00
|
|
|
|
2012-07-24 00:31:48 +04:00
|
|
|
/**
|
|
|
|
* remove all used backends
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function clearBackends() {
|
2012-07-24 00:31:48 +04:00
|
|
|
self::$_usedBackends=array();
|
|
|
|
}
|
|
|
|
|
2012-09-01 04:48:54 +04:00
|
|
|
/**
|
|
|
|
* setup the configured backends in config.php
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function setupBackends() {
|
2012-10-24 00:53:54 +04:00
|
|
|
$backends=OC_Config::getValue('user_backends', array());
|
2012-09-07 17:22:01 +04:00
|
|
|
foreach($backends as $i=>$config) {
|
2012-09-01 04:48:54 +04:00
|
|
|
$class=$config['class'];
|
|
|
|
$arguments=$config['arguments'];
|
2012-12-11 17:56:04 +04:00
|
|
|
if(class_exists($class)) {
|
|
|
|
if(array_search($i, self::$_setupedBackends)===false) {
|
|
|
|
// make a reflection object
|
|
|
|
$reflectionObj = new ReflectionClass($class);
|
2012-09-01 04:48:54 +04:00
|
|
|
|
2012-12-11 17:56:04 +04:00
|
|
|
// use Reflection to create a new instance, using the $args
|
|
|
|
$backend = $reflectionObj->newInstanceArgs($arguments);
|
|
|
|
self::useBackend($backend);
|
|
|
|
$_setupedBackends[]=$i;
|
|
|
|
} else {
|
|
|
|
OC_Log::write('core', 'User backend '.$class.' already initialized.', OC_Log::DEBUG);
|
|
|
|
}
|
|
|
|
} else {
|
2012-11-04 21:28:29 +04:00
|
|
|
OC_Log::write('core', 'User backend '.$class.' not found.', OC_Log::ERROR);
|
2012-09-01 04:48:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-22 21:03:54 +04:00
|
|
|
/**
|
2011-04-18 12:41:01 +04:00
|
|
|
* @brief Create a new user
|
2011-04-18 13:39:29 +04:00
|
|
|
* @param $uid The username of the user to create
|
2011-04-15 19:14:02 +04:00
|
|
|
* @param $password The password of the new user
|
2011-04-18 12:41:01 +04:00
|
|
|
* @returns true/false
|
|
|
|
*
|
2011-07-29 23:36:03 +04:00
|
|
|
* Creates a new user. Basic checking of username is done in OC_User
|
2011-04-18 13:39:29 +04:00
|
|
|
* itself, not in its subclasses.
|
|
|
|
*
|
|
|
|
* Allowed characters in the username are: "a-z", "A-Z", "0-9" and "_.@-"
|
2010-07-23 01:42:18 +04:00
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function createUser( $uid, $password ) {
|
2011-04-18 13:39:29 +04:00
|
|
|
// Check the name for bad characters
|
|
|
|
// Allowed are: "a-z", "A-Z", "0-9" and "_.@-"
|
2012-09-07 17:22:01 +04:00
|
|
|
if( preg_match( '/[^a-zA-Z0-9 _\.@\-]/', $uid )) {
|
2013-02-11 20:44:02 +04:00
|
|
|
throw new Exception('Only the following characters are allowed in a username:'
|
|
|
|
.' "a-z", "A-Z", "0-9", and "_.@-"');
|
2011-04-18 13:39:29 +04:00
|
|
|
}
|
2011-04-18 13:48:52 +04:00
|
|
|
// No empty username
|
2012-09-07 17:22:01 +04:00
|
|
|
if(trim($uid) == '') {
|
2012-05-07 02:04:31 +04:00
|
|
|
throw new Exception('A valid username must be provided');
|
2011-04-18 13:48:52 +04:00
|
|
|
}
|
2012-05-13 22:53:45 +04:00
|
|
|
// No empty password
|
2012-09-07 17:22:01 +04:00
|
|
|
if(trim($password) == '') {
|
2012-05-13 22:53:45 +04:00
|
|
|
throw new Exception('A valid password must be provided');
|
|
|
|
}
|
2012-06-04 23:31:35 +04:00
|
|
|
|
2011-04-18 14:39:28 +04:00
|
|
|
// Check if user already exists
|
2013-02-08 20:16:18 +04:00
|
|
|
if( self::userExistsForCreation($uid) ) {
|
2012-05-07 02:04:31 +04:00
|
|
|
throw new Exception('The username is already being used');
|
2011-04-18 14:39:28 +04:00
|
|
|
}
|
|
|
|
|
2011-04-18 13:48:52 +04:00
|
|
|
|
2011-04-18 13:39:29 +04:00
|
|
|
$run = true;
|
2011-07-29 23:36:03 +04:00
|
|
|
OC_Hook::emit( "OC_User", "pre_createUser", array( "run" => &$run, "uid" => $uid, "password" => $password ));
|
2011-04-18 13:39:29 +04:00
|
|
|
|
2012-09-07 17:22:01 +04:00
|
|
|
if( $run ) {
|
2011-06-21 21:28:46 +04:00
|
|
|
//create the user in the first backend that supports creating users
|
2012-09-07 17:22:01 +04:00
|
|
|
foreach(self::$_usedBackends as $backend) {
|
2011-06-23 17:24:09 +04:00
|
|
|
if(!$backend->implementsActions(OC_USER_BACKEND_CREATE_USER))
|
|
|
|
continue;
|
|
|
|
|
2012-11-02 22:53:02 +04:00
|
|
|
$backend->createUser($uid, $password);
|
2011-07-29 23:36:03 +04:00
|
|
|
OC_Hook::emit( "OC_User", "post_createUser", array( "uid" => $uid, "password" => $password ));
|
2011-06-23 17:24:09 +04:00
|
|
|
|
2012-11-15 00:44:44 +04:00
|
|
|
return self::userExists($uid);
|
2011-06-21 21:28:46 +04:00
|
|
|
}
|
2011-04-18 13:39:29 +04:00
|
|
|
}
|
2011-06-21 21:28:46 +04:00
|
|
|
return false;
|
2010-07-21 19:53:51 +04:00
|
|
|
}
|
2010-07-19 20:52:49 +04:00
|
|
|
|
2011-04-17 03:04:23 +04:00
|
|
|
/**
|
2011-04-18 12:41:01 +04:00
|
|
|
* @brief delete a user
|
|
|
|
* @param $uid The username of the user to delete
|
|
|
|
* @returns true/false
|
|
|
|
*
|
|
|
|
* Deletes a user
|
2011-04-17 03:04:23 +04:00
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function deleteUser( $uid ) {
|
2011-04-18 13:39:29 +04:00
|
|
|
$run = true;
|
2011-07-29 23:36:03 +04:00
|
|
|
OC_Hook::emit( "OC_User", "pre_deleteUser", array( "run" => &$run, "uid" => $uid ));
|
2011-04-18 13:39:29 +04:00
|
|
|
|
2012-09-07 17:22:01 +04:00
|
|
|
if( $run ) {
|
2011-06-21 21:28:46 +04:00
|
|
|
//delete the user from all backends
|
2012-09-07 17:22:01 +04:00
|
|
|
foreach(self::$_usedBackends as $backend) {
|
2012-05-08 11:07:11 +04:00
|
|
|
$backend->deleteUser($uid);
|
2011-06-21 21:28:46 +04:00
|
|
|
}
|
2012-11-15 21:10:40 +04:00
|
|
|
if (self::userExists($uid)) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-18 13:48:52 +04:00
|
|
|
// We have to delete the user from all groups
|
2012-09-07 17:22:01 +04:00
|
|
|
foreach( OC_Group::getUserGroups( $uid ) as $i ) {
|
2011-07-29 23:36:03 +04:00
|
|
|
OC_Group::removeFromGroup( $uid, $i );
|
2011-04-18 13:48:52 +04:00
|
|
|
}
|
2012-01-05 00:19:16 +04:00
|
|
|
// Delete the user's keys in preferences
|
|
|
|
OC_Preferences::deleteUser($uid);
|
2012-10-17 00:05:06 +04:00
|
|
|
|
|
|
|
// Delete user files in /data/
|
|
|
|
OC_Helper::rmdirr(OC_Config::getValue( "datadirectory", OC::$SERVERROOT."/data" ) . '/'.$uid.'/');
|
|
|
|
|
2011-04-18 13:48:52 +04:00
|
|
|
// Emit and exit
|
2011-07-29 23:36:03 +04:00
|
|
|
OC_Hook::emit( "OC_User", "post_deleteUser", array( "uid" => $uid ));
|
2012-11-15 21:10:40 +04:00
|
|
|
return true;
|
2011-04-18 13:39:29 +04:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-17 03:04:23 +04:00
|
|
|
}
|
|
|
|
|
2010-04-22 21:03:54 +04:00
|
|
|
/**
|
2011-04-18 12:41:01 +04:00
|
|
|
* @brief Try to login a user
|
|
|
|
* @param $uid The username of the user to log in
|
2011-04-15 19:14:02 +04:00
|
|
|
* @param $password The password of the user
|
2011-04-18 12:41:01 +04:00
|
|
|
* @returns true/false
|
|
|
|
*
|
2012-03-13 19:00:53 +04:00
|
|
|
* Log in a user and regenerate a new session - if the password is ok
|
2010-07-23 01:42:18 +04:00
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function login( $uid, $password ) {
|
2011-04-18 13:39:29 +04:00
|
|
|
$run = true;
|
2011-07-29 23:36:03 +04:00
|
|
|
OC_Hook::emit( "OC_User", "pre_login", array( "run" => &$run, "uid" => $uid ));
|
2011-04-18 13:39:29 +04:00
|
|
|
|
2012-09-07 17:22:01 +04:00
|
|
|
if( $run ) {
|
2012-07-29 20:07:51 +04:00
|
|
|
$uid = self::checkPassword( $uid, $password );
|
|
|
|
$enabled = self::isEnabled($uid);
|
2012-09-07 17:22:01 +04:00
|
|
|
if($uid && $enabled) {
|
2012-07-15 23:22:31 +04:00
|
|
|
session_regenerate_id(true);
|
2012-02-06 00:49:22 +04:00
|
|
|
self::setUserId($uid);
|
2013-01-24 16:07:59 +04:00
|
|
|
self::setDisplayName($uid);
|
2011-11-24 04:44:54 +04:00
|
|
|
OC_Hook::emit( "OC_User", "post_login", array( "uid" => $uid, 'password'=>$password ));
|
2012-02-06 00:49:22 +04:00
|
|
|
return true;
|
2011-08-25 23:51:04 +04:00
|
|
|
}
|
2011-04-18 13:39:29 +04:00
|
|
|
}
|
2011-09-18 17:05:53 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sets user id for session and triggers emit
|
|
|
|
*/
|
|
|
|
public static function setUserId($uid) {
|
|
|
|
$_SESSION['user_id'] = $uid;
|
2010-07-21 19:53:51 +04:00
|
|
|
}
|
2010-07-19 20:52:49 +04:00
|
|
|
|
2013-01-29 23:42:21 +04:00
|
|
|
/**
|
|
|
|
* @brief Sets user display name for session
|
|
|
|
*/
|
2013-01-28 17:09:11 +04:00
|
|
|
public static function setDisplayName($uid, $displayName = null) {
|
|
|
|
$result = false;
|
2013-01-29 23:42:21 +04:00
|
|
|
if ($displayName ) {
|
|
|
|
foreach(self::$_usedBackends as $backend) {
|
|
|
|
if($backend->implementsActions(OC_USER_BACKEND_SET_DISPLAYNAME)) {
|
|
|
|
if($backend->userExists($uid)) {
|
2013-02-06 14:38:03 +04:00
|
|
|
$result |= $backend->setDisplayName($uid, $displayName);
|
2013-01-29 23:42:21 +04:00
|
|
|
}
|
|
|
|
}
|
2013-01-28 17:09:11 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$displayName = self::determineDisplayName($uid);
|
|
|
|
$result = true;
|
|
|
|
}
|
2013-01-30 01:33:46 +04:00
|
|
|
if (OC_User::getUser() === $uid) {
|
|
|
|
$_SESSION['display_name'] = $displayName;
|
|
|
|
}
|
2013-01-29 23:42:21 +04:00
|
|
|
return $result;
|
2013-01-24 16:07:59 +04:00
|
|
|
}
|
2013-01-29 23:42:21 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief get display name
|
|
|
|
* @param $uid The username
|
|
|
|
* @returns string display name or uid if no display name is defined
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private static function determineDisplayName( $uid ) {
|
|
|
|
foreach(self::$_usedBackends as $backend) {
|
|
|
|
if($backend->implementsActions(OC_USER_BACKEND_GET_DISPLAYNAME)) {
|
|
|
|
$result=$backend->getDisplayName( $uid );
|
|
|
|
if($result) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
2013-01-24 16:07:59 +04:00
|
|
|
}
|
2013-01-29 23:42:21 +04:00
|
|
|
return $uid;
|
2013-01-24 16:07:59 +04:00
|
|
|
}
|
2010-07-19 20:52:49 +04:00
|
|
|
|
2011-03-02 01:20:16 +03:00
|
|
|
/**
|
2012-05-01 23:07:08 +04:00
|
|
|
* @brief Logs the current user out and kills all the session data
|
2011-04-18 12:41:01 +04:00
|
|
|
*
|
|
|
|
* Logout, destroys session
|
2011-03-02 01:20:16 +03:00
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function logout() {
|
2011-07-29 23:36:03 +04:00
|
|
|
OC_Hook::emit( "OC_User", "logout", array());
|
2012-03-13 19:00:53 +04:00
|
|
|
session_unset();
|
|
|
|
session_destroy();
|
2011-09-18 17:05:53 +04:00
|
|
|
OC_User::unsetMagicInCookie();
|
2011-03-02 01:20:16 +03:00
|
|
|
}
|
|
|
|
|
2010-04-22 21:03:54 +04:00
|
|
|
/**
|
2011-04-15 19:14:02 +04:00
|
|
|
* @brief Check if the user is logged in
|
2011-04-18 12:41:01 +04:00
|
|
|
* @returns true/false
|
|
|
|
*
|
|
|
|
* Checks if the user is logged in
|
2010-07-23 01:42:18 +04:00
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function isLoggedIn() {
|
2012-06-04 23:31:35 +04:00
|
|
|
if( isset($_SESSION['user_id']) AND $_SESSION['user_id']) {
|
|
|
|
OC_App::loadApps(array('authentication'));
|
2012-09-01 04:48:54 +04:00
|
|
|
self::setupBackends();
|
2012-09-07 17:22:01 +04:00
|
|
|
if (self::userExists($_SESSION['user_id']) ) {
|
2012-07-05 17:55:46 +04:00
|
|
|
return true;
|
2012-06-04 23:31:35 +04:00
|
|
|
}
|
2011-06-21 21:28:46 +04:00
|
|
|
}
|
2012-07-05 17:55:46 +04:00
|
|
|
return false;
|
2010-07-21 19:53:51 +04:00
|
|
|
}
|
2010-07-19 20:52:49 +04:00
|
|
|
|
2013-01-14 22:45:17 +04:00
|
|
|
/**
|
|
|
|
* @brief Check if the user is an admin user
|
|
|
|
* @param $uid uid of the admin
|
|
|
|
* @returns bool
|
|
|
|
*/
|
|
|
|
public static function isAdminUser($uid) {
|
|
|
|
if(OC_Group::inGroup($uid, 'admin' )) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-22 14:50:57 +04:00
|
|
|
/**
|
2012-04-30 15:08:08 +04:00
|
|
|
* @brief get the user id of the user currently logged in.
|
2011-06-22 14:50:57 +04:00
|
|
|
* @return string uid or false
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function getUser() {
|
|
|
|
if( isset($_SESSION['user_id']) AND $_SESSION['user_id'] ) {
|
2011-06-22 14:50:57 +04:00
|
|
|
return $_SESSION['user_id'];
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 23:42:21 +04:00
|
|
|
/**
|
|
|
|
* @brief get the display name of the user currently logged in.
|
|
|
|
* @return string uid or false
|
|
|
|
*/
|
2013-01-25 16:00:17 +04:00
|
|
|
public static function getDisplayName($user=null) {
|
|
|
|
if ( $user ) {
|
|
|
|
return self::determineDisplayName($user);
|
2013-01-29 23:42:21 +04:00
|
|
|
} else if( isset($_SESSION['display_name']) AND $_SESSION['display_name'] ) {
|
|
|
|
return $_SESSION['display_name'];
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-24 16:07:59 +04:00
|
|
|
}
|
2013-01-29 23:42:21 +04:00
|
|
|
|
2010-04-22 21:03:54 +04:00
|
|
|
/**
|
2011-04-18 12:41:01 +04:00
|
|
|
* @brief Autogenerate a password
|
|
|
|
* @returns string
|
|
|
|
*
|
|
|
|
* generates a password
|
2010-07-23 01:42:18 +04:00
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function generatePassword() {
|
2013-04-04 02:27:13 +04:00
|
|
|
return OC_Util::generate_random_bytes(30);
|
2010-07-21 19:53:51 +04:00
|
|
|
}
|
2010-07-19 20:52:49 +04:00
|
|
|
|
2010-04-22 21:03:54 +04:00
|
|
|
/**
|
2011-04-18 12:41:01 +04:00
|
|
|
* @brief Set password
|
|
|
|
* @param $uid The username
|
|
|
|
* @param $password The new password
|
|
|
|
* @returns true/false
|
|
|
|
*
|
|
|
|
* Change the password of a user
|
2010-07-23 01:42:18 +04:00
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function setPassword( $uid, $password ) {
|
2011-04-18 13:39:29 +04:00
|
|
|
$run = true;
|
2011-07-29 23:36:03 +04:00
|
|
|
OC_Hook::emit( "OC_User", "pre_setPassword", array( "run" => &$run, "uid" => $uid, "password" => $password ));
|
2011-04-18 13:39:29 +04:00
|
|
|
|
2012-09-07 17:22:01 +04:00
|
|
|
if( $run ) {
|
2012-05-01 15:40:01 +04:00
|
|
|
$success = false;
|
2012-09-07 17:22:01 +04:00
|
|
|
foreach(self::$_usedBackends as $backend) {
|
|
|
|
if($backend->implementsActions(OC_USER_BACKEND_SET_PASSWORD)) {
|
|
|
|
if($backend->userExists($uid)) {
|
2012-11-02 22:53:02 +04:00
|
|
|
$success |= $backend->setPassword($uid, $password);
|
2011-06-23 20:23:06 +04:00
|
|
|
}
|
2011-06-21 21:28:46 +04:00
|
|
|
}
|
|
|
|
}
|
2012-10-11 13:54:40 +04:00
|
|
|
// invalidate all login cookies
|
|
|
|
OC_Preferences::deleteApp($uid, 'login_token');
|
2011-07-29 23:36:03 +04:00
|
|
|
OC_Hook::emit( "OC_User", "post_setPassword", array( "uid" => $uid, "password" => $password ));
|
2012-05-01 15:40:01 +04:00
|
|
|
return $success;
|
2011-04-18 13:39:29 +04:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
return false;
|
|
|
|
}
|
2010-07-21 19:53:51 +04:00
|
|
|
}
|
2010-07-19 20:52:49 +04:00
|
|
|
|
2013-02-05 17:58:35 +04:00
|
|
|
/**
|
|
|
|
* @brief Check whether user can change his password
|
|
|
|
* @param $uid The username
|
|
|
|
* @returns true/false
|
|
|
|
*
|
|
|
|
* Check whether a specified user can change his password
|
|
|
|
*/
|
|
|
|
public static function canUserChangePassword($uid) {
|
|
|
|
foreach(self::$_usedBackends as $backend) {
|
|
|
|
if($backend->implementsActions(OC_USER_BACKEND_SET_PASSWORD)) {
|
|
|
|
if($backend->userExists($uid)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-22 20:21:57 +04:00
|
|
|
|
2013-02-06 14:38:03 +04:00
|
|
|
/**
|
|
|
|
* @brief Check whether user can change his display name
|
|
|
|
* @param $uid The username
|
|
|
|
* @returns true/false
|
|
|
|
*
|
|
|
|
* Check whether a specified user can change his display name
|
|
|
|
*/
|
|
|
|
public static function canUserChangeDisplayName($uid) {
|
2013-02-10 17:43:31 +04:00
|
|
|
if (OC_Config::getValue('allow_user_to_change_display_name', true)) {
|
|
|
|
foreach(self::$_usedBackends as $backend) {
|
|
|
|
if($backend->implementsActions(OC_USER_BACKEND_SET_DISPLAYNAME)) {
|
|
|
|
if($backend->userExists($uid)) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-02-06 14:38:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-05 17:58:35 +04:00
|
|
|
|
2010-04-22 21:03:54 +04:00
|
|
|
/**
|
2011-04-18 12:41:01 +04:00
|
|
|
* @brief Check if the password is correct
|
|
|
|
* @param $uid The username
|
|
|
|
* @param $password The password
|
2012-05-17 02:57:43 +04:00
|
|
|
* @returns string
|
2011-04-18 12:41:01 +04:00
|
|
|
*
|
|
|
|
* Check if the password is correct without logging in the user
|
2012-05-17 02:57:43 +04:00
|
|
|
* returns the user id or false
|
2010-07-23 01:42:18 +04:00
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function checkPassword( $uid, $password ) {
|
|
|
|
foreach(self::$_usedBackends as $backend) {
|
|
|
|
if($backend->implementsActions(OC_USER_BACKEND_CHECK_PASSWORD)) {
|
2011-06-23 20:23:06 +04:00
|
|
|
$result=$backend->checkPassword( $uid, $password );
|
2012-09-07 17:22:01 +04:00
|
|
|
if($result) {
|
2011-08-25 23:51:04 +04:00
|
|
|
return $result;
|
2011-06-23 20:23:06 +04:00
|
|
|
}
|
2011-06-21 21:28:46 +04:00
|
|
|
}
|
|
|
|
}
|
2010-07-21 19:53:51 +04:00
|
|
|
}
|
2010-07-15 21:10:20 +04:00
|
|
|
|
2012-08-26 18:24:25 +04:00
|
|
|
/**
|
|
|
|
* @brief Check if the password is correct
|
2013-01-28 22:08:03 +04:00
|
|
|
* @param string $uid The username
|
|
|
|
* @param string $password The password
|
2012-08-26 18:24:25 +04:00
|
|
|
* @returns string
|
|
|
|
*
|
2012-10-27 17:23:35 +04:00
|
|
|
* returns the path to the users home directory
|
2012-08-26 18:24:25 +04:00
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function getHome($uid) {
|
|
|
|
foreach(self::$_usedBackends as $backend) {
|
2013-02-06 05:26:35 +04:00
|
|
|
if($backend->implementsActions(OC_USER_BACKEND_GET_HOME) && $backend->userExists($uid)) {
|
2012-08-26 18:24:25 +04:00
|
|
|
$result=$backend->getHome($uid);
|
2012-09-07 17:22:01 +04:00
|
|
|
if($result) {
|
2012-08-26 18:24:25 +04:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-26 23:57:05 +04:00
|
|
|
return OC_Config::getValue( "datadirectory", OC::$SERVERROOT."/data" ) . '/' . $uid;
|
2012-08-26 18:24:25 +04:00
|
|
|
}
|
|
|
|
|
2010-09-15 20:24:14 +04:00
|
|
|
/**
|
2011-04-18 12:41:01 +04:00
|
|
|
* @brief Get a list of all users
|
|
|
|
* @returns array with all uids
|
|
|
|
*
|
|
|
|
* Get a list of all users.
|
2010-09-15 20:24:14 +04:00
|
|
|
*/
|
2012-08-25 02:05:07 +04:00
|
|
|
public static function getUsers($search = '', $limit = null, $offset = null) {
|
2012-07-31 04:20:46 +04:00
|
|
|
$users = array();
|
|
|
|
foreach (self::$_usedBackends as $backend) {
|
|
|
|
$backendUsers = $backend->getUsers($search, $limit, $offset);
|
|
|
|
if (is_array($backendUsers)) {
|
|
|
|
$users = array_merge($users, $backendUsers);
|
2011-06-21 21:28:46 +04:00
|
|
|
}
|
|
|
|
}
|
2012-06-01 16:21:24 +04:00
|
|
|
asort($users);
|
2011-06-21 21:28:46 +04:00
|
|
|
return $users;
|
|
|
|
}
|
|
|
|
|
2013-01-29 23:42:21 +04:00
|
|
|
/**
|
|
|
|
* @brief Get a list of all users display name
|
|
|
|
* @returns associative array with all display names (value) and corresponding uids (key)
|
|
|
|
*
|
|
|
|
* Get a list of all display names and user ids.
|
|
|
|
*/
|
|
|
|
public static function getDisplayNames($search = '', $limit = null, $offset = null) {
|
|
|
|
$displayNames = array();
|
|
|
|
foreach (self::$_usedBackends as $backend) {
|
|
|
|
$backendDisplayNames = $backend->getDisplayNames($search, $limit, $offset);
|
|
|
|
if (is_array($backendDisplayNames)) {
|
2013-05-01 03:38:06 +04:00
|
|
|
$displayNames = array_merge($displayNames, $backendDisplayNames);
|
2013-01-29 23:42:21 +04:00
|
|
|
}
|
|
|
|
}
|
2013-02-27 01:56:55 +04:00
|
|
|
asort($displayNames);
|
2013-01-29 23:42:21 +04:00
|
|
|
return $displayNames;
|
2013-01-25 14:05:00 +04:00
|
|
|
}
|
2011-06-21 21:28:46 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief check if a user exists
|
|
|
|
* @param string $uid the username
|
2012-12-06 21:09:47 +04:00
|
|
|
* @param string $excludingBackend (default none)
|
2011-06-21 21:28:46 +04:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-12-06 21:09:47 +04:00
|
|
|
public static function userExists($uid, $excludingBackend=null) {
|
2012-09-07 17:22:01 +04:00
|
|
|
foreach(self::$_usedBackends as $backend) {
|
2013-02-09 20:35:47 +04:00
|
|
|
if (!is_null($excludingBackend) && !strcmp(get_class($backend), $excludingBackend)) {
|
2013-02-10 01:44:11 +04:00
|
|
|
OC_Log::write('OC_User', $excludingBackend . 'excluded from user existance check.', OC_Log::DEBUG);
|
|
|
|
continue;
|
2012-12-06 21:09:47 +04:00
|
|
|
}
|
2012-05-08 11:07:11 +04:00
|
|
|
$result=$backend->userExists($uid);
|
2012-09-07 17:22:01 +04:00
|
|
|
if($result===true) {
|
2012-07-09 12:31:50 +04:00
|
|
|
return true;
|
2011-06-21 21:28:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2010-09-15 20:24:14 +04:00
|
|
|
}
|
2012-08-29 10:38:33 +04:00
|
|
|
|
2013-02-08 20:16:18 +04:00
|
|
|
public static function userExistsForCreation($uid) {
|
|
|
|
foreach(self::$_usedBackends as $backend) {
|
2013-02-12 01:01:52 +04:00
|
|
|
if(!$backend->hasUserListings())
|
2013-02-08 20:16:18 +04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
$result=$backend->userExists($uid);
|
|
|
|
if($result===true) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-29 20:07:51 +04:00
|
|
|
/**
|
|
|
|
* disables a user
|
|
|
|
* @param string $userid the user to disable
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function disableUser($userid) {
|
2012-09-12 14:47:18 +04:00
|
|
|
$sql = "INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, `configkey`, `configvalue`) VALUES(?, ?, ?, ?)";
|
|
|
|
$stmt = OC_DB::prepare($sql);
|
|
|
|
if ( ! OC_DB::isError($stmt) ) {
|
|
|
|
$result = $stmt->execute(array($userid, 'core', 'enabled', 'false'));
|
|
|
|
if ( OC_DB::isError($result) ) {
|
|
|
|
OC_Log::write('OC_User', 'could not enable user: '. OC_DB::getErrorMessage($result), OC_Log::ERROR);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
OC_Log::write('OC_User', 'could not disable user: '. OC_DB::getErrorMessage($stmt), OC_Log::ERROR);
|
|
|
|
}
|
2012-07-29 20:07:51 +04:00
|
|
|
}
|
2012-08-29 10:38:33 +04:00
|
|
|
|
2012-07-29 20:07:51 +04:00
|
|
|
/**
|
|
|
|
* enable a user
|
|
|
|
* @param string $userid
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function enableUser($userid) {
|
2013-02-26 23:48:01 +04:00
|
|
|
$sql = 'DELETE FROM `*PREFIX*preferences`'
|
|
|
|
." WHERE `userid` = ? AND `appid` = ? AND `configkey` = ? AND `configvalue` = ?";
|
2012-09-12 14:47:18 +04:00
|
|
|
$stmt = OC_DB::prepare($sql);
|
|
|
|
if ( ! OC_DB::isError($stmt) ) {
|
|
|
|
$result = $stmt->execute(array($userid, 'core', 'enabled', 'false'));
|
|
|
|
if ( OC_DB::isError($result) ) {
|
|
|
|
OC_Log::write('OC_User', 'could not enable user: '. OC_DB::getErrorMessage($result), OC_Log::ERROR);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
OC_Log::write('OC_User', 'could not enable user: '. OC_DB::getErrorMessage($stmt), OC_Log::ERROR);
|
|
|
|
}
|
2012-07-29 20:07:51 +04:00
|
|
|
}
|
2012-08-29 10:38:33 +04:00
|
|
|
|
2012-07-29 20:07:51 +04:00
|
|
|
/**
|
|
|
|
* checks if a user is enabled
|
|
|
|
* @param string $userid
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function isEnabled($userid) {
|
2013-02-15 06:20:02 +04:00
|
|
|
$sql = 'SELECT `userid` FROM `*PREFIX*preferences`'
|
|
|
|
.' WHERE `userid` = ? AND `appid` = ? AND `configkey` = ? AND `configvalue` = ?';
|
2012-09-12 14:47:18 +04:00
|
|
|
$stmt = OC_DB::prepare($sql);
|
|
|
|
if ( ! OC_DB::isError($stmt) ) {
|
|
|
|
$result = $stmt->execute(array($userid, 'core', 'enabled', 'false'));
|
|
|
|
if ( ! OC_DB::isError($result) ) {
|
|
|
|
return $result->numRows() ? false : true;
|
|
|
|
} else {
|
2013-02-11 20:44:02 +04:00
|
|
|
OC_Log::write('OC_User',
|
|
|
|
'could not check if enabled: '. OC_DB::getErrorMessage($result),
|
|
|
|
OC_Log::ERROR);
|
2012-09-12 14:47:18 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
OC_Log::write('OC_User', 'could not check if enabled: '. OC_DB::getErrorMessage($stmt), OC_Log::ERROR);
|
|
|
|
}
|
|
|
|
return false;
|
2012-07-29 20:07:51 +04:00
|
|
|
}
|
2011-07-20 17:04:14 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set cookie value to use in next page load
|
|
|
|
* @param string $username username to be set
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function setMagicInCookie($username, $token) {
|
2011-09-23 15:52:10 +04:00
|
|
|
$secure_cookie = OC_Config::getValue("forcessl", false);
|
2012-09-05 19:33:15 +04:00
|
|
|
$expires = time() + OC_Config::getValue('remember_login_cookie_lifetime', 60*60*24*15);
|
2013-04-06 02:16:52 +04:00
|
|
|
setcookie("oc_username", $username, $expires, OC::$WEBROOT, '', $secure_cookie);
|
|
|
|
setcookie("oc_token", $token, $expires, OC::$WEBROOT, '', $secure_cookie, true);
|
|
|
|
setcookie("oc_remember_login", true, $expires, OC::$WEBROOT, '', $secure_cookie);
|
2011-07-20 17:04:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Remove cookie for "remember username"
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function unsetMagicInCookie() {
|
2011-09-18 11:15:30 +04:00
|
|
|
unset($_COOKIE["oc_username"]);
|
2011-09-18 17:05:53 +04:00
|
|
|
unset($_COOKIE["oc_token"]);
|
2011-09-18 11:15:30 +04:00
|
|
|
unset($_COOKIE["oc_remember_login"]);
|
2012-10-23 10:01:09 +04:00
|
|
|
setcookie("oc_username", null, -1);
|
|
|
|
setcookie("oc_token", null, -1);
|
|
|
|
setcookie("oc_remember_login", null, -1);
|
2011-07-20 17:04:14 +04:00
|
|
|
}
|
2010-04-22 21:03:54 +04:00
|
|
|
}
|