Skip to content

Core\Ldap\Ad\AccountFlags::bits

$accountFlags->bits(): int

Returns the whole number of userAccountControl, exactly as Active Directory stores it. Each flag of the account is one bit of this number. A normal account that can log in is 512, and the same account when it is disabled is 514.

bits returns every stored bit, also the bits no function names. It does not include isLockedOut and isPasswordExpired. Active Directory computes these two flags and does not store them.

Use bits when you need the number itself, for example to write changed flags back to the directory. To check one flag, call the function for that flag, such as isDisabled().

Return value

Returns int. The number, with every bit. It does not include the bits Active Directory computes.

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

The stored number

<?nvs
// Shows the number Active Directory stores in `userAccountControl` for three people.
// 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=Dora Lind,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;
}
// This prints `Anna Berg: 512`, `Ben Kohl: 66048` and `Dora Lind: 514`.
echo $person->dn()->rdnValue(), ': ', $flags->bits(), "\n";
}
Output
Anna Berg: 512
Ben Kohl: 66048
Dora Lind: 514

The same number as int

<?nvs
// Shows that `bits` returns the same number that `int` reads from `userAccountControl`.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $person = $corp->read('CN=Anna Berg,OU=Company,DC=example,DC=test', { select: ['userAccountControl'] });
var $flags = $person?->accountFlags();
if ($person != null && $flags != null) {
// This prints `512` twice.
echo $flags->bits(), "\n";
echo $person->int('userAccountControl') ?? 0, "\n";
}
Output
512
512

The number to write back

<?nvs
// Shows how to get the number for changed flags, for example to write it back to the directory.
// 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) {
// `with` disables the account. `bits` returns the new number.
// This prints `512` and then `514`.
echo $flags->bits(), "\n";
echo $flags->with({ disabled: true })->bits(), "\n";
}
Output
512
514