Skip to content

Core\Ldap\Ad\AccountFlags::isDisabled

$accountFlags->isDisabled(): bool

Checks if an account is disabled. The result is true or false.

Nobody can log in with a disabled account, even with the right password. An administrator disables an account, for example when a person leaves the company. The account and its groups stay in the directory, so it can be enabled again later.

isDisabled reads the bit 0x2 of userAccountControl. A normal account is 512, and the same account when it is disabled is 514.

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"]

A disabled account

<?nvs
// Shows that `isDisabled` returns `true` for an account that nobody can log in with.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $flags = $corp->read('CN=Dora Lind,OU=Company,DC=example,DC=test', { select: ['userAccountControl'] })?->accountFlags();
if ($flags != null) {
// This prints `disabled: true`.
echo 'disabled: ', $flags->isDisabled() ? 'true' : 'false', "\n";
}
Output
disabled: true

List the active people

<?nvs
// Shows how to keep only the people who can log in.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $found = $corp->search(
Core\Ldap\Filter::equals('objectClass', 'user'),
{ base: 'OU=Company,DC=example,DC=test', select: ['userAccountControl'] },
);
array<tainted string> $names = [];
foreach ($found as Core\Ldap\Entry $person) {
var $flags = $person->accountFlags();
if ($flags != null && !$flags->isDisabled()) {
$names[] = $person->dn()->rdnValue();
}
}
// The server sends entries in no fixed order, so the names are sorted.
// Dora Lind is disabled. This prints `Anna Berg`, `Ben Kohl`, `Carla Diaz` and `Reader`.
foreach (Core\Arr::sort($names) as tainted string $name) {
echo $name, "\n";
}
Output
Anna Berg
Ben Kohl
Carla Diaz
Reader

Enable a copy

<?nvs
// Shows how `isDisabled` changes on a copy made with `with`.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $flags = $corp->read('CN=Dora Lind,OU=Company,DC=example,DC=test', { select: ['userAccountControl'] })?->accountFlags();
if ($flags != null) {
var $enabled = $flags->with({ disabled: false });
// This prints `before: true, after: false`.
echo 'before: ', $flags->isDisabled() ? 'true' : 'false';
echo ', after: ', $enabled->isDisabled() ? 'true' : 'false', "\n";
}
Output
before: true, after: false