Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make search function use search_with_scope (#22) #28

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading