diff options
Diffstat (limited to 'lib/group')
-rw-r--r-- | lib/group/backend.php | 69 | ||||
-rw-r--r-- | lib/group/database.php | 89 | ||||
-rw-r--r-- | lib/group/dummy.php | 9 | ||||
-rw-r--r-- | lib/group/example.php | 25 | ||||
-rw-r--r-- | lib/group/interface.php | 76 |
5 files changed, 198 insertions, 70 deletions
diff --git a/lib/group/backend.php b/lib/group/backend.php index af6c53c8035..5969986c652 100644 --- a/lib/group/backend.php +++ b/lib/group/backend.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 @@ -31,28 +31,20 @@ define('OC_GROUP_BACKEND_NOT_IMPLEMENTED', -501); */ define('OC_GROUP_BACKEND_CREATE_GROUP', 0x00000001); define('OC_GROUP_BACKEND_DELETE_GROUP', 0x00000010); -define('OC_GROUP_BACKEND_IN_GROUP', 0x00000100); -define('OC_GROUP_BACKEND_ADD_TO_GROUP', 0x00001000); -define('OC_GROUP_BACKEND_REMOVE_FROM_GOUP', 0x00010000); -define('OC_GROUP_BACKEND_GET_USER_GROUPS', 0x00100000); -define('OC_GROUP_BACKEND_GET_USERS', 0x01000000); -define('OC_GROUP_BACKEND_GET_GROUPS', 0x10000000); +define('OC_GROUP_BACKEND_ADD_TO_GROUP', 0x00000100); +define('OC_GROUP_BACKEND_REMOVE_FROM_GOUP', 0x00001000); /** * Abstract base class for user management */ -abstract class OC_Group_Backend { +abstract class OC_Group_Backend implements OC_Group_Interface { protected $possibleActions = array( OC_GROUP_BACKEND_CREATE_GROUP => 'createGroup', OC_GROUP_BACKEND_DELETE_GROUP => 'deleteGroup', - OC_GROUP_BACKEND_IN_GROUP => 'inGroup', OC_GROUP_BACKEND_ADD_TO_GROUP => 'addToGroup', OC_GROUP_BACKEND_REMOVE_FROM_GOUP => 'removeFromGroup', - OC_GROUP_BACKEND_GET_USER_GROUPS => 'getUserGroups', - OC_GROUP_BACKEND_GET_USERS => 'usersInGroup', - OC_GROUP_BACKEND_GET_GROUPS => 'getGroups' ); - + /** * @brief Get all supported actions * @returns bitwise-or'ed actions @@ -70,7 +62,7 @@ abstract class OC_Group_Backend { return $actions; } - + /** * @brief Check if backend implements actions * @param $actions bitwise-or'ed actions @@ -84,14 +76,55 @@ abstract class OC_Group_Backend { } /** + * @brief is user in group? + * @param $uid uid of the user + * @param $gid gid of the group + * @returns true/false + * + * Checks whether the user is member of a group or not. + */ + public function inGroup($uid, $gid){ + return in_array($gid, $this->getUserGroups($uid)); + } + + /** + * @brief Get all groups a user belongs to + * @param $uid Name of the user + * @returns array with group names + * + * This function fetches all groups a user belongs to. It does not check + * if the user exists at all. + */ + public function getUserGroups($uid){ + return array(); + } + + /** + * @brief get a list of all groups + * @returns array with group names + * + * Returns a list with all groups + */ + + public function getGroups($search = '', $limit = -1, $offset = 0) { + return array(); + } + + /** * check if a group exists * @param string $gid * @return bool */ public function groupExists($gid){ - if(!$this->implementsActions(OC_GROUP_BACKEND_GET_GROUPS)){ - return false; - } - return in_array($gid, $this->getGroups()); + return in_array($gid, $this->getGroups($gid, 1)); } + + /** + * @brief get a list of all users in a group + * @returns array with user ids + */ + public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { + return array(); + } + } diff --git a/lib/group/database.php b/lib/group/database.php index 5e52432c492..52608b2db73 100644 --- a/lib/group/database.php +++ b/lib/group/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 @@ -41,7 +41,6 @@ * Class for group management in a SQL Database (e.g. MySQL, SQLite) */ class OC_Group_Database extends OC_Group_Backend { - static private $userGroupCache=array(); /** * @brief Try to create a new group @@ -51,10 +50,10 @@ class OC_Group_Database extends OC_Group_Backend { * Trys to create a new group. If the group name already exists, false will * be returned. */ - public static function createGroup( $gid ){ + public function createGroup( $gid ){ // Check for existence - $query = OC_DB::prepare( 'SELECT `gid` FROM `*PREFIX*groups` WHERE `gid` = ?' ); - $result = $query->execute( array( $gid )); + $stmt = OC_DB::prepare( "SELECT `gid` FROM `*PREFIX*groups` WHERE `gid` = ?" ); + $result = $stmt->execute( array( $gid )); if( $result->fetchRow() ){ // Can not add an existing group @@ -62,8 +61,8 @@ class OC_Group_Database extends OC_Group_Backend { } else{ // Add group and exit - $query = OC_DB::prepare( 'INSERT INTO `*PREFIX*groups` ( `gid` ) VALUES( ? )' ); - $result = $query->execute( array( $gid )); + $stmt = OC_DB::prepare( "INSERT INTO `*PREFIX*groups` ( `gid` ) VALUES( ? )" ); + $result = $stmt->execute( array( $gid )); return $result ? true : false; } @@ -76,14 +75,14 @@ class OC_Group_Database extends OC_Group_Backend { * * Deletes a group and removes it from the group_user-table */ - public static function deleteGroup( $gid ){ + public function deleteGroup( $gid ){ // Delete the group - $query = OC_DB::prepare( 'DELETE FROM `*PREFIX*groups` WHERE `gid` = ?' ); - $result = $query->execute( array( $gid )); + $stmt = OC_DB::prepare( "DELETE FROM `*PREFIX*groups` WHERE `gid` = ?" ); + $result = $stmt->execute( array( $gid )); // Delete the group-user relation - $query = OC_DB::prepare( 'DELETE FROM `*PREFIX*group_user` WHERE `gid` = ?' ); - $result = $query->execute( array( $gid )); + $stmt = OC_DB::prepare( "DELETE FROM `*PREFIX*group_user` WHERE `gid` = ?" ); + $result = $stmt->execute( array( $gid )); return true; } @@ -96,10 +95,10 @@ class OC_Group_Database extends OC_Group_Backend { * * Checks whether the user is member of a group or not. */ - public static function inGroup( $uid, $gid ){ + public function inGroup( $uid, $gid ){ // check - $query = OC_DB::prepare( 'SELECT `uid` FROM `*PREFIX*group_user` WHERE `gid` = ? AND `uid` = ?' ); - $result = $query->execute( array( $gid, $uid )); + $stmt = OC_DB::prepare( "SELECT `uid` FROM `*PREFIX*group_user` WHERE `gid` = ? AND `uid` = ?" ); + $result = $stmt->execute( array( $gid, $uid )); return $result->fetchRow() ? true : false; } @@ -112,11 +111,11 @@ class OC_Group_Database extends OC_Group_Backend { * * Adds a user to a group. */ - public static function addToGroup( $uid, $gid ){ + public function addToGroup( $uid, $gid ){ // No duplicate entries! - if( !self::inGroup( $uid, $gid )){ - $query = OC_DB::prepare( 'INSERT INTO `*PREFIX*group_user` ( `uid`, `gid` ) VALUES( ?, ? )' ); - $result = $query->execute( array( $uid, $gid )); + if( !$this->inGroup( $uid, $gid )){ + $stmt = OC_DB::prepare( "INSERT INTO `*PREFIX*group_user` ( `uid`, `gid` ) VALUES( ?, ? )" ); + $stmt->execute( array( $uid, $gid )); return true; }else{ return false; @@ -131,9 +130,9 @@ class OC_Group_Database extends OC_Group_Backend { * * removes the user from a group. */ - public static function removeFromGroup( $uid, $gid ){ - $query = OC_DB::prepare( 'DELETE FROM `*PREFIX*group_user` WHERE `uid` = ? AND `gid` = ?' ); - $result = $query->execute( array( $uid, $gid )); + public function removeFromGroup( $uid, $gid ){ + $stmt = OC_DB::prepare( "DELETE FROM `*PREFIX*group_user` WHERE `uid` = ? AND `gid` = ?" ); + $stmt->execute( array( $uid, $gid )); return true; } @@ -146,10 +145,10 @@ class OC_Group_Database extends OC_Group_Backend { * This function fetches all groups a user belongs to. It does not check * if the user exists at all. */ - public static function getUserGroups( $uid ){ + public function getUserGroups( $uid ){ // No magic! - $query = OC_DB::prepare( 'SELECT `gid` FROM `*PREFIX*group_user` WHERE `uid` = ?' ); - $result = $query->execute( array( $uid )); + $stmt = OC_DB::prepare( "SELECT `gid` FROM `*PREFIX*group_user` WHERE `uid` = ?" ); + $result = $stmt->execute( array( $uid )); $groups = array(); while( $row = $result->fetchRow()){ @@ -165,28 +164,40 @@ class OC_Group_Database extends OC_Group_Backend { * * Returns a list with all groups */ - public static function getGroups(){ - $query = OC_DB::prepare( 'SELECT `gid` FROM `*PREFIX*groups`' ); - $result = $query->execute(); - + public function getGroups($search = '', $limit = null, $offset = null) { + $stmt = OC_DB::prepare('SELECT `gid` FROM `*PREFIX*groups` WHERE `gid` LIKE ?', $limit, $offset); + $result = $stmt->execute(array($search.'%')); $groups = array(); - while( $row = $result->fetchRow()){ - $groups[] = $row["gid"]; + while ($row = $result->fetchRow()) { + $groups[] = $row['gid']; } - return $groups; } - + + /** + * check if a group exists + * @param string $gid + * @return bool + */ + public function groupExists($gid) { + $query = OC_DB::prepare('SELECT `gid` FROM `*PREFIX*groups` WHERE `gid` = ?'); + $result = $query->execute(array($gid))->fetchOne(); + if ($result) { + return true; + } + return false; + } + /** * @brief get a list of all users in a group * @returns array with user ids */ - public static function usersInGroup($gid){ - $query=OC_DB::prepare('SELECT `uid` FROM `*PREFIX*group_user` WHERE `gid`=?'); - $users=array(); - $result=$query->execute(array($gid)); - while($row=$result->fetchRow()){ - $users[]=$row['uid']; + public function usersInGroup($gid, $search = '', $limit = null, $offset = null) { + $stmt = OC_DB::prepare('SELECT `uid` FROM `*PREFIX*group_user` WHERE `gid` = ? AND `uid` LIKE ?', $limit, $offset); + $result = $stmt->execute(array($gid, $search.'%')); + $users = array(); + while ($row = $result->fetchRow()) { + $users[] = $row['uid']; } return $users; } diff --git a/lib/group/dummy.php b/lib/group/dummy.php index 5220237ecbf..51eca28f3f4 100644 --- a/lib/group/dummy.php +++ b/lib/group/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 @@ -126,7 +126,8 @@ class OC_Group_Dummy extends OC_Group_Backend { */ public function getUserGroups($uid){ $groups=array(); - foreach($this->groups as $group=>$user){ + $allGroups=array_keys($this->groups); + foreach($allGroups as $group){ if($this->inGroup($uid,$group)){ $groups[]=$group; } @@ -140,7 +141,7 @@ class OC_Group_Dummy extends OC_Group_Backend { * * Returns a list with all groups */ - public function getGroups(){ + public function getGroups($search = '', $limit = -1, $offset = 0) { return array_keys($this->groups); } @@ -148,7 +149,7 @@ class OC_Group_Dummy extends OC_Group_Backend { * @brief get a list of all users in a group * @returns array with user ids */ - public function usersInGroup($gid){ + public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { if(isset($this->groups[$gid])){ return $this->groups[$gid]; }else{ diff --git a/lib/group/example.php b/lib/group/example.php index a88159f91be..76d12629763 100644 --- a/lib/group/example.php +++ b/lib/group/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 @@ -34,7 +34,7 @@ abstract class OC_Group_Example { * Trys to create a new group. If the group name already exists, false will * be returned. */ - public static function createGroup($gid){} + abstract public static function createGroup($gid); /** * @brief delete a group @@ -43,7 +43,7 @@ abstract class OC_Group_Example { * * Deletes a group and removes it from the group_user-table */ - public static function deleteGroup($gid){} + abstract public static function deleteGroup($gid); /** * @brief is user in group? @@ -53,7 +53,7 @@ abstract class OC_Group_Example { * * Checks whether the user is member of a group or not. */ - public static function inGroup($uid, $gid){} + abstract public static function inGroup($uid, $gid); /** * @brief Add a user to a group @@ -63,7 +63,7 @@ abstract class OC_Group_Example { * * Adds a user to a group. */ - public static function addToGroup($uid, $gid){} + abstract public static function addToGroup($uid, $gid); /** * @brief Removes a user from a group @@ -73,7 +73,7 @@ abstract class OC_Group_Example { * * removes the user from a group. */ - public static function removeFromGroup($uid,$gid){} + abstract public static function removeFromGroup($uid,$gid); /** * @brief Get all groups a user belongs to @@ -83,7 +83,7 @@ abstract class OC_Group_Example { * This function fetches all groups a user belongs to. It does not check * if the user exists at all. */ - public static function getUserGroups($uid){} + abstract public static function getUserGroups($uid); /** * @brief get a list of all groups @@ -91,12 +91,19 @@ abstract class OC_Group_Example { * * Returns a list with all groups */ - public static function getGroups(){} + abstract public static function getGroups($search = '', $limit = -1, $offset = 0); + + /** + * check if a group exists + * @param string $gid + * @return bool + */ + abstract public function groupExists($gid); /** * @brief get a list of all users in a group * @returns array with user ids */ - public static function usersInGroup($gid){} + abstract public static function usersInGroup($gid, $search = '', $limit = -1, $offset = 0); } diff --git a/lib/group/interface.php b/lib/group/interface.php new file mode 100644 index 00000000000..12cc07a5374 --- /dev/null +++ b/lib/group/interface.php @@ -0,0 +1,76 @@ +<?php + +/** + * ownCloud - group 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_Group_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_GROUP_BACKEND_CREATE_GROUP etc. + */ + public function implementsActions($actions); + + /** + * @brief is user in group? + * @param $uid uid of the user + * @param $gid gid of the group + * @returns true/false + * + * Checks whether the user is member of a group or not. + */ + public function inGroup($uid, $gid); + + /** + * @brief Get all groups a user belongs to + * @param $uid Name of the user + * @returns array with group names + * + * This function fetches all groups a user belongs to. It does not check + * if the user exists at all. + */ + public function getUserGroups($uid); + + /** + * @brief get a list of all groups + * @returns array with group names + * + * Returns a list with all groups + */ + public function getGroups($search = '', $limit = -1, $offset = 0); + + /** + * check if a group exists + * @param string $gid + * @return bool + */ + public function groupExists($gid); + + /** + * @brief get a list of all users in a group + * @returns array with user ids + */ + public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0); + +}
\ No newline at end of file |