Skip to content

Commit

Permalink
fix new Ldap.php functions + add corresponding tests (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Coutadeur committed Mar 29, 2024
1 parent 7bdb9a5 commit 891d20d
Show file tree
Hide file tree
Showing 3 changed files with 348 additions and 16 deletions.
29 changes: 15 additions & 14 deletions src/Ltb/Ldap.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,20 +139,21 @@ static function sorted_search($ldap, $ldap_base, $ldap_filter, $attributes, $sor

/**
* Gets the value of the password attribute
* @param \LDAP\Connection|array $ldap An LDAP\Connection instance, returned by ldap_connect()
* @param \LDAP\Connection $ldap An LDAP\Connection instance, returned by ldap_connect()
* @param string $dn the dn of the user
* @param type $pwdattribute the Attribute that contains the password
* @return string the value of $pwdattribute
* @param string $pwdattribute the Attribute that contains the password
* @return array|false the values of the password, as returned by ldap_get_values
*/
static function get_password_value($ldap, $dn, $pwdattribute): string {
static function get_password_values($ldap, $dn, $pwdattribute): array|false {
$search_userpassword = \Ltb\PhpLDAP::ldap_read($ldap, $dn, "(objectClass=*)", array($pwdattribute));
if ($search_userpassword) {
return \Ltb\PhpLDAP::ldap_get_values($ldap, ldap_first_entry($ldap, $search_userpassword), $pwdattribute);
return \Ltb\PhpLDAP::ldap_get_values($ldap, \Ltb\PhpLDAP::ldap_first_entry($ldap, $search_userpassword), $pwdattribute);
}
return false;
}

/**
* Changes the password of an user while binded as the user in an Active Directory
* Changes the password of a user while binded as the user in an Active Directory
* @param \LDAP\Connection|array $ldap An LDAP\Connection instance, returned by ldap_connect()
* @param string $dn the dn of the user
* @param string $oldpassword the old password
Expand All @@ -163,7 +164,7 @@ static function change_ad_password_as_user($ldap, $dn, $oldpassword, $password):
# The AD password change procedure is modifying the attribute unicodePwd by
# first deleting unicodePwd with the old password and them adding it with the
# the new password
$oldpassword_hashed = make_ad_password($oldpassword);
$oldpassword_hashed = \Ltb\Password::make_ad_password($oldpassword);

$modifications = array(
array(
Expand All @@ -175,12 +176,12 @@ static function change_ad_password_as_user($ldap, $dn, $oldpassword, $password):
"attrib" => "unicodePwd",
"modtype" => LDAP_MODIFY_BATCH_ADD,
"values" => array($password),
),
)
);

\Ltb\PhpLDAP::ldap_modify_batch($ldap, $dn, $modifications);
$error_code = ldap_errno($ldap);
$error_msg = ldap_error($ldap);
$error_code = \Ltb\PhpLDAP::ldap_errno($ldap);
$error_msg = \Ltb\PhpLDAP::ldap_error($ldap);
return array($error_code, $error_msg);
}

Expand Down Expand Up @@ -226,10 +227,10 @@ static function change_password_with_exop($ldap, $dn, $oldpassword, $password, $
}

/**
* Changes attributes (and password) using Password Policy Control
* Changes attributes (and possibly password) using Password Policy Control
* @param \LDAP\Connection|array $ldap An LDAP\Connection instance, returned by ldap_connect()
* @param string $dn the dn of the user
* @param array $userdata the array, containing the new (hashed) password
* @param array $userdata the array, containing the modifications
* @return array 0: error_code, 1: error_msg, 2: ppolicy_error_code
*/
static function modify_attributes_using_ppolicy($ldap, $dn, $userdata): array {
Expand All @@ -253,8 +254,8 @@ static function modify_attributes_using_ppolicy($ldap, $dn, $userdata): array {
*/
static function modify_attributes($ldap, $dn, $userdata): array {
\Ltb\PhpLDAP::ldap_mod_replace($ldap, $dn, $userdata);
$error_code = ldap_errno($ldap);
$error_msg = ldap_error($ldap);
$error_code = \Ltb\PhpLDAP::ldap_errno($ldap);
$error_msg = \Ltb\PhpLDAP::ldap_error($ldap);
return array($error_code, $error_msg);
}

Expand Down
Loading

0 comments on commit 891d20d

Please sign in to comment.