diff options
author | blizzz <blizzz@owncloud.com> | 2014-10-20 13:42:51 +0400 |
---|---|---|
committer | blizzz <blizzz@owncloud.com> | 2014-10-20 13:42:51 +0400 |
commit | 6fc04c2f9129fc55966ad66d2ddca1b7d884f4e9 (patch) | |
tree | 290a9b2a19c2c9e133911e8b519b4c05d63df40f /lib | |
parent | 55573645c6ebcbe3710150b55a94fe117c476a2d (diff) | |
parent | 4e6a4e53cf7081c332826bc9c339fb14b66b4765 (diff) |
Merge pull request #11217 from owncloud/backport-9225-stable6
Backport of #9225 to stable6
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/group/backend.php | 5 | ||||
-rw-r--r-- | lib/private/group/database.php | 41 | ||||
-rw-r--r-- | lib/private/group/dummy.php | 10 | ||||
-rw-r--r-- | lib/private/group/group.php | 46 | ||||
-rw-r--r-- | lib/private/group/manager.php | 56 |
5 files changed, 128 insertions, 30 deletions
diff --git a/lib/private/group/backend.php b/lib/private/group/backend.php index 11ec6c2920d..fd9a8d61c12 100644 --- a/lib/private/group/backend.php +++ b/lib/private/group/backend.php @@ -33,7 +33,8 @@ define('OC_GROUP_BACKEND_CREATE_GROUP', 0x00000001); define('OC_GROUP_BACKEND_DELETE_GROUP', 0x00000010); define('OC_GROUP_BACKEND_ADD_TO_GROUP', 0x00000100); define('OC_GROUP_BACKEND_REMOVE_FROM_GOUP', 0x00001000); -define('OC_GROUP_BACKEND_GET_DISPLAYNAME', 0x00010000); //OBSOLETE +define('OC_GROUP_BACKEND_GET_DISPLAYNAME', 0x00010000); +define('OC_GROUP_BACKEND_COUNT_USERS', 0x00100000); /** * Abstract base class for user management @@ -44,6 +45,8 @@ abstract class OC_Group_Backend implements OC_Group_Interface { OC_GROUP_BACKEND_DELETE_GROUP => 'deleteGroup', OC_GROUP_BACKEND_ADD_TO_GROUP => 'addToGroup', OC_GROUP_BACKEND_REMOVE_FROM_GOUP => 'removeFromGroup', + OC_GROUP_BACKEND_GET_DISPLAYNAME => 'displayNamesInGroup', + OC_GROUP_BACKEND_COUNT_USERS => 'countUsersInGroup', ); /** diff --git a/lib/private/group/database.php b/lib/private/group/database.php index ff2c24ae9d7..66c2577dcdc 100644 --- a/lib/private/group/database.php +++ b/lib/private/group/database.php @@ -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; @@ -211,4 +211,43 @@ class OC_Group_Database extends OC_Group_Backend { return $users; } + /** + * @brief get the number of all users matching the search string in a group + * @param string $gid + * @param string $search + * @param int $limit + * @param int $offset + * @return int | false + */ + 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(); + } + + /** + * @brief get a list of all display names in a group + * @param string $gid + * @param string $search + * @param int $limit + * @param int $offset + * @return array with display names (value) and user ids (key) + */ + public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) { + $displayNames = array(); + + $stmt = OC_DB::prepare('SELECT `*PREFIX*users`.`uid`, `*PREFIX*users`.`displayname`' + .' FROM `*PREFIX*users`' + .' INNER JOIN `*PREFIX*group_user` ON `*PREFIX*group_user`.`uid` = `*PREFIX*users`.`uid`' + .' WHERE `gid` = ? AND `*PREFIX*group_user`.`uid` LIKE ?', + $limit, + $offset); + $result = $stmt->execute(array($gid, $search.'%')); + $users = array(); + while ($row = $result->fetchRow()) { + $displayName = trim($row['displayname'], ' '); + $displayNames[$row['uid']] = empty($displayName) ? $row['uid'] : $displayName; + } + return $displayNames; + } } diff --git a/lib/private/group/dummy.php b/lib/private/group/dummy.php index 8dad769f9c6..a502800b16a 100644 --- a/lib/private/group/dummy.php +++ b/lib/private/group/dummy.php @@ -175,6 +175,14 @@ class OC_Group_Dummy extends OC_Group_Backend { } } - + /** + * @brief get the number of all users in a group + * @returns int | bool + */ + public function countUsersInGroup($gid, $search = '', $limit = -1, $offset = 0) { + if(isset($this->groups[$gid])) { + return count($this->groups[$gid]); + } + } } diff --git a/lib/private/group/group.php b/lib/private/group/group.php index ae8eeacb23a..7593bb68d1a 100644 --- a/lib/private/group/group.php +++ b/lib/private/group/group.php @@ -18,7 +18,12 @@ class Group { /** * @var \OC\User\User[] $users */ - private $users; + private $users = array(); + + /** + * @var bool $usersLoaded + */ + private $usersLoaded; /** * @var \OC_Group_Backend[] | \OC_Group_Database[] $backend @@ -26,7 +31,7 @@ class Group { private $backends; /** - * @var \OC\Hooks\PublicEmitter $emitter; + * @var \OC\Hooks\PublicEmitter $emitter */ private $emitter; @@ -58,7 +63,7 @@ class Group { * @return \OC\User\User[] */ public function getUsers() { - if ($this->users) { + if ($this->usersLoaded) { return $this->users; } @@ -74,6 +79,7 @@ class Group { } $this->users = $this->getVerifiedUsers($userIds); + $this->usersLoaded = true; return $this->users; } @@ -84,8 +90,12 @@ class Group { * @return bool */ public function inGroup($user) { + if (isset($this->users[$user->getUID()])) { + return true; + } foreach ($this->backends as $backend) { if ($backend->inGroup($user->getUID(), $this->gid)) { + $this->users[$user->getUID()] = $user; return true; } } @@ -171,6 +181,27 @@ class Group { } /** + * returns the number of users matching the search string + * + * @param string $search + * @return int | bool + */ + public function count($search) { + $users = false; + foreach ($this->backends as $backend) { + if($backend->implementsActions(OC_GROUP_BACKEND_COUNT_USERS)) { + if($users === false) { + //we could directly add to a bool variable, but this would + //be ugly + $users = 0; + } + $users += $backend->countUsersInGroup($this->gid, $search); + } + } + return $users; + } + + /** * search for users in the group by displayname * * @param string $search @@ -179,6 +210,7 @@ class Group { * @return \OC\User\User[] */ public function searchDisplayName($search, $limit = null, $offset = null) { + $users = array(); foreach ($this->backends as $backend) { $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); $users = $this->getVerifiedUsers($userIds); @@ -213,17 +245,17 @@ class Group { /** * @brief returns all the Users from an array that really exists - * @param $userIds an array containing user IDs - * @return an Array with the userId as Key and \OC\User\User as value + * @param string[] $userIds an array containing user IDs + * @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value */ private function getVerifiedUsers($userIds) { - if(!is_array($userIds)) { + if (!is_array($userIds)) { return array(); } $users = array(); foreach ($userIds as $userId) { $user = $this->userManager->get($userId); - if(!is_null($user)) { + if (!is_null($user)) { $users[$userId] = $user; } } diff --git a/lib/private/group/manager.php b/lib/private/group/manager.php index e5d3076b7eb..2a2fa5a67f7 100644 --- a/lib/private/group/manager.php +++ b/lib/private/group/manager.php @@ -76,12 +76,7 @@ class Manager extends PublicEmitter { if (isset($this->cachedGroups[$gid])) { return $this->cachedGroups[$gid]; } - foreach ($this->backends as $backend) { - if ($backend->groupExists($gid)) { - return $this->getGroupObject($gid); - } - } - return null; + return $this->getGroupObject($gid); } protected function getGroupObject($gid) { @@ -91,6 +86,9 @@ class Manager extends PublicEmitter { $backends[] = $backend; } } + if (count($backends) === 0) { + return null; + } $this->cachedGroups[$gid] = new Group($gid, $backends, $this->userManager, $this); return $this->cachedGroups[$gid]; } @@ -108,10 +106,10 @@ class Manager extends PublicEmitter { * @return \OC\Group\Group */ public function createGroup($gid) { - if (!$gid) { + if ($gid === '' || is_null($gid)) { return false; - } else if ($this->groupExists($gid)) { - return $this->get($gid); + } else if ($group = $this->get($gid)) { + return $group; } else { $this->emit('\OC\Group', 'preCreate', array($gid)); foreach ($this->backends as $backend) { @@ -174,23 +172,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; } |