Skip to content

Core\Ldap\Ad\AccountFlags::isPasswordExpired

$accountFlags->isPasswordExpired(): bool

Checks if the password of an account has expired. The result is true or false.

The domain sets how long a password is valid, for example six weeks. After that time, the user must choose a new password before they can log in again. When hasNonExpiringPassword returns true, the password does not expire this way.

Active Directory computes this flag. It does not store it in userAccountControl. A search that selects userAccountControl also reads msDS-User-Account-Control-Computed, and isPasswordExpired reads the bit 0x800000 from there. So bits never includes it, and with cannot set or clear it.

Return value

Returns bool. true when the bit is set, and false when it is not.

Changelog

VersionChange
0.0.6Every Core\Ldap\Ad\AccountFlags method that checks a flag now starts with is, has, allows or uses, for example isDisabled() and hasNonExpiringPassword(). The field trustedToAuthForDelegation of with() is now trustedToAuthenticateForDelegation.

Examples

The examples below run with this configuration file.

nvs.toml
# These examples use the LDAP directory server on this computer at port 16636.
# `tls_ca_file` is the certificate that the server's certificate is checked against.
[ldap.corp]
url = "ldaps://127.0.0.1:16636"
base = "DC=example,DC=test"
user = "CN=Reader,OU=Company,DC=example,DC=test"
password = "Novis-reader-1"
tls_ca_file = "../../../../../tests/db/ca.crt"
timeout = "5s"
# The program may open the `corp` directory.
[capabilities.ldap]
connect = ["corp"]

Check each person

<?nvs
// Shows how to check if the password of each person has expired.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
array<string> $dns = [
'CN=Anna Berg,OU=Company,DC=example,DC=test',
'CN=Ben Kohl,OU=Company,DC=example,DC=test',
'CN=Carla Diaz,OU=Company,DC=example,DC=test',
];
foreach ($dns as string $dn) {
var $person = $corp->read($dn, { select: ['userAccountControl'] });
var $flags = $person?->accountFlags();
if ($person == null || $flags == null) {
continue;
}
// Carla Diaz must set a new password, so Active Directory reports it as expired.
// This prints `Anna Berg: valid`, `Ben Kohl: valid` and `Carla Diaz: expired`.
echo $person->dn()->rdnValue(), ': ', $flags->isPasswordExpired() ? 'expired' : 'valid', "\n";
}
Output
Anna Berg: valid
Ben Kohl: valid
Carla Diaz: expired

Not part of bits

<?nvs
// Shows that `isPasswordExpired` is not part of the number `bits` returns.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $flags = $corp->read('CN=Carla Diaz,OU=Company,DC=example,DC=test', { select: ['userAccountControl'] })?->accountFlags();
if ($flags != null) {
// `bits` returns only the stored number. This prints `bits: 512` and `expired: true`.
echo 'bits: ', $flags->bits(), "\n";
echo 'expired: ', $flags->isPasswordExpired() ? 'true' : 'false', "\n";
}
Output
bits: 512
expired: true

A copy keeps it

<?nvs
// Shows that a copy made with `with` keeps `isPasswordExpired`, because a program cannot change it.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $flags = $corp->read('CN=Anna Berg,OU=Company,DC=example,DC=test', { select: ['userAccountControl'] })?->accountFlags();
if ($flags != null) {
// Anna Berg's password is still valid.
// Setting the `passwordNeverExpires` option to `false` does not change that. This prints `false` twice.
echo $flags->isPasswordExpired() ? 'true' : 'false', "\n";
echo $flags->with({ passwordNeverExpires: false })->isPasswordExpired() ? 'true' : 'false', "\n";
}
Output
false
false