Skip to content

Commit

Permalink
make search function use search_with_scope (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Coutadeur authored and davidcoutadeur committed Jul 19, 2024
1 parent b13ef0b commit e505816
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
18 changes: 8 additions & 10 deletions src/Ltb/Ldap.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function search_with_scope(string $scope = "sub", ...$searchargs)
}
}

function search($ldap_filter,$attributes, $attributes_map, $search_result_title, $search_result_sortby, $search_result_items)
function search($ldap_filter,$attributes, $attributes_map, $search_result_title, $search_result_sortby, $search_result_items, $search_scope = "sub")
{

$result = "";
Expand All @@ -115,11 +115,9 @@ function search($ldap_filter,$attributes, $attributes_map, $search_result_title,

# Connect to LDAP
$ldap_connection = $this->connect();

$ldap = $ldap_connection[0];
$result = $ldap_connection[1];

if ($ldap) {
if ($this->ldap) {

foreach( $search_result_items as $item ) {
$attributes[] = $attributes_map[$item]['attribute'];
Expand All @@ -128,25 +126,25 @@ function search($ldap_filter,$attributes, $attributes_map, $search_result_title,
$attributes[] = $attributes_map[$search_result_sortby]['attribute'];

# Search for users
$search = \Ltb\PhpLDAP::ldap_search($ldap, $this->ldap_user_base, $ldap_filter, $attributes, 0, $this->ldap_size_limit);
$search = $this->search_with_scope($search_scope, $this->ldap_user_base, $ldap_filter, $attributes, 0, $this->ldap_size_limit);

$errno = \Ltb\PhpLDAP::ldap_errno($ldap);
$errno = \Ltb\PhpLDAP::ldap_errno($this->ldap);

if ( $errno == 4) {
$size_limit_reached = true;
}
if ( $errno != 0 and $errno !=4 ) {
$result = "ldaperror";
error_log("LDAP - Search error $errno (".\Ltb\PhpLDAP::ldap_error($ldap).")");
error_log("LDAP - Search error $errno (".\Ltb\PhpLDAP::ldap_error($this->ldap).")");
} else {

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

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

# Sort entries
if (isset($search_result_sortby)) {
Expand All @@ -159,7 +157,7 @@ function search($ldap_filter,$attributes, $attributes_map, $search_result_title,
}
}

return [$ldap,$result,$nb_entries,$entries,$size_limit_reached];
return [$this->ldap,$result,$nb_entries,$entries,$size_limit_reached];

}

Expand Down
5 changes: 3 additions & 2 deletions tests/Ltb/LdapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function test_search(): void
$search_result_title = "fullname";
$search_result_sortby = "lastname";
$search_result_items = array('identifier', 'mail', 'mobile');

$search_scope = "sub";

$entries = [
'count' => 2,
Expand Down Expand Up @@ -200,7 +200,8 @@ public function test_search(): void
$attributes_map,
$search_result_title,
$search_result_sortby,
$search_result_items
$search_result_items,
$search_scope
);

$this->assertEquals("ldap_connection", $ldap, "Error while getting ldap_connection in search function");
Expand Down

0 comments on commit e505816

Please sign in to comment.