PHP LDAP - 允许用户更改自己的密码

时间:2018-02-07 10:41:12

标签: php ldap

我一直在创建一个门户网站,允许我们的员工更改他们的AD密码。虽然当管理员凭据用于ldap_bind时,我可以使用ldap_mod_replace更改用户密码,但是当我使用用户凭据绑定时,它将不起作用。为了让用户更改自己的密码,我还需要做些什么吗?

这是我的代码片段,用于更改密码。

$ldap_conn = ldap_connect($ldap_host);

ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);

$ldap_username  = $_SESSION['User'];

$ldap_oldpassword = $_POST['oldpswd'];
$ldap_newpassword = $_POST['newpswd'];
$ldap_confirmnewpassword = $_POST['newpswdconfirm'];

if (ldap_bind($ldap_conn, $ldap_username, $ldap_oldpassword)) {
//encode password
    if ($ldap_newpassword == $ldap_confirmnewpassword){
        $pwdtxt = $ldap_newpassword;
        $newPassword = '"' . $pwdtxt . '"';
        $newPass = iconv( 'UTF-8', 'UTF-16LE', $newPassword );
        $userdata["unicodepwd"] = $newPass; 
        // change password
        $userDn = $_SESSION['DN'];
        $result = ldap_mod_replace($ldap_conn, $userDn, $userdata); 
        if ($result) {
            echo "User modified!";
        } 
    }
    else{
        $response = "failed";
        echo $response;
    }
}
else {
        ldap_get_option($ldap_conn, LDAP_OPT_DIAGNOSTIC_MESSAGE, $extended_error);

            $response = $extended_error;
            echo json_encode(array($response));
}

由于

0 个答案:

没有答案