}
/**
- * 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 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 = [];
+ // part for counting users (see countUsers in user backend)
+ // it is consolidated in OC 8. No big changes for OC 7.
+ $filterParts[] = \OCP\Util::mb_str_replace(
+ '%uid', '*', $this->access->connection->ldapLoginFilter, 'UTF-8');
+ if(!empty($search)) {
+ $search = $this->access->escapeFilterPart($search, true);
+ $filterParts[] = $this->access->getFilterPartForUserSearch($search);
+ }
+ $filterParts[] = 'primaryGroupID=' . $groupID;
+
+ $filter = $this->access->combineFilterWithAnd($filterParts);
- $users = $this->access->fetchListOfUsers(
- $filter,
- array($this->access->connection->ldapUserDisplayName, 'dn'),
- $limit,
- $offset
- );
+ return $filter;
+ }
+
+ /**
+ * 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 string $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 (int)$users;
+ } catch (\Exception $e) {
+ return 0;
+ }
}
/**
if(!$this->groupExists($gid)) {
return array();
}
+ $search = $this->access->escapeFilterPart($search, true);
$cacheKey = 'usersInGroup-'.$gid.'-'.$search.'-'.$limit.'-'.$offset;
// check for cache of the exact query
$groupUsers = $this->access->connection->getFromCache($cacheKey);
$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);
}
if(empty($search)) {
- $groupUsers = count($members);
+ $primaryUsers = $this->countUsersInPrimaryGroup($groupDN, '');
+ $groupUsers = count($members) + $primaryUsers;
+
$this->access->connection->writeToCache($cacheKey, $groupUsers);
return $groupUsers;
}
+ $search = $this->access->escapeFilterPart($search, true);
$isMemberUid =
(strtolower($this->access->connection->ldapGroupMemberAssocAttr)
=== 'memberuid');
}
//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;
}
/**
if(!$this->enabled) {
return array();
}
+ $search = $this->access->escapeFilterPart($search, true);
$pagingSize = $this->access->connection->ldapPagingSize;
if ((! $this->access->connection->hasPagedResultSupport)
|| empty($pagingSize)) {
}
$base = $this->configuration->ldapBase[0];
foreach($cns as $cn) {
- $rr = $this->ldap->search($cr, $base, 'cn=' . $cn, array('dn'));
+ $rr = $this->ldap->search($cr, $base, 'cn=' . $cn, array('dn', 'primaryGroupToken'));
if(!$this->ldap->isResource($rr)) {
continue;
}
$er = $this->ldap->firstEntry($cr, $rr);
+ $attrs = $this->ldap->getAttributes($cr, $er);
$dn = $this->ldap->getDN($cr, $er);
- $filter .= '(memberof=' . $dn . ')';
+ if(empty($dn)) {
+ continue;
+ }
+ $filterPart = '(memberof=' . $dn . ')';
+ if(isset($attrs['primaryGroupToken'])) {
+ $pgt = $attrs['primaryGroupToken'][0];
+ $primaryFilterPart = '(primaryGroupID=' . $pgt .')';
+ $filterPart = '(|' . $filterPart . $primaryFilterPart . ')';
+ }
+ $filter .= $filterPart;
}
$filter .= ')';
}