Skip to content

Commit

Permalink
add page size parameter in search function (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Coutadeur committed Sep 18, 2024
1 parent 2469ffb commit dbeee9f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
54 changes: 45 additions & 9 deletions src/Ltb/Ldap.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Ldap {
public $ldap_user_base = null;
public $ldap_size_limit = null;
public $ldap_krb5ccname = null;
public $ldap_page_size = 0;

public function __construct(
$ldap_url,
Expand All @@ -24,7 +25,8 @@ public function __construct(
$ldap_network_timeout,
$ldap_user_base,
$ldap_size_limit,
$ldap_krb5ccname
$ldap_krb5ccname,
$ldap_page_size = 0
)
{
$this->ldap_url = $ldap_url;
Expand All @@ -35,6 +37,7 @@ public function __construct(
$this->ldap_user_base = $ldap_user_base;
$this->ldap_size_limit = $ldap_size_limit;
$this->ldap_krb5ccname = $ldap_krb5ccname;
$this->ldap_page_size = $ldap_page_size;

}

Expand Down Expand Up @@ -125,10 +128,47 @@ function search($ldap_filter,$attributes, $attributes_map, $search_result_title,
$attributes[] = $attributes_map[$search_result_title]['attribute'];
$attributes[] = $attributes_map[$search_result_sortby]['attribute'];

# Search for users
$search = $this->search_with_scope($search_scope, $this->ldap_user_base, $ldap_filter, $attributes, 0, $this->ldap_size_limit);

$errno = \Ltb\PhpLDAP::ldap_errno($this->ldap);
$cookie = "";
do {
$controls = null;
if($this->ldap_page_size != 0)
{
$controls = [[
'oid' => LDAP_CONTROL_PAGEDRESULTS,
'value' => [
'size' => $this->ldap_page_size,
'cookie' => $cookie]
]];
}

# Search for users
$search = $this->search_with_scope($search_scope,
$this->ldap_user_base,
$ldap_filter,
$attributes,
0,
$this->ldap_size_limit,
-1,
LDAP_DEREF_NEVER,
$controls );

$errno = null;
$matcheddn = null;
$errmsg = null;
$referrals = null;
\Ltb\PhpLDAP::ldap_parse_result($this->ldap, $search, $errno, $matcheddn, $errmsg, $referrals, $controls);

if($errno != 0)
{
# if any error occurs, stop the search loop and treat error
break;
}

$nb_entries += \Ltb\PhpLDAP::ldap_count_entries($this->ldap, $search);
$entries = array_merge($entries, \Ltb\PhpLDAP::ldap_get_entries($this->ldap, $search));
$entries["count"] = $nb_entries;

} while (!empty($cookie) && $this->ldap_page_size != 0);

if ( $errno == 4) {
$size_limit_reached = true;
Expand All @@ -138,13 +178,9 @@ function search($ldap_filter,$attributes, $attributes_map, $search_result_title,
error_log("LDAP - Search error $errno (".\Ltb\PhpLDAP::ldap_error($this->ldap).")");
} else {

# Get search results
$nb_entries = \Ltb\PhpLDAP::ldap_count_entries($this->ldap, $search);

if ($nb_entries === 0) {
$result = "noentriesfound";
} else {
$entries = \Ltb\PhpLDAP::ldap_get_entries($this->ldap, $search);

# Sort entries
if (isset($search_result_sortby)) {
Expand Down
10 changes: 6 additions & 4 deletions tests/Ltb/LdapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,15 @@ public function test_search(): void
"(objectClass=inetOrgPerson)",
[0 => 'cn', 1 => 'sn', 2 => 'uid', 3 => 'mail', 4 => 'mobile', 5 => 'cn', 6 => 'sn'],
0,
$this->ldap_size_limit
$this->ldap_size_limit,
-1,
0,
null
)
->andReturn("ldap_search_result");

$phpLDAPMock->shouldreceive('ldap_errno')
->with("ldap_connection")
->andReturn(0);
$phpLDAPMock->shouldreceive('ldap_parse_result')
->with("ldap_connection", "ldap_search_result", null, null, null, null, null);

$phpLDAPMock->shouldreceive('ldap_count_entries')
->with("ldap_connection", "ldap_search_result")
Expand Down

0 comments on commit dbeee9f

Please sign in to comment.