From 60edf98c151ad678abe6e9dae292896946a97630 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Fri, 14 Mar 2014 13:51:17 +0100 Subject: remove OC_GROUP_BACKEND_GET_DISPLAYNAME option for group backends --- lib/private/group/backend.php | 4 ++-- lib/private/group/group.php | 5 +---- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/private/group/backend.php b/lib/private/group/backend.php index 2e17b5d0b7f..4604e1cc415 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); +// deprecated, do not use 0x00010000 for new features though. +// define('OC_GROUP_BACKEND_GET_DISPLAYNAME', 0x00010000); /** * Abstract base class for user management @@ -44,7 +45,6 @@ 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', ); /** diff --git a/lib/private/group/group.php b/lib/private/group/group.php index d5a17f6d215..851eb582698 100644 --- a/lib/private/group/group.php +++ b/lib/private/group/group.php @@ -180,10 +180,7 @@ class Group { */ public function searchDisplayName($search, $limit = null, $offset = null) { foreach ($this->backends as $backend) { - if ($backend->implementsActions(OC_GROUP_BACKEND_GET_DISPLAYNAME)) { - $userIds = array_keys($backend->displayNamesInGroup($this->gid, $search, $limit, $offset)); - } else { - $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); + $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); } $users = $this->getVerifiedUsers($userIds); if (!is_null($limit) and $limit <= 0) { -- cgit v1.2.3 From 8d00f1ca1f2bfe5399554b6b2895d5168c3f4058 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Fri, 14 Mar 2014 13:52:54 +0100 Subject: LDAP: getDisplayNamesInGroup is not an option for group backends anymore --- apps/user_ldap/group_ldap.php | 21 +-------------------- apps/user_ldap/group_proxy.php | 16 ---------------- 2 files changed, 1 insertion(+), 36 deletions(-) diff --git a/apps/user_ldap/group_ldap.php b/apps/user_ldap/group_ldap.php index 32e2cec5960..30217412611 100644 --- a/apps/user_ldap/group_ldap.php +++ b/apps/user_ldap/group_ldap.php @@ -218,25 +218,6 @@ class GROUP_LDAP extends BackendUtility implements \OCP\GroupInterface { return $groupUsers; } - /** - * @brief get a list of all display names in a group - * @returns array with display names (value) and user ids(key) - */ - public function displayNamesInGroup($gid, $search, $limit, $offset) { - if(!$this->enabled) { - return array(); - } - if(!$this->groupExists($gid)) { - return array(); - } - $users = $this->usersInGroup($gid, $search, $limit, $offset); - $displayNames = array(); - foreach($users as $user) { - $displayNames[$user] = \OC_User::getDisplayName($user); - } - return $displayNames; - } - /** * @brief get a list of all groups * @returns array with group names @@ -318,6 +299,6 @@ class GROUP_LDAP extends BackendUtility implements \OCP\GroupInterface { * compared with OC_USER_BACKEND_CREATE_USER etc. */ public function implementsActions($actions) { - return (bool)(OC_GROUP_BACKEND_GET_DISPLAYNAME & $actions); + return false; } } diff --git a/apps/user_ldap/group_proxy.php b/apps/user_ldap/group_proxy.php index 4404bd7fe3a..97b342c1183 100644 --- a/apps/user_ldap/group_proxy.php +++ b/apps/user_ldap/group_proxy.php @@ -144,22 +144,6 @@ class Group_Proxy extends lib\Proxy implements \OCP\GroupInterface { return $users; } - /** - * @brief get a list of all display names in a group - * @returns array with display names (value) and user ids(key) - */ - public function displayNamesInGroup($gid, $search, $limit, $offset) { - $displayNames = array(); - - foreach($this->backends as $backend) { - $backendUsers = $backend->displayNamesInGroup($gid, $search, $limit, $offset); - if (is_array($backendUsers)) { - $displayNames = array_merge($displayNames, $backendUsers); - } - } - return $displayNames; - } - /** * @brief get a list of all groups * @returns array with group names -- cgit v1.2.3 From 649233e54cd1db432d78c4a56352fe5af97be869 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Fri, 14 Mar 2014 13:54:22 +0100 Subject: clean up group backends --- lib/private/group/backend.php | 19 ------------------- lib/private/group/database.php | 26 -------------------------- 2 files changed, 45 deletions(-) diff --git a/lib/private/group/backend.php b/lib/private/group/backend.php index 4604e1cc415..7a41aff8b52 100644 --- a/lib/private/group/backend.php +++ b/lib/private/group/backend.php @@ -135,23 +135,4 @@ abstract class OC_Group_Backend implements OC_Group_Interface { public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { return array(); } - - /** - * @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(); - $users = $this->usersInGroup($gid, $search, $limit, $offset); - foreach ($users as $user) { - $displayNames[$user] = $user; - } - - return $displayNames; - } - } diff --git a/lib/private/group/database.php b/lib/private/group/database.php index d0974685ff6..fa43d4e12fa 100644 --- a/lib/private/group/database.php +++ b/lib/private/group/database.php @@ -210,30 +210,4 @@ class OC_Group_Database extends OC_Group_Backend { } return $users; } - - /** - * @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; - } } -- cgit v1.2.3 From 637aa5619798b09d54dd4642214e72079272c243 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Fri, 14 Mar 2014 13:57:25 +0100 Subject: remove now unnecessary test --- tests/lib/group.php | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/tests/lib/group.php b/tests/lib/group.php index 8de8d033e19..26232187c36 100644 --- a/tests/lib/group.php +++ b/tests/lib/group.php @@ -109,25 +109,6 @@ class Test_Group extends PHPUnit_Framework_TestCase { $this->assertEquals(array(), OC_Group::getGroups()); } - public function testDisplayNamesInGroup() { - OC_Group::useBackend(new OC_Group_Dummy()); - $userBackend = new \OC_User_Dummy(); - \OC_User::getManager()->registerBackend($userBackend); - - $group1 = uniqid(); - $user1 = 'uid1'; - $user2 = 'uid2'; - OC_Group::createGroup($group1); - $userBackend->createUser($user1, ''); - $userBackend->createUser($user2, ''); - OC_Group::addToGroup($user1, $group1); - OC_Group::addToGroup($user2, $group1); - //Dummy backend does not support setting displaynames, uid will always - //be returned. This checks primarily, that the return format is okay. - $expected = array($user1 => $user1, $user2 => $user2); - $this->assertEquals($expected, OC_Group::displayNamesInGroup($group1)); - } - public function testUsersInGroup() { OC_Group::useBackend(new OC_Group_Dummy()); $userBackend = new \OC_User_Dummy(); -- cgit v1.2.3 From f93ab1105f044b098395d099646747992ef565cb Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Sat, 15 Mar 2014 06:03:28 +0100 Subject: implement getDisplayNames in group manager --- lib/private/group.php | 12 +----------- lib/private/group/manager.php | 33 +++++++++++++++++++++++++++++++++ lib/private/user/manager.php | 4 ++-- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/lib/private/group.php b/lib/private/group.php index 444788c97f1..b45855c9215 100644 --- a/lib/private/group.php +++ b/lib/private/group.php @@ -263,17 +263,7 @@ class OC_Group { * @returns array with display names (value) and user ids(key) */ public static function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) { - $group = self::getManager()->get($gid); - if ($group) { - $users = $group->searchDisplayName($search, $limit, $offset); - $displayNames = array(); - foreach ($users as $user) { - $displayNames[$user->getUID()] = $user->getDisplayName(); - } - return $displayNames; - } else { - return array(); - } + return self::getManager()->displayNamesInGroup($gid, $search, $limit, $offset); } /** diff --git a/lib/private/group/manager.php b/lib/private/group/manager.php index 454e2b63b47..611af0a40fb 100644 --- a/lib/private/group/manager.php +++ b/lib/private/group/manager.php @@ -160,4 +160,37 @@ class Manager extends PublicEmitter { } return array_values($groups); } + + /** + * @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) { + $group = $this->get($gid); + if(is_null($group)) { + return array(); + } + // only user backends have the capability to do a complex search for users + $groupUsers = $group->searchUsers('', $limit, $offset); + if(!empty(trim($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; + } else { + $filteredUsers = array(); + $testUsers = false; + } + + $matchingUsers = array(); + foreach($groupUsers as $user) { + if(!$testUsers || isset($filteredUsers[$user->getUID()])) { + $matchingUsers[$user->getUID()] = $user->getDisplayName(); + } + } + return $matchingUsers; + } } diff --git a/lib/private/user/manager.php b/lib/private/user/manager.php index 61abb00f995..d23ef381bec 100644 --- a/lib/private/user/manager.php +++ b/lib/private/user/manager.php @@ -174,12 +174,12 @@ class Manager extends PublicEmitter { $backendUsers = $backend->getUsers($pattern, $limit, $offset); if (is_array($backendUsers)) { foreach ($backendUsers as $uid) { - $users[] = $this->getUserObject($uid, $backend); + $users[$uid] = $this->getUserObject($uid, $backend); } } } - usort($users, function ($a, $b) { + uasort($users, function ($a, $b) { /** * @var \OC\User\User $a * @var \OC\User\User $b -- cgit v1.2.3 From af45da94b363f3989e9d579b877e0a4245dbdd42 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Sat, 15 Mar 2014 06:41:46 +0100 Subject: adjust user manager tests --- tests/lib/user/manager.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/lib/user/manager.php b/tests/lib/user/manager.php index 8ca0f94c6fa..fd0931af7e4 100644 --- a/tests/lib/user/manager.php +++ b/tests/lib/user/manager.php @@ -190,8 +190,8 @@ class Manager extends \PHPUnit_Framework_TestCase { $result = $manager->search('fo'); $this->assertEquals(2, count($result)); - $this->assertEquals('afoo', $result[0]->getUID()); - $this->assertEquals('foo', $result[1]->getUID()); + $this->assertEquals('afoo', array_shift($result)->getUID()); + $this->assertEquals('foo', array_shift($result)->getUID()); } public function testSearchTwoBackendLimitOffset() { @@ -219,9 +219,9 @@ class Manager extends \PHPUnit_Framework_TestCase { $result = $manager->search('fo', 3, 1); $this->assertEquals(3, count($result)); - $this->assertEquals('foo1', $result[0]->getUID()); - $this->assertEquals('foo2', $result[1]->getUID()); - $this->assertEquals('foo3', $result[2]->getUID()); + $this->assertEquals('foo1', array_shift($result)->getUID()); + $this->assertEquals('foo2', array_shift($result)->getUID()); + $this->assertEquals('foo3', array_shift($result)->getUID()); } public function testCreateUserSingleBackendNotExists() { -- cgit v1.2.3 From 6aeb5996b63cd542bb75ce139700eee27d13a620 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Sat, 15 Mar 2014 08:11:10 +0100 Subject: test for group manager's displayNamesInGroup --- tests/lib/group/manager.php | 50 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/lib/group/manager.php b/tests/lib/group/manager.php index 830eb1b48bf..18be3eb96c1 100644 --- a/tests/lib/group/manager.php +++ b/tests/lib/group/manager.php @@ -310,4 +310,54 @@ class Manager extends \PHPUnit_Framework_TestCase { $this->assertEquals('group1', $group1->getGID()); $this->assertEquals('group2', $group2->getGID()); } + + public function testDisplayNamesInGroupMultipleUserBackends() { + $user1 = new User('user1', null); + $user2 = new User('user2', null); + $user3 = new User('user3', null); + $user4 = new User('user33', null); + + /** + * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1 + */ + $backend = $this->getMock('\OC_Group_Database'); + $backend->expects($this->exactly(2)) + ->method('groupExists') + ->with('testgroup') + ->will($this->returnValue(true)); + + $backend->expects($this->once()) + ->method('usersInGroup') + ->with('testgroup', '', -1, 0) + ->will($this->returnValue(array('user2', 'user33'))); + + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $userManager->expects($this->once()) + ->method('search') + ->with('user3') + ->will($this->returnValue(array('user3' => $user3, 'user33' => $user4))); + + $userManager->expects($this->any()) + ->method('get') + ->will($this->returnCallback(function($uid) { + switch($uid) { + case 'user1' : return new User('user1', null); + case 'user2' : return new User('user2', null); + case 'user3' : return new User('user3', null); + case 'user33': return new User('user33', null); + default: + return null; + } + })); + + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend); + + $users = $manager->displayNamesInGroup('testgroup', 'user3'); + $this->assertEquals(1, count($users)); + $this->assertTrue(isset($users['user33'])); + } } -- cgit v1.2.3 From 72b90816faba41b7bbf22973e826010fa16be257 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Sat, 15 Mar 2014 10:32:16 +0100 Subject: trim must not be used in empty in PHP < 5.5 --- lib/private/group/manager.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/private/group/manager.php b/lib/private/group/manager.php index 611af0a40fb..e5d3076b7eb 100644 --- a/lib/private/group/manager.php +++ b/lib/private/group/manager.php @@ -176,7 +176,8 @@ class Manager extends PublicEmitter { } // only user backends have the capability to do a complex search for users $groupUsers = $group->searchUsers('', $limit, $offset); - if(!empty(trim($search))) { + $search = trim($search); + 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; -- cgit v1.2.3 From fcffccf0181271afd185af774a8f86bbd56c2700 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Mon, 14 Apr 2014 17:54:14 +0200 Subject: keep the constant to not provoke PHP warnings --- lib/private/group/backend.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/private/group/backend.php b/lib/private/group/backend.php index 7a41aff8b52..11ec6c2920d 100644 --- a/lib/private/group/backend.php +++ b/lib/private/group/backend.php @@ -33,8 +33,7 @@ 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); -// deprecated, do not use 0x00010000 for new features though. -// define('OC_GROUP_BACKEND_GET_DISPLAYNAME', 0x00010000); +define('OC_GROUP_BACKEND_GET_DISPLAYNAME', 0x00010000); //OBSOLETE /** * Abstract base class for user management -- cgit v1.2.3 From 0a9487ec801c640bfdb59c037b4ca3f4ea95fe0a Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Wed, 23 Apr 2014 13:55:21 +0200 Subject: fix after rebase --- lib/private/group/group.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/private/group/group.php b/lib/private/group/group.php index 851eb582698..ae8eeacb23a 100644 --- a/lib/private/group/group.php +++ b/lib/private/group/group.php @@ -181,7 +181,6 @@ class Group { public function searchDisplayName($search, $limit = null, $offset = null) { foreach ($this->backends as $backend) { $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); - } $users = $this->getVerifiedUsers($userIds); if (!is_null($limit) and $limit <= 0) { return array_values($users); -- cgit v1.2.3