Search code examples
symfonyactive-directorypasswords

Symfony dont respect Active Directory constraint


I have this method to change AD user password :

  public function changePassword($ldapUserIdentifier, $oldPassword, $newPassword): string
    {


        try {

            $this->ldap->bind("DOMAIN\\".$ldapUserIdentifier, $oldPassword);
            $query = $this->ldap->query('dc=domain,dc=com', '(&(sAMAccountName=' . $ldapUserIdentifier . '))');
            $result = $query->execute();
            $entry = $result[0];
            $newPassword = mb_convert_encoding('"' . $newPassword . '"', 'utf-16le');
            $entryManager = $this->ldap->getEntryManager();
            $entryManager->applyOperations($entry->getDn(), [
                new UpdateOperation(LDAP_MODIFY_BATCH_REPLACE, 'unicodePwd', [$newPassword])
            ]);
            dd("wait");
        } catch (\Throwable $th)
{dd($th);
            return $th->getMessage();
        }

Works great but the script doesnt care about AD constraint. My Active Directory constraint is :

  1. Cant use 6 last passwords

But my code dont respect this constraint.

I want respect AD constraint


Solution

  • After many days, i solved the problem.

    In this case :

                $entryManager->applyOperations($entry->getDn(), [
                new UpdateOperation(LDAP_MODIFY_BATCH_REPLACE, 'unicodePwd', [$newPassword])
            ]);
    

    Active Directory thinks he needs to reset the password and need an admin account.

    In this case :

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

    Active Directory thinks he needs to CHANGE the password and dont need an admin account.

    See here https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-adts/6e803168-f140-4d23-b2d3-c3a8ab5917d2

    or here :

    PHP-LDAP change password