diff options
Diffstat (limited to 'lib/user.php')
-rw-r--r-- | lib/user.php | 129 |
1 files changed, 77 insertions, 52 deletions
diff --git a/lib/user.php b/lib/user.php index c14ef2d6ca6..89dab9f9647 100644 --- a/lib/user.php +++ b/lib/user.php @@ -40,6 +40,8 @@ class OC_User { // The backend used for user management private static $_usedBackends = array(); + private static $_setupedBackends = array(); + // Backends available (except database) private static $_backends = array(); @@ -50,7 +52,7 @@ class OC_User { * * Makes a list of backends that can be used by other modules */ - public static function registerBackend( $backend ){ + public static function registerBackend( $backend ) { self::$_backends[] = $backend; return true; } @@ -61,7 +63,7 @@ class OC_User { * * Returns the names of all backends. */ - public static function getBackends(){ + public static function getBackends() { return self::$_backends; } @@ -71,7 +73,7 @@ class OC_User { * * Returns the names of all used backends. */ - public static function getUsedBackends(){ + public static function getUsedBackends() { return array_keys(self::$_usedBackends); } @@ -82,17 +84,17 @@ class OC_User { * * Set the User Authentication Module */ - public static function useBackend( $backend = 'database' ){ - if($backend instanceof OC_User_Interface){ + public static function useBackend( $backend = 'database' ) { + if($backend instanceof OC_User_Interface) { self::$_usedBackends[get_class($backend)]=$backend; }else{ // You'll never know what happens - if( null === $backend OR !is_string( $backend )){ + if( null === $backend OR !is_string( $backend )) { $backend = 'database'; } // Load backend - switch( $backend ){ + switch( $backend ) { case 'database': case 'mysql': case 'sqlite': @@ -110,11 +112,33 @@ class OC_User { /** * remove all used backends */ - public static function clearBackends(){ + public static function clearBackends() { self::$_usedBackends=array(); } /** + * setup the configured backends in config.php + */ + public static function setupBackends() { + $backends=OC_Config::getValue('user_backends',array()); + foreach($backends as $i=>$config) { + $class=$config['class']; + $arguments=$config['arguments']; + if(class_exists($class) and array_search($i,self::$_setupedBackends)===false) { + // make a reflection object + $reflectionObj = new ReflectionClass($class); + + // 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.' not found.',OC_Log::ERROR); + } + } + } + + /** * @brief Create a new user * @param $uid The username of the user to create * @param $password The password of the new user @@ -125,23 +149,23 @@ class OC_User { * * Allowed characters in the username are: "a-z", "A-Z", "0-9" and "_.@-" */ - public static function createUser( $uid, $password ){ + public static function createUser( $uid, $password ) { // Check the name for bad characters // Allowed are: "a-z", "A-Z", "0-9" and "_.@-" - if( preg_match( '/[^a-zA-Z0-9 _\.@\-]/', $uid )){ + if( preg_match( '/[^a-zA-Z0-9 _\.@\-]/', $uid )) { throw new Exception('Only the following characters are allowed in a username: "a-z", "A-Z", "0-9", and "_.@-"'); } // No empty username - if(trim($uid) == ''){ + if(trim($uid) == '') { throw new Exception('A valid username must be provided'); } // No empty password - if(trim($password) == ''){ + if(trim($password) == '') { throw new Exception('A valid password must be provided'); } // Check if user already exists - if( self::userExists($uid) ){ + if( self::userExists($uid) ) { throw new Exception('The username is already being used'); } @@ -149,9 +173,9 @@ class OC_User { $run = true; OC_Hook::emit( "OC_User", "pre_createUser", array( "run" => &$run, "uid" => $uid, "password" => $password )); - if( $run ){ + if( $run ) { //create the user in the first backend that supports creating users - foreach(self::$_usedBackends as $backend){ + foreach(self::$_usedBackends as $backend) { if(!$backend->implementsActions(OC_USER_BACKEND_CREATE_USER)) continue; @@ -171,17 +195,17 @@ class OC_User { * * Deletes a user */ - public static function deleteUser( $uid ){ + public static function deleteUser( $uid ) { $run = true; OC_Hook::emit( "OC_User", "pre_deleteUser", array( "run" => &$run, "uid" => $uid )); - if( $run ){ + if( $run ) { //delete the user from all backends - foreach(self::$_usedBackends as $backend){ + foreach(self::$_usedBackends as $backend) { $backend->deleteUser($uid); } // We have to delete the user from all groups - foreach( OC_Group::getUserGroups( $uid ) as $i ){ + foreach( OC_Group::getUserGroups( $uid ) as $i ) { OC_Group::removeFromGroup( $uid, $i ); } // Delete the user's keys in preferences @@ -203,14 +227,14 @@ class OC_User { * * Log in a user and regenerate a new session - if the password is ok */ - public static function login( $uid, $password ){ + public static function login( $uid, $password ) { $run = true; OC_Hook::emit( "OC_User", "pre_login", array( "run" => &$run, "uid" => $uid )); - if( $run ){ + if( $run ) { $uid = self::checkPassword( $uid, $password ); $enabled = self::isEnabled($uid); - if($uid && $enabled){ + if($uid && $enabled) { session_regenerate_id(true); self::setUserId($uid); OC_Hook::emit( "OC_User", "post_login", array( "uid" => $uid, 'password'=>$password )); @@ -236,7 +260,7 @@ class OC_User { * * Logout, destroys session */ - public static function logout(){ + public static function logout() { OC_Hook::emit( "OC_User", "logout", array()); session_unset(); session_destroy(); @@ -250,10 +274,11 @@ class OC_User { * * Checks if the user is logged in */ - public static function isLoggedIn(){ + public static function isLoggedIn() { if( isset($_SESSION['user_id']) AND $_SESSION['user_id']) { OC_App::loadApps(array('authentication')); - if (self::userExists($_SESSION['user_id']) ){ + self::setupBackends(); + if (self::userExists($_SESSION['user_id']) ) { return true; } } @@ -264,8 +289,8 @@ class OC_User { * @brief get the user id of the user currently logged in. * @return string uid or false */ - public static function getUser(){ - if( isset($_SESSION['user_id']) AND $_SESSION['user_id'] ){ + public static function getUser() { + if( isset($_SESSION['user_id']) AND $_SESSION['user_id'] ) { return $_SESSION['user_id']; } else{ @@ -279,7 +304,7 @@ class OC_User { * * generates a password */ - public static function generatePassword(){ + public static function generatePassword() { return uniqId(); } @@ -291,15 +316,15 @@ class OC_User { * * Change the password of a user */ - public static function setPassword( $uid, $password ){ + public static function setPassword( $uid, $password ) { $run = true; OC_Hook::emit( "OC_User", "pre_setPassword", array( "run" => &$run, "uid" => $uid, "password" => $password )); - if( $run ){ + if( $run ) { $success = false; - foreach(self::$_usedBackends as $backend){ - if($backend->implementsActions(OC_USER_BACKEND_SET_PASSWORD)){ - if($backend->userExists($uid)){ + foreach(self::$_usedBackends as $backend) { + if($backend->implementsActions(OC_USER_BACKEND_SET_PASSWORD)) { + if($backend->userExists($uid)) { $success |= $backend->setPassword($uid,$password); } } @@ -321,11 +346,11 @@ class OC_User { * Check if the password is correct without logging in the user * returns the user id or false */ - public static function checkPassword( $uid, $password ){ - foreach(self::$_usedBackends as $backend){ - if($backend->implementsActions(OC_USER_BACKEND_CHECK_PASSWORD)){ + public static function checkPassword( $uid, $password ) { + foreach(self::$_usedBackends as $backend) { + if($backend->implementsActions(OC_USER_BACKEND_CHECK_PASSWORD)) { $result=$backend->checkPassword( $uid, $password ); - if($result){ + if($result) { return $result; } } @@ -341,11 +366,11 @@ class OC_User { * Check if the password is correct without logging in the user * returns the user id or false */ - public static function getHome($uid){ - foreach(self::$_usedBackends as $backend){ - if($backend->implementsActions(OC_USER_BACKEND_GET_HOME)){ + public static function getHome($uid) { + foreach(self::$_usedBackends as $backend) { + if($backend->implementsActions(OC_USER_BACKEND_GET_HOME)) { $result=$backend->getHome($uid); - if($result){ + if($result) { return $result; } } @@ -376,42 +401,42 @@ class OC_User { * @param string $uid the username * @return boolean */ - public static function userExists($uid){ - foreach(self::$_usedBackends as $backend){ + public static function userExists($uid) { + foreach(self::$_usedBackends as $backend) { $result=$backend->userExists($uid); - if($result===true){ + if($result===true) { return true; } } return false; } - + /** * disables a user * @param string $userid the user to disable */ - public static function disableUser($userid){ + public static function disableUser($userid) { $query = "INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, `configkey`, `configvalue`) VALUES(?, ?, ?, ?)"; $query = OC_DB::prepare($query); $query->execute(array($userid, 'core', 'enabled', 'false')); } - + /** * enable a user * @param string $userid */ - public static function enableUser($userid){ + public static function enableUser($userid) { $query = "DELETE FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ? AND `configvalue` = ?"; $query = OC_DB::prepare($query); $query->execute(array($userid, 'core', 'enabled', 'false')); } - + /** * checks if a user is enabled * @param string $userid * @return bool */ - public static function isEnabled($userid){ + public static function isEnabled($userid) { $query = "SELECT `userid` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ? AND `configvalue` = ?"; $query = OC_DB::prepare($query); $results = $query->execute(array($userid, 'core', 'enabled', 'false')); @@ -422,7 +447,7 @@ class OC_User { * @brief Set cookie value to use in next page load * @param string $username username to be set */ - public static function setMagicInCookie($username, $token){ + public static function setMagicInCookie($username, $token) { $secure_cookie = OC_Config::getValue("forcessl", false); setcookie("oc_username", $username, time()+60*60*24*15, '', '', $secure_cookie); setcookie("oc_token", $token, time()+60*60*24*15, '', '', $secure_cookie); @@ -432,7 +457,7 @@ class OC_User { /** * @brief Remove cookie for "remember username" */ - public static function unsetMagicInCookie(){ + public static function unsetMagicInCookie() { unset($_COOKIE["oc_username"]); unset($_COOKIE["oc_token"]); unset($_COOKIE["oc_remember_login"]); |