diff options
Diffstat (limited to 'lib/user')
-rw-r--r-- | lib/user/backend.php | 50 | ||||
-rw-r--r-- | lib/user/database.php | 33 | ||||
-rw-r--r-- | lib/user/dummy.php | 4 | ||||
-rw-r--r-- | lib/user/example.php | 44 | ||||
-rw-r--r-- | lib/user/http.php | 93 | ||||
-rw-r--r-- | lib/user/interface.php | 60 |
6 files changed, 212 insertions, 72 deletions
diff --git a/lib/user/backend.php b/lib/user/backend.php index 4afdf152150..f67908cdac0 100644 --- a/lib/user/backend.php +++ b/lib/user/backend.php @@ -5,7 +5,7 @@ * * @author Frank Karlitschek * @author Dominik Schmidt - * @copyright 2010 Frank Karlitschek karlitschek@kde.org + * @copyright 2012 Frank Karlitschek frank@owncloud.org * @copyright 2011 Dominik Schmidt dev@dominik-schmidt.de * * This library is free software; you can redistribute it and/or @@ -32,26 +32,22 @@ define('OC_USER_BACKEND_NOT_IMPLEMENTED', -501); * actions that user backends can define */ define('OC_USER_BACKEND_CREATE_USER', 0x000001); -define('OC_USER_BACKEND_DELETE_USER', 0x000010); -define('OC_USER_BACKEND_SET_PASSWORD', 0x000100); -define('OC_USER_BACKEND_CHECK_PASSWORD', 0x001000); -define('OC_USER_BACKEND_GET_USERS', 0x010000); -define('OC_USER_BACKEND_USER_EXISTS', 0x100000); +define('OC_USER_BACKEND_SET_PASSWORD', 0x000010); +define('OC_USER_BACKEND_CHECK_PASSWORD', 0x000100); /** - * abstract base class for user management - * subclass this for your own backends and see OC_User_Example for descriptions + * Abstract base class for user management. Provides methods for querying backend + * capabilities. + * + * Subclass this for your own backends, and see OC_User_Example for descriptions */ -abstract class OC_User_Backend { +abstract class OC_User_Backend implements OC_User_Interface { protected $possibleActions = array( OC_USER_BACKEND_CREATE_USER => 'createUser', - OC_USER_BACKEND_DELETE_USER => 'deleteUser', OC_USER_BACKEND_SET_PASSWORD => 'setPassword', OC_USER_BACKEND_CHECK_PASSWORD => 'checkPassword', - OC_USER_BACKEND_GET_USERS => 'getUsers', - OC_USER_BACKEND_USER_EXISTS => 'userExists' ); /** @@ -83,4 +79,34 @@ abstract class OC_User_Backend { public function implementsActions($actions){ return (bool)($this->getSupportedActions() & $actions); } + + /** + * @brief delete a user + * @param $uid The username of the user to delete + * @returns true/false + * + * Deletes a user + */ + public function deleteUser( $uid ){ + return false; + } + + /** + * @brief Get a list of all users + * @returns array with all uids + * + * Get a list of all users. + */ + public function getUsers($search = '', $limit = -1, $offset = 0) { + return array(); + } + + /** + * @brief check if a user exists + * @param string $uid the username + * @return boolean + */ + public function userExists($uid){ + return false; + } } diff --git a/lib/user/database.php b/lib/user/database.php index 5464a4abfac..1746c4fe0c0 100644 --- a/lib/user/database.php +++ b/lib/user/database.php @@ -4,7 +4,7 @@ * ownCloud * * @author Frank Karlitschek - * @copyright 2010 Frank Karlitschek karlitschek@kde.org + * @copyright 2012 Frank Karlitschek frank@owncloud.org * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE @@ -39,7 +39,6 @@ require_once 'phpass/PasswordHash.php'; * Class for user management in a SQL Database (e.g. MySQL, SQLite) */ class OC_User_Database extends OC_User_Backend { - static private $userGroupCache=array(); /** * @var PasswordHash */ @@ -70,7 +69,7 @@ class OC_User_Database extends OC_User_Backend { }else{ $hasher=$this->getHasher(); $hash = $hasher->HashPassword($password.OC_Config::getValue('passwordsalt', '')); - $query = OC_DB::prepare( "INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )" ); + $query = OC_DB::prepare( 'INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )' ); $result = $query->execute( array( $uid, $hash)); return $result ? true : false; @@ -86,8 +85,8 @@ class OC_User_Database extends OC_User_Backend { */ public function deleteUser( $uid ){ // Delete user-group-relation - $query = OC_DB::prepare( "DELETE FROM `*PREFIX*users` WHERE `uid` = ?" ); - $result = $query->execute( array( $uid )); + $query = OC_DB::prepare( 'DELETE FROM `*PREFIX*users` WHERE uid = ?' ); + $query->execute( array( $uid )); return true; } @@ -103,12 +102,11 @@ class OC_User_Database extends OC_User_Backend { if( $this->userExists($uid) ){ $hasher=$this->getHasher(); $hash = $hasher->HashPassword($password.OC_Config::getValue('passwordsalt', '')); - $query = OC_DB::prepare( "UPDATE `*PREFIX*users` SET `password` = ? WHERE `uid` = ?" ); - $result = $query->execute( array( $hash, $uid )); + $query = OC_DB::prepare( 'UPDATE `*PREFIX*users` SET `password` = ? WHERE `uid` = ?' ); + $query->execute( array( $hash, $uid )); return true; - } - else{ + }else{ return false; } } @@ -123,7 +121,7 @@ class OC_User_Database extends OC_User_Backend { * returns the user id or false */ public function checkPassword( $uid, $password ){ - $query = OC_DB::prepare( "SELECT `uid`, `password` FROM `*PREFIX*users` WHERE `uid` = ?" ); + $query = OC_DB::prepare( 'SELECT `uid`, `password` FROM `*PREFIX*users` WHERE `uid` = ?' ); $result = $query->execute( array( $uid)); $row=$result->fetchRow(); @@ -156,13 +154,12 @@ class OC_User_Database extends OC_User_Backend { * * Get a list of all users. */ - public function getUsers(){ - $query = OC_DB::prepare( "SELECT `uid` FROM `*PREFIX*users`" ); - $result = $query->execute(); - - $users=array(); - while( $row = $result->fetchRow()){ - $users[] = $row["uid"]; + public function getUsers($search = '', $limit = null, $offset = null) { + $query = OC_DB::prepare('SELECT `uid` FROM `*PREFIX*users` WHERE `uid` LIKE ? LIMIT',$limit,$offset); + $result = $query->execute(array($search.'%')); + $users = array(); + while ($row = $result->fetchRow()) { + $users[] = $row['uid']; } return $users; } @@ -173,7 +170,7 @@ class OC_User_Database extends OC_User_Backend { * @return boolean */ public function userExists($uid){ - $query = OC_DB::prepare( "SELECT * FROM `*PREFIX*users` WHERE `uid` = ?" ); + $query = OC_DB::prepare( 'SELECT * FROM `*PREFIX*users` WHERE `uid` = ?' ); $result = $query->execute( array( $uid )); return $result->numRows() > 0; diff --git a/lib/user/dummy.php b/lib/user/dummy.php index 03d5c3256da..da3edfb2df4 100644 --- a/lib/user/dummy.php +++ b/lib/user/dummy.php @@ -4,7 +4,7 @@ * ownCloud * * @author Frank Karlitschek -* @copyright 2010 Frank Karlitschek karlitschek@kde.org +* @copyright 2012 Frank Karlitschek frank@owncloud.org * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE @@ -100,7 +100,7 @@ class OC_User_Dummy extends OC_User_Backend { * * Get a list of all users. */ - public function getUsers(){ + public function getUsers($search = '', $limit = -1, $offset = 0) { return array_keys($this->users); } diff --git a/lib/user/example.php b/lib/user/example.php index b6a2091b685..77246d8136c 100644 --- a/lib/user/example.php +++ b/lib/user/example.php @@ -4,7 +4,7 @@ * ownCloud * * @author Frank Karlitschek - * @copyright 2010 Frank Karlitschek karlitschek@kde.org + * @copyright 2012 Frank Karlitschek frank@owncloud.org * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE @@ -35,20 +35,7 @@ abstract class OC_User_Example extends OC_User_Backend { * Creates a new user. Basic checking of username is done in OC_User * itself, not in its subclasses. */ - public function createUser($uid, $password){ - return OC_USER_BACKEND_NOT_IMPLEMENTED; - } - - /** - * @brief delete a user - * @param $uid The username of the user to delete - * @returns true/false - * - * Deletes a user - */ - public function deleteUser( $uid ){ - return OC_USER_BACKEND_NOT_IMPLEMENTED; - } + abstract public function createUser($uid, $password); /** * @brief Set password @@ -58,9 +45,7 @@ abstract class OC_User_Example extends OC_User_Backend { * * Change the password of a user */ - public function setPassword($uid, $password){ - return OC_USER_BACKEND_NOT_IMPLEMENTED; - } + abstract public function setPassword($uid, $password); /** * @brief Check if the password is correct @@ -71,26 +56,5 @@ abstract class OC_User_Example extends OC_User_Backend { * Check if the password is correct without logging in the user * returns the user id or false */ - public function checkPassword($uid, $password){ - return OC_USER_BACKEND_NOT_IMPLEMENTED; - } - - /** - * @brief Get a list of all users - * @returns array with all uids - * - * Get a list of all users. - */ - public function getUsers(){ - return OC_USER_BACKEND_NOT_IMPLEMENTED; - } - - /** - * @brief check if a user exists - * @param string $uid the username - * @return boolean - */ - public function userExists($uid){ - return OC_USER_BACKEND_NOT_IMPLEMENTED; - } + abstract public function checkPassword($uid, $password); } diff --git a/lib/user/http.php b/lib/user/http.php new file mode 100644 index 00000000000..009aa30c6f5 --- /dev/null +++ b/lib/user/http.php @@ -0,0 +1,93 @@ +<?php + +/** +* ownCloud +* +* @author Frank Karlitschek +* @copyright 2012 Robin Appelman icewind@owncloud.com +* +* 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/>. +* +*/ + +/** + * user backend using http auth requests + */ +class OC_User_HTTP extends OC_User_Backend { + /** + * split http://user@host/path into a user and url part + * @param string path + * @return array + */ + private function parseUrl($url){ + $parts=parse_url($url); + $url=$parts['scheme'].'://'.$parts['host']; + if(isset($parts['port'])){ + $url.=':'.$parts['port']; + } + $url.=$parts['path']; + if(isset($parts['query'])){ + $url.='?'.$parts['query']; + } + return array($parts['user'],$url); + + } + + /** + * check if an url is a valid login + * @param string url + * @return boolean + */ + private function matchUrl($url){ + return ! is_null(parse_url($url,PHP_URL_USER)); + } + + /** + * @brief Check if the password is correct + * @param $uid The username + * @param $password The password + * @returns string + * + * Check if the password is correct without logging in the user + * returns the user id or false + */ + public function checkPassword($uid, $password){ + if(!$this->matchUrl($uid)){ + return false; + } + list($user,$url)=$this->parseUrl($uid); + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_USERPWD, $user.':'.$password); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + curl_exec($ch); + + $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); + + curl_close($ch); + + return $status==200; + } + + /** + * @brief check if a user exists + * @param string $uid the username + * @return boolean + */ + public function userExists($uid){ + return $this->matchUrl($uid); + } +}
\ No newline at end of file diff --git a/lib/user/interface.php b/lib/user/interface.php new file mode 100644 index 00000000000..a4903898fb1 --- /dev/null +++ b/lib/user/interface.php @@ -0,0 +1,60 @@ +<?php + +/** + * ownCloud - user interface + * + * @author Arthur Schiwon + * @copyright 2012 Arthur Schiwon blizzz@owncloud.org + * + * 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/>. + * + */ + +interface OC_User_Interface { + + /** + * @brief Check if backend implements actions + * @param $actions bitwise-or'ed actions + * @returns boolean + * + * Returns the supported actions as int to be + * compared with OC_USER_BACKEND_CREATE_USER etc. + */ + public function implementsActions($actions); + + /** + * @brief delete a user + * @param $uid The username of the user to delete + * @returns true/false + * + * Deletes a user + */ + public function deleteUser($uid); + + /** + * @brief Get a list of all users + * @returns array with all uids + * + * Get a list of all users. + */ + public function getUsers($search = '', $limit = -1, $offset = 0); + + /** + * @brief check if a user exists + * @param string $uid the username + * @return boolean + */ + public function userExists($uid); + +}
\ No newline at end of file |