Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArthur Schiwon <blizzz@owncloud.com>2015-01-28 17:52:59 +0300
committerArthur Schiwon <blizzz@owncloud.com>2015-07-23 15:33:30 +0300
commit5d10c37b08c8df859eab5f7b338a82291951095f (patch)
treeaf32696edbc0c503ac322fd75eee7db10400b557
parent0d5b4b95ef030e3ecc265c1e54673ca9df17dab4 (diff)
fix counting of users in primary group
-rw-r--r--apps/user_ldap/group_ldap.php86
-rw-r--r--apps/user_ldap/tests/group_ldap.php7
2 files changed, 69 insertions, 24 deletions
diff --git a/apps/user_ldap/group_ldap.php b/apps/user_ldap/group_ldap.php
index cba19f3791c..916d5f97649 100644
--- a/apps/user_ldap/group_ldap.php
+++ b/apps/user_ldap/group_ldap.php
@@ -249,32 +249,71 @@ class GROUP_LDAP extends BackendUtility implements \OCP\GroupInterface {
}
/**
- * returns a list of users that have the given group as primary group
+ * returns a filter for a "users in primary group" search or count operation
*
- * @param string $groupDN
- * @param $limit
- * @param int $offset
- * @return string[]
+ * @param $groupDN
+ * @param string $search
+ * @return string
+ * @throws \Exception
*/
- public function getUsersInPrimaryGroup($groupDN, $limit = -1, $offset = 0) {
+ private function prepareFilterForUsersInPrimaryGroup($groupDN, $search = '') {
$groupID = $this->getGroupPrimaryGroupID($groupDN);
if($groupID === false) {
- return array();
+ throw new \Exception('Not a valid group');
}
- $filter = $this->access->combineFilterWithAnd(array(
- $this->access->connection->ldapUserFilter,
- 'primaryGroupID=' . $groupID
- ));
+ $filterParts = [];
+ $filterParts[] = $this->access->getFilterForUserCount();
+ if(!empty($search)) {
+ $filterParts[] = $this->access->getFilterPartForUserSearch($search);
+ }
+ $filterParts[] = 'primaryGroupID=' . $groupID;
+
+ $filter = $this->access->combineFilterWithAnd($filterParts);
+
+ return $filter;
+ }
- $users = $this->access->fetchListOfUsers(
- $filter,
- array($this->access->connection->ldapUserDisplayName, 'dn'),
- $limit,
- $offset
- );
+ /**
+ * returns a list of users that have the given group as primary group
+ *
+ * @param string $groupDN
+ * @param string $search
+ * @param int $limit
+ * @param int $offset
+ * @return \string[]
+ */
+ 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();
+ }
+ }
- 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 8d15806be28..f2b16ee2eab 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() {