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

add a search function with scope (#17) #18

Merged
merged 1 commit into from
Jul 10, 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
add a search function with scope (#17)
  • Loading branch information
David Coutadeur committed Jul 10, 2024
commit 343d3cd523dc1e3c0bbdd27dfbc67d272bf4c124
28 changes: 28 additions & 0 deletions src/Ltb/Ldap.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,34 @@ function connect() {
return array($ldap, false);
}

# Function that call ldap_search, ldap_read, or ldap_list
# depending on the given scope
# Expected arguments: scope + same list as ldap_search without ldap connection
# - string $scope
# - array|string $base,
# - array|string $filter,
# - array $attributes = [],
# - int $attributes_only = 0,
# - int $sizelimit = -1,
# - int $timelimit = -1,
# - int $deref = LDAP_DEREF_NEVER,
# - ?array $controls = null
function search_with_scope(string $scope = "sub", ...$searchargs): LDAP\Result|array|false
{
switch($scope)
{
case "sub":
return \Ltb\PhpLDAP::ldap_search($this->ldap, ...$searchargs);
case "one":
return \Ltb\PhpLDAP::ldap_list($this->ldap, ...$searchargs);
case "base":
return \Ltb\PhpLDAP::ldap_read($this->ldap, ...$searchargs);
default:
error_log("search_with_scope: invalid scope $scope");
return false;
}
}

function search($ldap_filter,$attributes, $attributes_map, $search_result_title, $search_result_sortby, $search_result_items)
{

Expand Down
5 changes: 5 additions & 0 deletions src/Ltb/PhpLDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public static function ldap_read(...$args)
return ldap_read(...$args);
}

public static function ldap_list(...$args)
{
return ldap_list(...$args);
}

public static function ldap_count_entries($ldap, $result)
{
return ldap_count_entries($ldap, $result);
Expand Down
32 changes: 32 additions & 0 deletions tests/Ltb/LdapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,38 @@ public function test_get_list(): void

}

public function test_search_with_scope(): void
{

$phpLDAPMock = Mockery::mock('overload:Ltb\PhpLDAP');

$phpLDAPMock->shouldreceive('ldap_search')
->with("ldap_connection", "ou=people,dc=my-domain,dc=com", "(uid=test)", array("cn", "sn"))
->andReturn(array("ldap_search_result"));

$phpLDAPMock->shouldreceive('ldap_list')
->with("ldap_connection", "ou=people,dc=my-domain,dc=com", "(uid=test)", array("cn", "sn"))
->andReturn(array("ldap_list_result"));

$phpLDAPMock->shouldreceive('ldap_read')
->with("ldap_connection", "ou=people,dc=my-domain,dc=com", "(uid=test)", array("cn", "sn"))
->andReturn(array("ldap_read_result"));

$ldapInstance = new \Ltb\Ldap( null, null, null, null, null, null, null, null );
$ldapInstance->ldap = "ldap_connection";

$result_search = $ldapInstance->search_with_scope("sub", "ou=people,dc=my-domain,dc=com", "(uid=test)", array("cn","sn"));
$result_list = $ldapInstance->search_with_scope("one", "ou=people,dc=my-domain,dc=com", "(uid=test)", array("cn","sn"));
$result_read = $ldapInstance->search_with_scope("base", "ou=people,dc=my-domain,dc=com", "(uid=test)", array("cn","sn"));
$result_unknown = $ldapInstance->search_with_scope("unknown", "ou=people,dc=my-domain,dc=com", "(uid=test)", array("cn","sn"));

$this->assertEquals(array('ldap_search_result'), $result_search, "function ldap_search not correctly called");
$this->assertEquals(array('ldap_list_result'), $result_list, "function ldap_list not correctly called");
$this->assertFalse($result_unknown, "weird return code in function ldap_read for scope=unknown");

}


public function test_ldapSort(): void
{

Expand Down
Loading