Core\Ldap\Entry::dn
$entry->dn(): Core\Ldap\DnReturns the DN of an entry as a Core\Ldap\Dn.
A DN (distinguished name) is the full path of an entry in the directory, such as CN=Anna Berg,OU=Company,DC=example,DC=test. Every entry has a DN, so dn never returns null.
The result is a Core\Ldap\Dn object. toString returns the whole DN as text. rdnValue returns the entry's own name, such as Anna Berg. parent returns the DN of the folder the entry is in. You can also pass the Dn to read to read the same entry again.
Good to know: the DN is not an attribute. toArray does not contain it, and string("dn") returns null.
Return value
Returns Core\Ldap\Dn. The DN, such as CN=Administrator,CN=Users,DC=example,DC=test.
Errors
Core\Ldap\LdapErrorThe server sent a DN that is not correct DN text.$kindisLdapError::Kind::Protocol.
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"]Print the names a search found
<?nvs// Shows how to print the name of every entry a search finds.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $sales = $corp->search(Core\Ldap\Filter::equals('department', 'Sales'), { select: ['cn'] });
// `rdnValue` returns the first part of the DN without `CN=`.// A search returns entries in no fixed order, so the names are sorted.array<tainted string> $names = [];foreach ($sales as Core\Ldap\Entry $person) { $names[] = $person->dn()->rdnValue();}
// This prints `Anna Berg` and `Carla Diaz`.foreach (Core\Arr::sort($names) as tainted string $name) { echo $name, "\n";}Anna BergCarla DiazThe folder an entry is in
<?nvs// Shows how to find the folder that contains an entry.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $person = $corp->read('CN=Anna Berg,OU=Company,DC=example,DC=test', { select: ['cn'] });
if ($person != null) { // This prints the whole DN. echo $person->dn()->toString(), "\n";
// `parent` removes the first part of the DN. // This prints `OU=Company,DC=example,DC=test`. var $folder = $person->dn()->parent(); if ($folder != null) { echo $folder->toString(), "\n"; }}CN=Anna Berg,OU=Company,DC=example,DC=testOU=Company,DC=example,DC=testRead the entry again later
<?nvs// Shows how to keep the DN of an entry and use it to read more attributes later.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
// The first search reads only the DN of the account `ben.kohl`.array<Core\Ldap\Dn> $found = [];var $accounts = $corp->search(Core\Ldap\Filter::equals('sAMAccountName', 'ben.kohl'), { select: ['cn'] });foreach ($accounts as Core\Ldap\Entry $account) { $found[] = $account->dn();}
// `read` accepts the `Dn` directly. This prints `ben.kohl@example.test`.foreach ($found as Core\Ldap\Dn $dn) { var $person = $corp->read($dn, { select: ['mail'] }); if ($person != null) { echo $person->string('mail') ?? 'no mail address', "\n"; }}ben.kohl@example.test