Skip to content

Commit

Permalink
Create abstract method get_attribute_value alongside get_password_value.
Browse files Browse the repository at this point in the history
  • Loading branch information
abpai94 committed Sep 18, 2024
1 parent 46f27f9 commit 6a036cd
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/Ltb/Ldap.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,23 +259,33 @@ function sorted_search($ldap_base, $ldap_filter, $attributes, $sortby, $ldap_siz
}

/**
* Gets the value of the password attribute
* Gets the value of the LDAP attribute
* @param string $dn the dn of the user
* @param string $pwdattribute the Attribute that contains the password
* @return string|false the first value of the password taken from ldap_get_values
* @param string $attribute the LDAP attribute
* @return array|false the array containing attribute values
*/
function get_password_value($dn, $pwdattribute) {
$search_userpassword = \Ltb\PhpLDAP::ldap_read($this->ldap, $dn, "(objectClass=*)", array($pwdattribute));
if ($search_userpassword) {
$password_values = \Ltb\PhpLDAP::ldap_get_values($this->ldap, \Ltb\PhpLDAP::ldap_first_entry($this->ldap, $search_userpassword), $pwdattribute);
if(isset($password_values[0]))
{
return $password_values[0];
}
function get_attribute_values($dn, $attribute) {
$search = \Ltb\PhpLDAP::ldap_read($this->ldap, $dn, "(objectClass=*)", array($attribute));
if ($search) {
return \Ltb\PhpLDAP::ldap_get_values($this->ldap, \Ltb\PhpLDAP::ldap_first_entry($this->ldap, $search), $attribute);
}
return false;
}

/*
* Gets the value of the password attribute
* @param string $dn the dn of the user
* @param string $pwdattribute the Attribute that contains the password
* @return array|false the array containing attribute values
*/
function get_password_value($dn, $pwdattribute) {
$password_values = $this->get_attribute_values($dn, $pwdattribute);
if(isset($password_values[0])) {
return $password_values[0];
}
return false;
}

/**
* Changes the password of a user while binded as the user in an Active Directory
* @param string $dn the dn of the user
Expand Down
36 changes: 36 additions & 0 deletions tests/Ltb/LdapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,42 @@ public function test_sorted_search_without_sort_control(): void

}

public function test_get_attribute_values(): void
{

$ldap_connection = "ldap_connection";
$dn = "uid=test,ou=people,dc=my-domain,dc=com";
$pwdhistory = "pwdHistory";
$expectedValues = [
"count" => 2,
0 => 'secret',
1 => 'testpassword'
];

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

$phpLDAPMock->shouldreceive('ldap_read')
->with($ldap_connection, $dn, '(objectClass=*)', [ $pwdhistory ])
->andReturn("ldap_result");

$phpLDAPMock->shouldreceive('ldap_first_entry')
->with($ldap_connection, "ldap_result")
->andReturn("result_entry");

$phpLDAPMock->shouldreceive('ldap_get_values')
->with($ldap_connection, "result_entry", $pwdhistory)
->andReturn($expectedValues);

$ldapInstance = new \Ltb\Ldap( null, null, null, null, null, null, null, null );
$ldapInstance->ldap = $ldap_connection;
$value = $ldapInstance->get_attribute_values(
$dn,
$pwdhistory
);

$this->assertEquals(["count" => 2, 'secret', 'testpassword'] , $value, "incorrect array of attribute values returned by get_attribute_values");
}

public function test_get_password_value(): void
{

Expand Down

0 comments on commit 6a036cd

Please sign in to comment.