diff options
Diffstat (limited to 'lib/private/group')
-rw-r--r-- | lib/private/group/database.php | 18 | ||||
-rw-r--r-- | lib/private/group/dummy.php | 33 | ||||
-rw-r--r-- | lib/private/group/group.php | 4 | ||||
-rw-r--r-- | lib/private/group/manager.php | 45 | ||||
-rw-r--r-- | lib/private/group/metadata.php | 100 |
5 files changed, 126 insertions, 74 deletions
diff --git a/lib/private/group/database.php b/lib/private/group/database.php index baaf2cf2739..8aebefabd27 100644 --- a/lib/private/group/database.php +++ b/lib/private/group/database.php @@ -168,8 +168,8 @@ class OC_Group_Database extends OC_Group_Backend { * Returns a list with all groups */ 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.'%')); + $stmt = OC_DB::prepare('SELECT `gid` FROM `*PREFIX*groups` WHERE LOWER(`gid`) LIKE LOWER(?) ORDER BY `gid` ASC', $limit, $offset); + $result = $stmt->execute(array('%' . $search . '%')); $groups = array(); while ($row = $result->fetchRow()) { $groups[] = $row['gid']; @@ -185,7 +185,7 @@ class OC_Group_Database extends OC_Group_Backend { public function groupExists($gid) { $query = OC_DB::prepare('SELECT `gid` FROM `*PREFIX*groups` WHERE `gid` = ?'); $result = $query->execute(array($gid))->fetchOne(); - if ($result) { + if ($result !== false) { return true; } return false; @@ -200,10 +200,10 @@ class OC_Group_Database extends OC_Group_Backend { * @return array an array of user ids */ public function usersInGroup($gid, $search = '', $limit = null, $offset = null) { - $stmt = OC_DB::prepare('SELECT `uid` FROM `*PREFIX*group_user` WHERE `gid` = ? AND `uid` LIKE ?', + $stmt = OC_DB::prepare('SELECT `uid` FROM `*PREFIX*group_user` WHERE `gid` = ? AND `uid` LIKE ? ORDER BY `uid` ASC', $limit, $offset); - $result = $stmt->execute(array($gid, $search.'%')); + $result = $stmt->execute(array($gid, '%'.$search.'%')); $users = array(); while ($row = $result->fetchRow()) { $users[] = $row['uid']; @@ -220,8 +220,12 @@ class OC_Group_Database extends OC_Group_Backend { */ public function countUsersInGroup($gid, $search = '') { $stmt = OC_DB::prepare('SELECT COUNT(`uid`) AS `count` FROM `*PREFIX*group_user` WHERE `gid` = ? AND `uid` LIKE ?'); - $result = $stmt->execute(array($gid, $search.'%')); - return $result->fetchOne(); + $result = $stmt->execute(array($gid, '%' . $search . '%')); + $count = $result->fetchOne(); + if($count !== false) { + $count = intval($count); + } + return $count; } } diff --git a/lib/private/group/dummy.php b/lib/private/group/dummy.php index e48c6a0e266..4af18b267bc 100644 --- a/lib/private/group/dummy.php +++ b/lib/private/group/dummy.php @@ -143,7 +143,16 @@ class OC_Group_Dummy extends OC_Group_Backend { * @return array an array of group names */ public function getGroups($search = '', $limit = -1, $offset = 0) { - return array_keys($this->groups); + if(empty($search)) { + return array_keys($this->groups); + } + $result = array(); + foreach(array_keys($this->groups) as $group) { + if(stripos($group, $search) !== false) { + $result[] = $group; + } + } + return $result; } /** @@ -156,7 +165,16 @@ class OC_Group_Dummy extends OC_Group_Backend { */ public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { if(isset($this->groups[$gid])) { - return $this->groups[$gid]; + if(empty($search)) { + return $this->groups[$gid]; + } + $result = array(); + foreach($this->groups[$gid] as $user) { + if(stripos($user, $search) !== false) { + $result[] = $user; + } + } + return $result; }else{ return array(); } @@ -172,7 +190,16 @@ class OC_Group_Dummy extends OC_Group_Backend { */ public function countUsersInGroup($gid, $search = '', $limit = -1, $offset = 0) { if(isset($this->groups[$gid])) { - return count($this->groups[$gid]); + if(empty($search)) { + return count($this->groups[$gid]); + } + $count = 0; + foreach($this->groups[$gid] as $user) { + if(stripos($user, $search) !== false) { + $count++; + } + } + return $count; } } diff --git a/lib/private/group/group.php b/lib/private/group/group.php index e0a35924854..6f8b84dff1a 100644 --- a/lib/private/group/group.php +++ b/lib/private/group/group.php @@ -9,7 +9,9 @@ namespace OC\Group; -class Group { +use OCP\IGroup; + +class Group implements IGroup { /** * @var string $id */ diff --git a/lib/private/group/manager.php b/lib/private/group/manager.php index 94616bfe4f4..816e7b427f5 100644 --- a/lib/private/group/manager.php +++ b/lib/private/group/manager.php @@ -10,6 +10,7 @@ namespace OC\Group; use OC\Hooks\PublicEmitter; +use OCP\IGroupManager; /** * Class Manager @@ -26,7 +27,7 @@ use OC\Hooks\PublicEmitter; * * @package OC\Group */ -class Manager extends PublicEmitter { +class Manager extends PublicEmitter implements IGroupManager { /** * @var \OC_Group_Backend[]|\OC_Group_Database[] $backends */ @@ -126,7 +127,7 @@ class Manager extends PublicEmitter { * @return \OC\Group\Group */ public function createGroup($gid) { - if (!$gid) { + if ($gid === '' || is_null($gid)) { return false; } else if ($group = $this->get($gid)) { return $group; @@ -215,23 +216,41 @@ class Manager extends PublicEmitter { if(is_null($group)) { return array(); } - // only user backends have the capability to do a complex search for users - $groupUsers = $group->searchUsers('', $limit, $offset); + $search = trim($search); + $groupUsers = array(); + if(!empty($search)) { - //TODO: for OC 7 earliest: user backend should get a method to check selected users against a pattern - $filteredUsers = $this->userManager->search($search); - $testUsers = true; + // only user backends have the capability to do a complex search for users + $searchOffset = 0; + if($limit === -1) { + $searchLimit = $group->count(''); + } else { + $searchLimit = $limit * 2; + } + + do { + $filteredUsers = $this->userManager->search($search, $searchLimit, $searchOffset); + foreach($filteredUsers as $filteredUser) { + if($group->inGroup($filteredUser)) { + $groupUsers[]= $filteredUser; + } + } + $searchOffset += $searchLimit; + } while(count($groupUsers) < $searchLimit+$offset && count($filteredUsers) === $searchLimit); + + if($limit === -1) { + $groupUsers = array_slice($groupUsers, $offset); + } else { + $groupUsers = array_slice($groupUsers, $offset, $limit); + } } else { - $filteredUsers = array(); - $testUsers = false; + $groupUsers = $group->searchUsers('', $limit, $offset); } $matchingUsers = array(); - foreach($groupUsers as $user) { - if(!$testUsers || isset($filteredUsers[$user->getUID()])) { - $matchingUsers[$user->getUID()] = $user->getDisplayName(); - } + foreach($groupUsers as $groupUser) { + $matchingUsers[$groupUser->getUID()] = $groupUser->getDisplayName(); } return $matchingUsers; } diff --git a/lib/private/group/metadata.php b/lib/private/group/metadata.php index 1883ba727e0..687a735347c 100644 --- a/lib/private/group/metadata.php +++ b/lib/private/group/metadata.php @@ -24,9 +24,9 @@ class MetaData { protected $isAdmin; /** - * @var string[] $groups + * @var array $metaData */ - protected $groups = array(); + protected $metaData = array(); /** * @var \OC\Group\Manager $groupManager @@ -39,14 +39,9 @@ class MetaData { protected $sorting = false; /** - * @var string $lastSearch - */ - protected $lastSearch; - - /** - * @param string the uid of the current user - * @param bool whether the current users is an admin - * @param \OC\Group\Manager + * @param string $user the uid of the current user + * @param bool $isAdmin whether the current users is an admin + * @param \OC\Group\Manager $groupManager */ public function __construct( $user, @@ -63,14 +58,15 @@ class MetaData { * the array is structured as follows: * [0] array containing meta data about admin groups * [1] array containing meta data about unprivileged groups - * @param string only effective when instance was created with isAdmin being - * true + * @param string $groupSearch only effective when instance was created with + * isAdmin being true + * @param string $userSearch the pattern users are search for * @return array */ - public function get($search = '') { - if($this->lastSearch !== $search) { - $this->lastSearch = $search; - $this->groups = array(); + public function get($groupSearch = '', $userSearch = '') { + $key = $groupSearch . '::' . $userSearch; + if(isset($this->metaData[$key])) { + return $this->metaData[$key]; } $adminGroups = array(); @@ -80,8 +76,8 @@ class MetaData { $sortAdminGroupsIndex = 0; $sortAdminGroupsKeys = array(); - foreach($this->getGroups($search) as $group) { - $groupMetaData = $this->generateGroupMetaData($group); + foreach($this->getGroups($groupSearch) as $group) { + $groupMetaData = $this->generateGroupMetaData($group, $userSearch); if (strtolower($group->getGID()) !== 'admin') { $this->addEntry( $groups, @@ -104,13 +100,14 @@ class MetaData { $this->sort($groups, $sortGroupsKeys); $this->sort($adminGroups, $sortAdminGroupsKeys); - return array($adminGroups, $groups); + $this->metaData[$key] = array($adminGroups, $groups); + return $this->metaData[$key]; } /** - * @brief sets the sort mode, currently 0 (none) and 1 (user entries, + * sets the sort mode, currently 0 (none) and 1 (user entries, * descending) are supported - * @param int the sortMode (SORT_NONE, SORT_USERCOUNT) + * @param int $sortMode (SORT_NONE, SORT_USERCOUNT) */ public function setSorting($sortMode) { if($sortMode >= 0 && $sortMode <= 1) { @@ -121,11 +118,11 @@ class MetaData { } /** - * @brief adds an group entry to the resulting array - * @param array the resulting array, by reference - * @param array the sort key array, by reference - * @param array the sort key index, by reference - * @param array the group's meta data as returned by generateGroupMetaData() + * adds an group entry to the resulting array + * @param array $entries the resulting array, by reference + * @param array $sortKeys the sort key array, by reference + * @param int $sortIndex the sort key index, by reference + * @param array $data the group's meta data as returned by generateGroupMetaData() * @return null */ private function addEntry(&$entries, &$sortKeys, &$sortIndex, $data) { @@ -137,22 +134,23 @@ class MetaData { } /** - * @brief creates an array containing the group meta data - * @param \OC\Group\Group + * creates an array containing the group meta data + * @param \OC\Group\Group $group + * @param string $userSearch * @return array with the keys 'id', 'name' and 'usercount' */ - private function generateGroupMetaData(\OC\Group\Group $group) { + private function generateGroupMetaData(\OC\Group\Group $group, $userSearch) { return array( - 'id' => str_replace(' ','', $group->getGID()), + 'id' => $group->getGID(), 'name' => $group->getGID(), - 'usercount' => $group->count() + 'usercount' => $group->count($userSearch) ); } /** - * @brief sorts the result array, if applicable - * @param array the result array, by reference - * @param array the array containing the sort keys + * sorts the result array, if applicable + * @param array $entries the result array, by reference + * @param array $sortKeys the array containing the sort keys * @param return null */ private function sort(&$entries, $sortKeys) { @@ -162,27 +160,29 @@ class MetaData { } /** - * @brief returns the available groups - * @param string a search string + * returns the available groups + * @param string $search a search string * @return \OC\Group\Group[] */ private function getGroups($search = '') { - if(count($this->groups) === 0) { - $this->fetchGroups($search); - } - return $this->groups; - } - - /** - * @brief fetches the group using the group manager or the subAdmin API - * @param string a search string - * @return null - */ - private function fetchGroups($search = '') { if($this->isAdmin) { - $this->groups = $this->groupManager->search($search); + return $this->groupManager->search($search); } else { - $this->groups = \OC_SubAdmin::getSubAdminsGroups($this->user); + $groupIds = \OC_SubAdmin::getSubAdminsGroups($this->user); + + /* \OC_SubAdmin::getSubAdminsGroups() returns an array of GIDs, but this + * method is expected to return an array with the GIDs as keys and group objects as + * values, so we need to convert this information. + */ + $groups = array(); + foreach($groupIds as $gid) { + $group = $this->groupManager->get($gid); + if (!is_null($group)) { + $groups[$gid] = $group; + } + } + + return $groups; } } } |