Core\Ldap\Entry::duration
$entry->duration(string $name): ?Core\Time\Duration$entry->duration( string $name, ): ?Core\Time\Duration
Returns the one value of an attribute as a Core\Time\Duration, such as the domain's maxPwdAge or lockoutDuration.
Active Directory writes a length of time as a negative number of 100-nanosecond steps. For example, 30 minutes is -18000000000. duration reads that number and returns a positive Duration, so toString prints 30m.
The result is null when the entry has no value for the attribute. It is also null for the smallest possible number, because Active Directory writes it for "never". For example, a domain where passwords never expire has maxPwdAge with that value.
For an attribute with several values, duration throws a LogicError. It also throws a LogicError when the value is not a length of time, and the message names the attribute.
Parameters
$namestringThe attribute's name, such asmail. Upper and lower case are the same.
Return value
Returns ?Core\Time\Duration. The duration, or null when the entry has no value for the attribute. It is also null when the number is -9223372036854775808. Active Directory uses that number for "never".
Errors
LogicErrorThe attribute has more than one value, or its value is not zero or a negative number, or it is longer than aDurationcan be. The message names the attribute.
Examples
The examples below run with this configuration file.
# 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"]How long a password lasts
<?nvs// Shows how to read how long a password lasts in a domain.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $domain = $corp->read('DC=example,DC=test', { select: ['maxPwdAge', 'minPwdAge'] });
if ($domain != null) { // This prints `A password must be changed after 6w.` echo 'A password must be changed after ', $domain->duration('maxPwdAge')?->toString() ?? 'never', ".\n";
// This prints `A new password can be changed after 1d.` echo 'A new password can be changed after ', $domain->duration('minPwdAge')?->toString() ?? 'any time', ".\n";}A password must be changed after 6w.A new password can be changed after 1d.How long an account is locked
<?nvs// Shows how to use a length of time from the directory in a calculation.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $domain = $corp->read('DC=example,DC=test', { select: ['lockoutDuration'] });
if ($domain != null) { ?Core\Time\Duration $locked = $domain->duration('lockoutDuration'); if ($locked != null) { // A locked account opens again after this time. This prints `1800`. echo $locked->toSeconds(), "\n";
// `plus` adds the time to an `Instant`. This prints `2024-03-01T12:30:00Z`. echo Core\Time\Instant::fromIso('2024-03-01T12:00:00Z')->plus($locked)->toIso(), "\n"; }}18002024-03-01T12:30:00ZA value that is not a length of time
<?nvs// Shows what `duration` does with a value that is missing, and with a value that is not a length of time.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $domain = $corp->read('DC=example,DC=test', { select: ['name'] });
if ($domain != null) { // The read did not select `maxPwdAge`, so `duration` returns `null`. This prints `no value`. echo $domain->duration('maxPwdAge')?->toString() ?? 'no value', "\n";
// `name` is the text `example`. `duration` throws a `LogicError` that names the attribute. try { echo $domain->duration('name')?->toString() ?? 'no value', "\n"; } catch (LogicError $error) { echo $error->message, "\n"; }}no valueCore\Ldap\Entry::duration: a value of `name` is not a length of time