Skip to content

Core\Ldap\Entry::uuid

$entry->uuid(string $name): ?Core\Uuid$entry->uuid(
    string $name,
): ?Core\Uuid

Returns the one value of an attribute as a Core\Uuid, such as an entry's objectGUID.

Every entry in Active Directory has an objectGUID. It does not change when the entry is renamed or moved, so it is a good key for an entry in your own database. The directory sends it as 16 bytes, in an order that is different from the usual UUID text. uuid reads those bytes and returns the Core\Uuid that Windows shows for the same entry.

If the entry has no value for the attribute, the result is null. For an attribute with several values, uuid throws a LogicError. It also throws a LogicError when the value is not 16 bytes, and the message names the attribute.

Parameters

  • $namestringThe attribute's name, such as mail. Upper and lower case are the same.

Return value

Returns ?Core\Uuid. The UUID, or null when the entry has no value for the attribute.

Errors

  • LogicErrorThe attribute has more than one value, or its value is not in the form this function reads. The message names the attribute.

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

Read the id of an entry

<?nvs
// Shows how to read the id of an entry as a UUID.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $person = $corp->read('CN=Anna Berg,OU=Company,DC=example,DC=test', { select: ['objectGUID'] });
if ($person != null) {
// `objectGUID` is different in every directory, so this prints only the length
// of its text: `36`.
?Core\Uuid $id = $person->uuid('objectGUID');
echo Core\Str::length($id?->toString() ?? ''), "\n";
}
Output
36

Find the same person again

<?nvs
// Shows how to use `objectGUID` to know that two entries are the same person.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
// The first read finds the person by the DN.
var $byDn = $corp->read('CN=Ben Kohl,OU=Company,DC=example,DC=test', { select: ['objectGUID'] });
string $first = $byDn?->uuid('objectGUID')?->toString() ?? 'none';
// The second search finds the person by the login name.
string $second = 'none';
var $found = $corp->search(Core\Ldap\Filter::equals('sAMAccountName', 'ben.kohl'), { select: ['objectGUID'] });
foreach ($found as Core\Ldap\Entry $person) {
$second = $person->uuid('objectGUID')?->toString() ?? 'none';
}
// Both have the same `objectGUID`. This prints `same person`.
echo $first == $second ? 'same person' : 'different people', "\n";
Output
same person

A value that is not a guid

<?nvs
// Shows what `uuid` does with a value that is not a GUID.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $person = $corp->read('CN=Carla Diaz,OU=Company,DC=example,DC=test', { select: ['department'] });
if ($person != null) {
// The read did not select `objectGUID`, so `uuid` returns `null`. This prints `no id`.
echo $person->uuid('objectGUID')?->toString() ?? 'no id', "\n";
// `department` is the text `Sales`. `uuid` throws a `LogicError` that names the attribute.
try {
echo $person->uuid('department')?->toString() ?? 'no id', "\n";
} catch (LogicError $error) {
echo $error->message, "\n";
}
}
Output
no id
Core\Ldap\Entry::uuid: a value of `department` is not a GUID, which is 16 bytes