Skip to content

Core\Ldap\Ad\AccountFlags::with

$accountFlags->with({…}): Core\Ldap\Ad\AccountFlags$accountFlags->with(
    {…},
): Core\Ldap\Ad\AccountFlags

Returns a copy of the account flags with some flags set or cleared, such as $flags->with({ disabled: true }). Each option is one flag: true sets it, and false clears it. A flag you do not name stays as it is.

The object you call with on does not change. Call bits() on the copy to get the new number of userAccountControl, for example to write it back to the directory.

There is no option for isLockedOut or isPasswordExpired. Active Directory computes these two flags, so a program cannot set them. The copy keeps their values and the value of mustChangePassword.

Options

  • scriptbooloptionaltrue sets the flag and false clears it. Leave it out to keep the flag as it is.
  • disabledbooloptionaltrue sets the flag and false clears it. Leave it out to keep the flag as it is.
  • homeDirectoryRequiredbooloptionaltrue sets the flag and false clears it. Leave it out to keep the flag as it is.
  • passwordNotRequiredbooloptionaltrue sets the flag and false clears it. Leave it out to keep the flag as it is.
  • reversibleEncryptionbooloptionaltrue sets the flag and false clears it. Leave it out to keep the flag as it is.
  • temporaryDuplicateAccountbooloptionaltrue sets the flag and false clears it. Leave it out to keep the flag as it is.
  • normalAccountbooloptionaltrue sets the flag and false clears it. Leave it out to keep the flag as it is.
  • interdomainTrustAccountbooloptionaltrue sets the flag and false clears it. Leave it out to keep the flag as it is.
  • workstationTrustAccountbooloptionaltrue sets the flag and false clears it. Leave it out to keep the flag as it is.
  • serverTrustAccountbooloptionaltrue sets the flag and false clears it. Leave it out to keep the flag as it is.
  • passwordNeverExpiresbooloptionaltrue sets the flag and false clears it. Leave it out to keep the flag as it is.
  • mnsLogonAccountbooloptionaltrue sets the flag and false clears it. Leave it out to keep the flag as it is.
  • smartcardRequiredbooloptionaltrue sets the flag and false clears it. Leave it out to keep the flag as it is.
  • trustedForDelegationbooloptionaltrue sets the flag and false clears it. Leave it out to keep the flag as it is.
  • notDelegatedbooloptionaltrue sets the flag and false clears it. Leave it out to keep the flag as it is.
  • useDesKeyOnlybooloptionaltrue sets the flag and false clears it. Leave it out to keep the flag as it is.
  • noPreauthRequiredbooloptionaltrue sets the flag and false clears it. Leave it out to keep the flag as it is.
  • trustedToAuthenticateForDelegationbooloptionaltrue sets the flag and false clears it. Leave it out to keep the flag as it is.
  • partialSecretsAccountbooloptionaltrue sets the flag and false clears it. Leave it out to keep the flag as it is.

Return value

Returns Core\Ldap\Ad\AccountFlags. A new Core\Ldap\Ad\AccountFlags. The object you call it on does not change.

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

Disable an account

<?nvs
// Shows how to get the flags of an account after it is disabled.
// 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` sets the flags you name and keeps every other flag.
var $changed = $flags->with({ disabled: true });
// The account stays a normal account. This prints `disabled: true, normal: true`.
echo 'disabled: ', $changed->isDisabled() ? 'true' : 'false';
echo ', normal: ', $changed->isNormalAccount() ? 'true' : 'false', "\n";
}
Output
disabled: true, normal: true

The original does not change

<?nvs
// Shows that `with` returns a new object, and the object you call it on does not change.
// 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 `514` and then `512`.
echo $flags->bits(), "\n";
echo $enabled->bits(), "\n";
}
Output
514
512

Change two flags

<?nvs
// Shows how to set one flag and clear another in the same call.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $flags = $corp->read('CN=Ben Kohl,OU=Company,DC=example,DC=test', { select: ['userAccountControl'] })?->accountFlags();
if ($flags != null) {
// Ben Kohl's password never expires. This copy is disabled, and its password expires again.
var $changed = $flags->with({ disabled: true, passwordNeverExpires: false });
// This prints `66048` and then `514`.
echo $flags->bits(), "\n";
echo $changed->bits(), "\n";
}
Output
66048
514