From 31de757514ce6b23587161c6e1b15016df11e02f Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Wed, 28 Jan 2015 15:52:59 +0100 Subject: [PATCH] fix counting of users in primary group --- apps/user_ldap/group_ldap.php | 86 +++++++++++++++++++++-------- apps/user_ldap/tests/group_ldap.php | 7 ++- 2 files changed, 69 insertions(+), 24 deletions(-) diff --git a/apps/user_ldap/group_ldap.php b/apps/user_ldap/group_ldap.php index e8d268d3df..b2796aaa61 100644 --- a/apps/user_ldap/group_ldap.php +++ b/apps/user_ldap/group_ldap.php @@ -248,33 +248,72 @@ class GROUP_LDAP extends BackendUtility implements \OCP\GroupInterface { return $this->getEntryGroupID($dn, 'primaryGroupID'); } + /** + * returns a filter for a "users in primary group" search or count operation + * + * @param $groupDN + * @param string $search + * @return string + * @throws \Exception + */ + private function prepareFilterForUsersInPrimaryGroup($groupDN, $search = '') { + $groupID = $this->getGroupPrimaryGroupID($groupDN); + if($groupID === false) { + throw new \Exception('Not a valid group'); + } + + $filterParts = []; + $filterParts[] = $this->access->getFilterForUserCount(); + if(!empty($search)) { + $filterParts[] = $this->access->getFilterPartForUserSearch($search); + } + $filterParts[] = 'primaryGroupID=' . $groupID; + + $filter = $this->access->combineFilterWithAnd($filterParts); + + return $filter; + } + /** * returns a list of users that have the given group as primary group * * @param string $groupDN - * @param $limit + * @param string $search + * @param int $limit * @param int $offset - * @return string[] + * @return \string[] */ - public function getUsersInPrimaryGroup($groupDN, $limit = -1, $offset = 0) { - $groupID = $this->getGroupPrimaryGroupID($groupDN); - if($groupID === false) { + public function getUsersInPrimaryGroup($groupDN, $search = '', $limit = -1, $offset = 0) { + try { + $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search); + return $this->access->fetchListOfUsers( + $filter, + array($this->access->connection->ldapUserDisplayName, 'dn'), + $limit, + $offset + ); + } catch (\Exception $e) { return array(); } + } - $filter = $this->access->combineFilterWithAnd(array( - $this->access->connection->ldapUserFilter, - 'primaryGroupID=' . $groupID - )); - - $users = $this->access->fetchListOfUsers( - $filter, - array($this->access->connection->ldapUserDisplayName, 'dn'), - $limit, - $offset - ); - - return $users; + /** + * returns the number of users that have the given group as primary group + * + * @param $groupDN + * @param string $search + * @param int $limit + * @param int $offset + * @return int + */ + public function countUsersInPrimaryGroup($groupDN, $search = '', $limit = -1, $offset = 0) { + try { + $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search); + $users = $this->access->countUsers($filter, array('dn'), $limit, $offset); + return (is_int($users)) ? $users : 0; + } catch (\Exception $e) { + return 0; + } } /** @@ -473,7 +512,7 @@ class GROUP_LDAP extends BackendUtility implements \OCP\GroupInterface { $groupUsers = array_slice($groupUsers, $offset, $limit); //and get users that have the group as primary - $primaryUsers = $this->getUsersInPrimaryGroup($groupDN, $limit, $offset); + $primaryUsers = $this->getUsersInPrimaryGroup($groupDN, $search, $limit, $offset); $groupUsers = array_unique(array_merge($groupUsers, $primaryUsers)); $this->access->connection->writeToCache($cacheKey, $groupUsers); @@ -512,7 +551,9 @@ class GROUP_LDAP extends BackendUtility implements \OCP\GroupInterface { } if(empty($search)) { - $groupUsers = count($members); + $primaryUsers = $this->countUsersInPrimaryGroup($groupDN, ''); + $groupUsers = count($members) + $primaryUsers; + $this->access->connection->writeToCache($cacheKey, $groupUsers); return $groupUsers; } @@ -557,10 +598,9 @@ class GROUP_LDAP extends BackendUtility implements \OCP\GroupInterface { } //and get users that have the group as primary - $primaryUsers = $this->getUsersInPrimaryGroup($groupDN); - $groupUsers = array_unique(array_merge($groupUsers, $primaryUsers)); + $primaryUsers = $this->countUsersInPrimaryGroup($groupDN, $search); - return count($groupUsers); + return count($groupUsers) + $primaryUsers; } /** diff --git a/apps/user_ldap/tests/group_ldap.php b/apps/user_ldap/tests/group_ldap.php index efd7f803f3..44f0f82c2f 100644 --- a/apps/user_ldap/tests/group_ldap.php +++ b/apps/user_ldap/tests/group_ldap.php @@ -76,10 +76,15 @@ class Test_Group_Ldap extends \Test\TestCase { ->method('readAttribute') ->will($this->returnValue(array('u11', 'u22', 'u33', 'u34'))); + // for primary groups + $access->expects($this->once()) + ->method('countUsers') + ->will($this->returnValue(2)); + $groupBackend = new GroupLDAP($access); $users = $groupBackend->countUsersInGroup('group'); - $this->assertSame(4, $users); + $this->assertSame(6, $users); } public function testCountWithSearchString() {