diff --git a/src/Ltb/Ldap.php b/src/Ltb/Ldap.php index ca019cd..28f62c4 100644 --- a/src/Ltb/Ldap.php +++ b/src/Ltb/Ldap.php @@ -257,23 +257,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 diff --git a/tests/Ltb/LdapTest.php b/tests/Ltb/LdapTest.php index 782f5cf..35ffd2f 100644 --- a/tests/Ltb/LdapTest.php +++ b/tests/Ltb/LdapTest.php @@ -542,6 +542,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 {