Skip to content

Core\Ldap\Entry::accountFlags

$entry->accountFlags(): ?Core\Ldap\Ad\AccountFlags

Returns the flags of an Active Directory account as a Core\Ldap\Ad\AccountFlags. Each flag is a function that returns a bool, such as isDisabled() or hasNonExpiringPassword().

Active Directory stores these flags as one number in userAccountControl. Select that attribute in the search. The search then also reads two more values: msDS-User-Account-Control-Computed, which says if the account is locked out or its password has expired, and pwdLastSet, which says if the user must change the password.

If the entry has no userAccountControl, the result is null. A group and an organizational unit have none. If the value is not a number, accountFlags throws a LogicError that names the attribute.

Good to know: bits() returns the whole number, and with returns a copy with some flags changed.

Return value

Returns ?Core\Ldap\Ad\AccountFlags. The flags, or null when the entry has no userAccountControl, such as a group.

Errors

  • LogicErrorThe attribute has more than one value, or its value is not in the form this function reads. The message names the attribute.

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 who can log in

<?nvs
// Shows how to check which accounts are disabled.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
array<string> $dns = [
'CN=Anna Berg,OU=Company,DC=example,DC=test',
'CN=Dora Lind,OU=Company,DC=example,DC=test',
];
foreach ($dns as string $dn) {
var $person = $corp->read($dn, { select: ['userAccountControl'] });
if ($person == null) {
continue;
}
// `accountFlags` returns one `bool` for each flag of `userAccountControl`.
// This prints `Anna Berg: can log in` and `Dora Lind: disabled`.
var $flags = $person->accountFlags();
if ($flags != null && $flags->isDisabled()) {
echo $person->dn()->rdnValue(), ": disabled\n";
} else {
echo $person->dn()->rdnValue(), ": can log in\n";
}
}
Output
Anna Berg: can log in
Dora Lind: disabled

Password rules of each person

<?nvs
// Shows how to read the password state of a few people from their account flags.
// 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) {
// When you select `userAccountControl`, the search also reads `pwdLastSet`
// and the flags Active Directory computes, such as the one `isPasswordExpired` reads.
var $person = $corp->read($dn, { select: ['userAccountControl'] });
var $flags = $person?->accountFlags();
if ($person == null || $flags == null) {
continue;
}
// This prints `Anna Berg: ok`, `Ben Kohl: never expires`
// and `Carla Diaz: must change it`.
string $state = 'ok';
if ($flags->hasNonExpiringPassword()) {
$state = 'never expires';
}
if ($flags->mustChangePassword()) {
$state = 'must change it';
}
echo $person->dn()->rdnValue(), ': ', $state, "\n";
}
Output
Anna Berg: ok
Ben Kohl: never expires
Carla Diaz: must change it

A group has no account flags

<?nvs
// Shows that `accountFlags` returns `null` for an entry that is not an account, such as a group.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $group = $corp->read('CN=Employees,OU=Company,DC=example,DC=test', { select: ['userAccountControl'] });
if ($group != null) {
// A group has no `userAccountControl`. This prints `no account flags`.
var $flags = $group->accountFlags();
echo $flags == null ? 'no account flags' : 'an account', "\n";
}
Output
no account flags