Core\Ldap\Connection::setPassword
Sets a new password for a user account. It returns nothing.
This is what an administrator or a help desk does. The old password is not needed, and the account can log in with the new password at once. A user who changes their own password uses changePassword instead, which checks the old password.
The connection must log in as an account that may reset passwords, such as an administrator. The connection must also use TLS, because the server only accepts a password over an encrypted connection. On a connection without TLS, setPassword throws an error and sends nothing.
The domain checks the new password against its rules, such as the minimum length. A password that breaks a rule throws Core\Ldap\LdapError with $kind set to PasswordPolicy.
Good to know: the password is a secret string. A program cannot print it or write it to a log. After a reset, you can set pwdLastSet to 0. The user must then choose a new password at the next login.
Parameters
$dnCore\Ldap\Dn|stringThe user account's DN, as aDnor as text. Text cannot betainted.$passwordsecret tainted stringThe new password.
Return value
Returns void. Nothing.
Errors
LogicErrordnis text that is not a DN, or the connection is closed.Core\Ldap\LdapError$kindisEncryptionRequiredwhen the connection does not use TLS. Nothing is sent then. It isPasswordPolicywhen the domain does not accept the new password, and another kind for any other error the server returns.
Examples
The examples below run with this configuration file.
# These examples use the LDAP directory server on this computer at port 16636.# They log in as the administrator, because they change accounts in the directory.# `tls_ca_file` is the certificate that the server's certificate is checked against.[ldap.corp-accounts]url = "ldaps://127.0.0.1:16636"base = "DC=example,DC=test"user = "CN=Administrator,CN=Users,DC=example,DC=test"password = "Novis-test-1"tls_ca_file = "../../../../../tests/db/ca.crt"timeout = "5s"
# The program may open the `corp-accounts` directory.[capabilities.ldap]connect = ["corp-accounts"]Give a new account its first password
<?nvs// Shows how to give a new user account its first password, so the person can log in.// requires: samba-ad
var $corp = Core\Ldap::connect('corp-accounts');string $unit = 'OU=New Staff 1,DC=example,DC=test';string $user = 'CN=Eva Roth,' . $unit;
// A run that stopped early can leave the account behind, so the program deletes it first.array<string> $leftovers = [$user, $unit];foreach ($leftovers as string $left) { try { $corp->delete($left); } catch (Core\Ldap\LdapError $missing) { }}$corp->add($unit, ['objectClass' => 'organizationalUnit']);$corp->add($user, ['objectClass' => 'user', 'sAMAccountName' => 'eva.roth']);
// The account gets its first password. The value 512 then turns the account on.$corp->setPassword($user, 'Welcome-to-work-4');$corp->modify($user, [Core\Ldap\Change::replace('userAccountControl', 512)]);
// `authenticate` throws an error when the password is wrong. This prints `login works`.$corp->authenticate($user, 'Welcome-to-work-4');echo 'login works', "\n";
$corp->delete($user);$corp->delete($unit);login worksA password that is too simple
<?nvs// Shows what happens when the domain does not accept a new password.// requires: samba-ad
var $corp = Core\Ldap::connect('corp-accounts');string $unit = 'OU=New Staff 2,DC=example,DC=test';string $user = 'CN=Finn Wolf,' . $unit;
// A run that stopped early can leave the account behind, so the program deletes it first.array<string> $leftovers = [$user, $unit];foreach ($leftovers as string $left) { try { $corp->delete($left); } catch (Core\Ldap\LdapError $missing) { }}$corp->add($unit, ['objectClass' => 'organizationalUnit']);$corp->add($user, ['objectClass' => 'user', 'sAMAccountName' => 'finn.wolf']);
// The domain needs longer passwords with several kinds of characters.// So `setPassword` throws an error, and `$kind` is `PasswordPolicy`.try { $corp->setPassword($user, 'abc'); echo 'the password was set', "\n";} catch (Core\Ldap\LdapError $error) { echo $error->kind == Core\Ldap\LdapError::Kind::PasswordPolicy ? 'PasswordPolicy' : 'another reason', "\n";}
// A stronger password works. This prints `the password was set`.$corp->setPassword($user, 'Strong-enough-Pass-9');echo 'the password was set', "\n";
$corp->delete($user);$corp->delete($unit);PasswordPolicythe password was setReset a forgotten password
<?nvs// Shows how a help desk resets a forgotten password, so the user must choose a new one.// requires: samba-ad
var $corp = Core\Ldap::connect('corp-accounts');string $unit = 'OU=New Staff 3,DC=example,DC=test';string $user = 'CN=Gina Hart,' . $unit;
// A run that stopped early can leave the account behind, so the program deletes it first.array<string> $leftovers = [$user, $unit];foreach ($leftovers as string $left) { try { $corp->delete($left); } catch (Core\Ldap\LdapError $missing) { }}$corp->add($unit, ['objectClass' => 'organizationalUnit']);$corp->add($user, ['objectClass' => 'user', 'sAMAccountName' => 'gina.hart']);$corp->setPassword($user, 'Forgotten-Pass-5');$corp->modify($user, [Core\Ldap\Change::replace('userAccountControl', 512)]);
// The help desk sets a temporary password. `pwdLastSet` 0 means// the user must change it at the next login.$corp->setPassword($user, 'Temporary-Pass-6');$corp->modify($user, [Core\Ldap\Change::replace('pwdLastSet', 0)]);
// The old password no longer works. This prints `InvalidCredentials`.try { $corp->authenticate($user, 'Forgotten-Pass-5');} catch (Core\Ldap\LdapError $error) { echo $error->kind == Core\Ldap\LdapError::Kind::InvalidCredentials ? 'InvalidCredentials' : 'another reason', "\n";}
// The temporary password is correct, but the user must change it first.// This prints `MustChangePassword`.try { $corp->authenticate($user, 'Temporary-Pass-6');} catch (Core\Ldap\LdapError $error) { echo $error->kind == Core\Ldap\LdapError::Kind::MustChangePassword ? 'MustChangePassword' : 'another reason', "\n";}
$corp->delete($user);$corp->delete($unit);InvalidCredentialsMustChangePassword