From 2f1a990cadc5202dd20c3ec87e886685f6733595 Mon Sep 17 00:00:00 2001
From: Michael Gapczynski <mtgap@owncloud.com>
Date: Mon, 30 Jul 2012 20:20:46 -0400
Subject: [PATCH] Add search, limit, and offset parameters to getUsers() and
 getGroups()

---
 lib/group.php           |  8 ++++----
 lib/group/backend.php   |  2 +-
 lib/group/database.php  | 12 +++++-------
 lib/group/interface.php |  2 +-
 lib/public/user.php     |  2 +-
 lib/user.php            | 12 ++++++------
 lib/user/backend.php    |  2 +-
 lib/user/database.php   | 13 ++++++-------
 lib/user/interface.php  |  2 +-
 9 files changed, 26 insertions(+), 29 deletions(-)

diff --git a/lib/group.php b/lib/group.php
index 12e5f5ebb3..eb6a582809 100644
--- a/lib/group.php
+++ b/lib/group.php
@@ -237,10 +237,10 @@ class OC_Group {
 	 *
 	 * Returns a list with all groups
 	 */
-	public static function getGroups(){
-		$groups=array();
-		foreach(self::$_usedBackends as $backend){
-			$groups=array_merge($backend->getGroups(),$groups);
+	public static function getGroups($search = '', $limit = 10, $offset = 0) {
+		$groups = array();
+		foreach (self::$_usedBackends as $backend) {
+			$groups = array_merge($backend->getGroups($search, $limit, $offset), $groups);
 		}
 		asort($groups);
 		return $groups;
diff --git a/lib/group/backend.php b/lib/group/backend.php
index ebc078f152..3f2909caa1 100644
--- a/lib/group/backend.php
+++ b/lib/group/backend.php
@@ -105,7 +105,7 @@ abstract class OC_Group_Backend implements OC_Group_Interface {
 	 *
 	 * Returns a list with all groups
 	 */
-	public function getGroups(){
+	public function getGroups($search = '', $limit = 10, $offset = 0) {
 		return array();
 	}
 
diff --git a/lib/group/database.php b/lib/group/database.php
index 2770ec185c..6314a12743 100644
--- a/lib/group/database.php
+++ b/lib/group/database.php
@@ -164,15 +164,13 @@ class OC_Group_Database extends OC_Group_Backend {
 	 *
 	 * Returns a list with all groups
 	 */
-	public function getGroups(){
-		$query = OC_DB::prepare( "SELECT gid FROM `*PREFIX*groups`" );
-		$result = $query->execute();
-
+	public function getGroups($search = '', $limit = 10, $offset = 0) {
+		$query = OC_DB::prepare('SELECT gid FROM *PREFIX*groups WHERE gid LIKE ? LIMIT '.$limit.' OFFSET '.$offset);
+		$result = $query->execute(array($search.'%'));
 		$groups = array();
-		while( $row = $result->fetchRow()){
-			$groups[] = $row["gid"];
+		while ($row = $result->fetchRow()) {
+			$groups[] = $row['gid'];
 		}
-
 		return $groups;
 	}
 
diff --git a/lib/group/interface.php b/lib/group/interface.php
index 7cca6061e1..6e492e7274 100644
--- a/lib/group/interface.php
+++ b/lib/group/interface.php
@@ -58,7 +58,7 @@ interface OC_Group_Interface {
 	 *
 	 * Returns a list with all groups
 	 */
-	public function getGroups();
+	public function getGroups($search = '', $limit = 10, $offset = 0);
 
 	/**
 	 * check if a group exists
diff --git a/lib/public/user.php b/lib/public/user.php
index 713e366b96..178d1dddd3 100644
--- a/lib/public/user.php
+++ b/lib/public/user.php
@@ -51,7 +51,7 @@ class User {
 	 *
 	 * Get a list of all users.
 	 */
-	public static function getUsers(){
+	public static function getUsers($search = '', $limit = 10, $offset = 0) {
 		return \OC_USER::getUsers();
 	}
 
diff --git a/lib/user.php b/lib/user.php
index e3c9c23eff..681fd82b99 100644
--- a/lib/user.php
+++ b/lib/user.php
@@ -337,12 +337,12 @@ class OC_User {
 	 *
 	 * Get a list of all users.
 	 */
-	public static function getUsers(){
-		$users=array();
-		foreach(self::$_usedBackends as $backend){
-			$backendUsers=$backend->getUsers();
-			if(is_array($backendUsers)){
-				$users=array_merge($users,$backendUsers);
+	public static function getUsers($search = '', $limit = 10, $offset = 0) {
+		$users = array();
+		foreach (self::$_usedBackends as $backend) {
+			$backendUsers = $backend->getUsers($search, $limit, $offset);
+			if (is_array($backendUsers)) {
+				$users = array_merge($users, $backendUsers);
 			}
 		}
 		asort($users);
diff --git a/lib/user/backend.php b/lib/user/backend.php
index daa942d261..ff00ef08f6 100644
--- a/lib/user/backend.php
+++ b/lib/user/backend.php
@@ -97,7 +97,7 @@ abstract class OC_User_Backend implements OC_User_Interface {
 	*
 	* Get a list of all users.
 	*/
-	public function getUsers(){
+	public function getUsers($search = '', $limit = 10, $offset = 0) {
 		return array();
 	}
 
diff --git a/lib/user/database.php b/lib/user/database.php
index cc27b3ddbf..968814d9d5 100644
--- a/lib/user/database.php
+++ b/lib/user/database.php
@@ -154,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 = 10, $offset = 0) {
+		$query = OC_DB::prepare('SELECT uid FROM *PREFIX*users WHERE uid LIKE ? LIMIT '.$limit.' OFFSET '.$offset);
+		$result = $query->execute(array($search.'%'));
+		$users = array();
+		while ($row = $result->fetchRow()) {
+			$users[] = $row['uid'];
 		}
 		return $users;
 	}
diff --git a/lib/user/interface.php b/lib/user/interface.php
index dc3685dc20..b3286cd2f9 100644
--- a/lib/user/interface.php
+++ b/lib/user/interface.php
@@ -48,7 +48,7 @@ interface OC_User_Interface {
 	*
 	* Get a list of all users.
 	*/
-	public function getUsers();
+	public function getUsers($search = '', $limit = 10, $offset = 0);
 
 	/**
 	* @brief check if a user exists
-- 
GitLab