Skip to content

Core\Ldap\Ad\AccountFlags::isHomeDirectoryRequired

$accountFlags->isHomeDirectoryRequired(): bool

Checks if an account needs a home directory. The result is true or false.

A home directory is a folder on a server where a user keeps their own files. When this flag is set, the account must have a home directory. Most accounts do not have this flag.

isHomeDirectoryRequired reads the bit 0x8 of userAccountControl. A normal account with this flag is 520.

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

An account without the flag

<?nvs
// Shows that `isHomeDirectoryRequired` returns `false` for an account that does not need a home 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) {
// This prints `home directory required: false`.
echo 'home directory required: ', $flags->isHomeDirectoryRequired() ? 'true' : 'false', "\n";
}
Output
home directory required: false

Count accounts that need one

<?nvs
// Shows how to count the accounts that need a home directory.
// 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'] },
);
uint $count = 0;
uint $all = 0;
foreach ($found as Core\Ldap\Entry $account) {
var $flags = $account->accountFlags();
if ($flags != null) {
$all = $all + 1;
if ($flags->isHomeDirectoryRequired()) {
$count = $count + 1;
}
}
}
// No account in this directory needs a home directory. This prints `0 of 5`.
echo $count, ' of ', $all, "\n";
Output
0 of 5

Require one on a copy

<?nvs
// Shows how `isHomeDirectoryRequired` and the number change on a copy made with `with`.
// 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) {
var $changed = $flags->with({ homeDirectoryRequired: true });
// This prints `512 false` and then `520 true`.
echo $flags->bits(), $flags->isHomeDirectoryRequired() ? ' true' : ' false', "\n";
echo $changed->bits(), $changed->isHomeDirectoryRequired() ? ' true' : ' false', "\n";
}
Output
512 false
520 true