PHP-LDAP修改密码

PHP-LDAP change password

我们使用 LDAP (Samba) 进行身份验证,现在我想让用户能够更改他们的密码。我正在使用这个脚本:

        $username = $request->request->get('username');
        $oldpasswd = $request->request->get('oldpasswd');
        $newpasswd = $request->request->get('newpasswd');
        $userPassword = mb_convert_encoding('"'.$newpasswd.'"', 'utf-16le');
        $ldapPasswd = $this->getParameter('LDAP_PASSWD');

        $ldap->bind("CN=LDAPADMIN,CN=Users,DC=DOMAIN,DC=net", $ldapPasswd);
        $query = $ldap->query('CN=users,DC=DOMAIN,DC=net', "(&(objectclass=person)(sAMAccountName=$username))");
        $result = $query->execute()->toArray();
        $entry = $result[0];

        $newEntry = new Entry($entry->getDn(), [
            'unicodePwd' => [$userPassword],
        ]);
        $ldap->getEntryManager()->update($newEntry);

它工作正常,但是所有安全设置都无效:不要重复密码、密码长度 > 6 个字符等...并且,脚本使用管理员帐户更改 password.Is 还有其他方法可以使用用户帐户更新用户密码并验证密码安全配置吗?

根据您的代码,您似乎正在使用 Symfony 并且您有一个 Active Directory 服务器。

您的代码正在执行“密码重置”,这是管理员在用户忘记密码时会执行的操作。它需要管理权限并且不需要知道以前的密码,因此它不会强制执行以前的历史记录。

您想做的是“密码更改”。 documentation for unicodePwd 解释了如何做到这一点:

If the Modify request contains a delete operation containing a value Vdel for unicodePwd followed by an add operation containing a value Vadd for unicodePwd, the server considers the request to be a request to change the password. The server decodes Vadd and Vdel using the password decoding procedure documented later in this section. Vdel is the old password, while Vadd is the new password.

在 LDAP 中,这称为“批处理请求”,其中包含删除指令和添加指令,所有这些都在对服务器的同一个请求中。 Symfony 文档包括一个关于如何做的部分 Batch Updating。在您的情况下,它应该看起来像这样:

$username = $request->request->get('username');
$oldpasswd = $request->request->get('oldpasswd');
$newpasswd = $request->request->get('newpasswd');
$oldUserPassword = mb_convert_encoding('"'.$oldpasswd.'"', 'utf-16le');
$userPassword = mb_convert_encoding('"'.$newpasswd.'"', 'utf-16le');
$ldapPasswd = $this->getParameter('LDAP_PASSWD');

$ldap->bind("CN=LDAPADMIN,CN=Users,DC=DOMAIN,DC=net", $ldapPasswd);
$query = $ldap->query('CN=users,DC=DOMAIN,DC=net', "(&(objectclass=person)(sAMAccountName=$username))");
$result = $query->execute()->toArray();
$entry = $result[0];

$entryManager->applyOperations($entry->getDn(), [
    new UpdateOperation(LDAP_MODIFY_BATCH_REMOVE, 'unicodePwd', $oldUserPassword),
    new UpdateOperation(LDAP_MODIFY_BATCH_ADD, 'unicodePwd', $userPassword),
]);

请注意,您必须按照对新密码进行编码的方式对旧密码进行编码。