summaryrefslogtreecommitdiffstats
path: root/lib/user.php
diff options
context:
space:
mode:
authorRobin Appelman <icewind1991@gmail.com>2011-06-21 19:28:46 +0200
committerRobin Appelman <icewind1991@gmail.com>2011-06-21 19:28:46 +0200
commitee0f1490e1872cbe6071f5758e292ae1646ab1af (patch)
treedc846299da889899d508e856c2926f5c5970e74a /lib/user.php
parentc36b8621b4e91f01e979d62d3af2075e5e5630ee (diff)
downloadnextcloud-server-ee0f1490e1872cbe6071f5758e292ae1646ab1af.tar.gz
nextcloud-server-ee0f1490e1872cbe6071f5758e292ae1646ab1af.zip
Multiply changes to user system
keeping tracked of the logged in user is no longer done by the active backend but by oc_user directly instead multiply backends can be active at the same time, allowing alternative authentication procedures like openid or tokens to be used next to the regular user system
Diffstat (limited to 'lib/user.php')
-rw-r--r--lib/user.php99
1 files changed, 79 insertions, 20 deletions
diff --git a/lib/user.php b/lib/user.php
index 09501e59c58..2e11a30e85e 100644
--- a/lib/user.php
+++ b/lib/user.php
@@ -40,7 +40,7 @@ if( !OC_CONFIG::getValue( "installed", false )){
*/
class OC_USER {
// The backend used for user management
- private static $_backend = null;
+ private static $_usedBackends = array();
// Backends available (except database)
private static $_backends = array();
@@ -66,15 +66,25 @@ class OC_USER {
public static function getBackends(){
return self::$_backends;
}
+
+ /**
+ * @brief gets used backends
+ * @returns array of backends
+ *
+ * Returns the names of all used backends.
+ */
+ public static function getUsedBackends(){
+ return array_keys(self::$_usedBackends);
+ }
/**
- * @brief Sets the backend
+ * @brief Adds the backend to the list of used backends
* @param $backend default: database The backend to use for user managment
* @returns true/false
*
* Set the User Authentication Module
*/
- public static function setBackend( $backend = 'database' ){
+ public static function useBackend( $backend = 'database' ){
// You'll never know what happens
if( null === $backend OR !is_string( $backend )){
$backend = 'database';
@@ -86,11 +96,11 @@ class OC_USER {
case 'mysql':
case 'sqlite':
require_once('User/database.php');
- self::$_backend = new OC_USER_DATABASE();
+ self::$_usedBackends[$backend] = new OC_USER_DATABASE();
break;
default:
$className = 'OC_USER_' . strToUpper($backend);
- self::$_backend = new $className();
+ self::$_usedBackends[$backend] = new $className();
break;
}
@@ -119,7 +129,7 @@ class OC_USER {
return false;
}
// Check if user already exists
- if( in_array( $uid, self::getUsers())){
+ if( self::userExists($uid) ){
return false;
}
@@ -127,13 +137,17 @@ class OC_USER {
$run = true;
OC_HOOK::emit( "OC_USER", "pre_createUser", array( "run" => &$run, "uid" => $uid, "password" => $password ));
- if( $run && self::$_backend->createUser( $uid, $password )){
- OC_HOOK::emit( "OC_USER", "post_createUser", array( "uid" => $uid, "password" => $password ));
- return true;
- }
- else{
- return false;
+ if( $run ){
+ //create the user in the first backend that supports creating users
+ foreach(self::$_usedBackends as $backend){
+ $result=$backend->createUser($uid,$password);
+ if($result!==OC_USER_BACKEND_NOT_IMPLEMENTED){
+ OC_HOOK::emit( "OC_USER", "post_createUser", array( "uid" => $uid, "password" => $password ));
+ return true;
+ }
+ }
}
+ return false;
}
/**
@@ -147,7 +161,11 @@ class OC_USER {
$run = true;
OC_HOOK::emit( "OC_USER", "pre_deleteUser", array( "run" => &$run, "uid" => $uid ));
- if( $run && self::$_backend->deleteUser( $uid )){
+ if( $run ){
+ //delete the user from all backends
+ foreach(self::$_usedBackends as $backend){
+ $backend->deleteUser($uid);
+ }
// We have to delete the user from all groups
foreach( OC_GROUP::getUserGroups( $uid ) as $i ){
OC_GROUP::removeFromGroup( $uid, $i );
@@ -174,7 +192,9 @@ class OC_USER {
$run = true;
OC_HOOK::emit( "OC_USER", "pre_login", array( "run" => &$run, "uid" => $uid ));
- if( $run && self::$_backend->login( $uid, $password )){
+ if( $run && self::checkPassword( $uid, $password )){
+ $_SESSION['user_id'] = $uid;
+ OC_LOG::add( "core", $_SESSION['user_id'], "login" );
OC_HOOK::emit( "OC_USER", "post_login", array( "uid" => $uid ));
return true;
}
@@ -191,7 +211,9 @@ class OC_USER {
*/
public static function logout(){
OC_HOOK::emit( "OC_USER", "logout", array());
- return self::$_backend->logout();
+ OC_LOG::add( "core", $_SESSION['user_id'], "logout" );
+ $_SESSION['user_id'] = false;
+ return true;
}
/**
@@ -201,7 +223,12 @@ class OC_USER {
* Checks if the user is logged in
*/
public static function isLoggedIn(){
- return self::$_backend->isLoggedIn();
+ if( isset($_SESSION['user_id']) AND $_SESSION['user_id'] ){
+ return true;
+ }
+ else{
+ return false;
+ }
}
/**
@@ -211,7 +238,7 @@ class OC_USER {
* generates a password
*/
public static function generatePassword(){
- return substr( md5( uniqId().time()), 0, 10 );
+ return uniqId();
}
/**
@@ -226,7 +253,12 @@ class OC_USER {
$run = true;
OC_HOOK::emit( "OC_USER", "pre_setPassword", array( "run" => &$run, "uid" => $uid, "password" => $password ));
- if( $run && self::$_backend->setPassword( $uid, $password )){
+ if( $run ){
+ foreach(self::$_usedBackends as $backend){
+ if($backend->userExists($uid)){
+ $backend->setPassword($uid,$password);
+ }
+ }
OC_HOOK::emit( "OC_USER", "post_setPassword", array( "uid" => $uid, "password" => $password ));
return true;
}
@@ -244,7 +276,12 @@ class OC_USER {
* Check if the password is correct without logging in the user
*/
public static function checkPassword( $uid, $password ){
- return self::$_backend->checkPassword( $uid, $password );
+ foreach(self::$_usedBackends as $backend){
+ $result=$backend->checkPassword( $uid, $password );
+ if($result===true){
+ return true;
+ }
+ }
}
/**
@@ -254,6 +291,28 @@ class OC_USER {
* Get a list of all users.
*/
public static function getUsers(){
- return self::$_backend->getUsers();
+ $users=array();
+ foreach(self::$_usedBackends as $backend){
+ $result=$backend->getUsers();
+ if($result!=OC_USER_BACKEND_NOT_IMPLEMENTED){
+ $users=array_merge($users,$result);
+ }
+ }
+ return $users;
+ }
+
+ /**
+ * @brief check if a user exists
+ * @param string $uid the username
+ * @return boolean
+ */
+ public static function userExists($uid){
+ foreach(self::$_usedBackends as $backend){
+ $result=$backend->userExists($uid);
+ if($result===true){
+ return true;
+ }
+ }
+ return false;
}
}