From 18ccc00454140e9e9170a437331ac4bb1cbb9f25 Mon Sep 17 00:00:00 2001 From: David Coutadeur Date: Fri, 19 Jul 2024 18:13:09 +0200 Subject: [PATCH] make search function use search_with_scope (#22) --- src/Ltb/Ldap.php | 18 ++++++++---------- tests/Ltb/LdapTest.php | 5 +++-- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Ltb/Ldap.php b/src/Ltb/Ldap.php index f1feb80..ca019cd 100644 --- a/src/Ltb/Ldap.php +++ b/src/Ltb/Ldap.php @@ -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 = ""; @@ -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']; @@ -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)) { @@ -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]; } diff --git a/tests/Ltb/LdapTest.php b/tests/Ltb/LdapTest.php index db2d87c..ea2b42d 100644 --- a/tests/Ltb/LdapTest.php +++ b/tests/Ltb/LdapTest.php @@ -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, @@ -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");