Skip to content

Commit

Permalink
add a get_first_value method in LDAP (#23)
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 46f27f9 commit f214c78
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/Ltb/Ldap.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(
$this->ldap_user_base = $ldap_user_base;
$this->ldap_size_limit = $ldap_size_limit;
$this->ldap_krb5ccname = $ldap_krb5ccname;

}

function connect() {
Expand Down Expand Up @@ -393,5 +393,43 @@ function modify_attributes($dn, $userdata): array {
8 => "inhistory"
];

/**
* get the first value of the first attribute in the first entry found
* @param string $ldap_base: the base search
* @param string $ldap_scope: the scope for the search
* @param string $ldap_filter: the filter for searching the entry
* @param string $attribute: a list of attributes, separated by ","
* @return string: the first value of the first attribute found in the first entry
*/
function get_first_value($ldap_base, $ldap_scope, $ldap_filter, $attribute): string {

$value = "";

if ($this->ldap) {

# Search entry
$search = $this->search_with_scope($ldap_scope, $ldap_base, $ldap_filter, explode(",", $attribute));

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

if ( $errno ) {
error_log("LDAP - Search error $errno (".\Ltb\PhpLDAP::ldap_error($this->ldap).")");
} else {
$entry = \Ltb\PhpLDAP::ldap_get_entries($this->ldap, $search);

# Loop over attribute
foreach ( explode(",", $attribute) as $ldap_attribute ) {
if ( isset ($entry[0][$ldap_attribute]) ) {
$value = $entry[0][$ldap_attribute][0];
break;
}
}
}
}

return $value;

}

}
?>
96 changes: 96 additions & 0 deletions tests/Ltb/LdapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,102 @@ public function test_modify_attributes(): void

}

public function test_get_first_value(): void
{

$ldap_connection = "ldap_connection";
$ldap_base = "uid=test,ou=people,dc=my-domain,dc=com";
$ldap_scope = "base";
$ldap_filter = '(objectClass=inetOrgPerson)';
$attribute = "mail,cn";
$search_result = "search_result";
$entries = [
'count' => 2,
0 => [
'count' => 2,
0 => 'cn',
1 => 'sn',
'cn' => [
'count' => 1,
0 => 'testcn1'
],
'sn' => [
'count' => 1,
0 => 'zzzzzz'
]
],
1 => [
'count' => 2,
0 => 'cn',
1 => 'sn',
'cn' => [
'count' => 1,
0 => 'testcn2'
],
'sn' => [
'count' => 1,
0 => 'aaaaaa'
]
]
];


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

$phpLDAPMock->shouldreceive('ldap_read')
->with($ldap_connection, $ldap_base, $ldap_filter, explode(",", $attribute))
->andReturn($search_result);

$phpLDAPMock->shouldreceive('ldap_errno')
->with($ldap_connection)
->andReturn(0);

$phpLDAPMock->shouldreceive('ldap_get_entries')
->with($ldap_connection, $search_result)
->andReturn($entries);

$ldapInstance = new \Ltb\Ldap( null, null, null, null, null, null, null, null );
$ldapInstance->ldap = $ldap_connection;
$value = $ldapInstance->get_first_value($ldap_base, $ldap_scope, $ldap_filter, $attribute);

$this->assertEquals("testcn1", $value, 'Weird value returned by get_first_value method');

}

public function test_get_first_value_error(): void
{

$ldap_connection = "ldap_connection";
$ldap_base = "DUMMY";
$ldap_scope = "base";
$ldap_filter = '(objectClass=inetOrgPerson)';
$attribute = "mail,cn";
$search_result = "search_result";
$errno = 34;
$error_msg = "invalidDNSyntax";

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

$phpLDAPMock->shouldreceive('ldap_read')
->with($ldap_connection, $ldap_base, $ldap_filter, explode(",", $attribute))
->andReturn($search_result);

$phpLDAPMock->shouldreceive('ldap_errno')
->with($ldap_connection)
->andReturn($errno);

$phpLDAPMock->shouldreceive('ldap_error')
->with($ldap_connection)
->andReturn($error_msg);

$ldapInstance = new \Ltb\Ldap( null, null, null, null, null, null, null, null );
$ldapInstance->ldap = $ldap_connection;
$value = $ldapInstance->get_first_value($ldap_base, $ldap_scope, $ldap_filter, $attribute);

$this->assertEquals("", $value, 'Weird value returned by get_first_value method called with an invalid DN');

}

public function setUp(): void
{
// Turn on error reporting
Expand Down

0 comments on commit f214c78

Please sign in to comment.