Skip to content

Core\Ldap\Connection::read

$connection->read(
    Core\Ldap\Dn|string $dn,
    {select?: array<string>},
): ?Core\Ldap\Entry

Reads the one entry at a DN. It returns a Core\Ldap\Entry, or null when there is no entry with that DN.

You give the DN as a Core\Ldap\Dn or as text. Text cannot be tainted. When part of the DN comes from a request, build the DN with Dn::of or child. They escape the special characters, so the name cannot point to another entry.

The option select names the attributes the entry should have. Without it, the entry has every attribute the account of the connection may read. Ask only for what you need: each value is sent over the network.

Good to know: the values of the entry are tainted, because the directory stores what people typed. A name in select that is not an attribute name throws LogicError. Any server error other than "no such entry" throws Core\Ldap\LdapError.

Parameters

  • $dnCore\Ldap\Dn|stringThe entry's DN, as a Dn or as text such as CN=Staff,OU=Groups,DC=example,DC=test. Text cannot be tainted. Build a DN from user input with Dn::of or child.

Options

  • selectarray<string>optionalThe attributes the entry has. Left out, it has every attribute the connection's account can read.

Return value

Returns ?Core\Ldap\Entry. A Core\Ldap\Entry, or null when no entry has this DN.

Errors

  • LogicErrorA name in select is not an attribute name, or the connection is closed.
  • Core\Ldap\LdapErrorThe server returned an error other than "no such entry".

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 one person

<?nvs
// Shows how to read one person from the directory by the DN of the entry.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
// `select` names the attributes the entry should have.
var $person = $corp->read('CN=Anna Berg,OU=Company,DC=example,DC=test', { select: ['department', 'mail'] });
if ($person != null) {
// This prints `Sales` and `anna.berg@example.test`.
echo $person->string('department') ?? 'no department', "\n";
echo $person->string('mail') ?? 'no address', "\n";
}
Output
Sales
anna.berg@example.test

An entry that does not exist

<?nvs
// Shows what `read` returns when there is no entry with the DN.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
array<string> $names = ['Ben Kohl', 'Nobody'];
// There is no entry for `Nobody`, so `read` returns `null`.
// This prints `Ben Kohl: found` and `Nobody: not found`.
foreach ($names as string $name) {
var $person = $corp->read('CN=' . $name . ',OU=Company,DC=example,DC=test', { select: ['cn'] });
echo $name, ': ', $person == null ? 'not found' : 'found', "\n";
}
Output
Ben Kohl: found
Nobody: not found

A name from a request

<?nvs
// Shows how to read an entry when the name comes from a request.
// requires: samba-ad
class Profile
{
// `read` does not accept tainted text, so `child` adds the name to a `Dn`.
// `child` escapes the special characters of the name.
public static function department(Core\Ldap\Connection $corp, tainted string $name): tainted string
{
var $company = Core\Ldap\Dn::parse('OU=Company,DC=example,DC=test');
var $person = $corp->read($company->child('CN', $name), { select: ['department'] });
return $person?->string('department') ?? '(no such person)';
}
}
var $corp = Core\Ldap::connect('corp');
// This prints `Sales`.
echo Profile::department($corp, 'Carla Diaz'), "\n";
// The `,` is escaped, so this name does not reach another entry.
// This prints `(no such person)`.
echo Profile::department($corp, 'Carla Diaz,OU=Company'), "\n";
Output
Sales
(no such person)