Skip to content

Core\Ldap::connect

Opens an LDAP directory, such as Active Directory, under the name an operator gave it.

An [ldap.<name>] block in nvs.toml has the server's URL, the account to log in with and its password. Core\Ldap::connect opens the block with that name and logs in as that account. Your program writes the name and nothing else, so a password never reaches your source code.

The block can list more than one URL. The servers are tried in that order, and the first one that answers is used. If none answers, the call throws a Core\Ldap\LdapError, and its kind is Unavailable.

Every call with the same name returns the same connection while the request runs. The connection is closed when the request ends.

Good to know: the operator decides which blocks a program may open. [capabilities.ldap] connect in nvs.toml lists the names. A name that is not in the list throws a RuntimeError.

Parameters

  • $namestringThe block to open: "corp" is [ldap.corp].

Return value

Returns Core\Ldap\Connection. A Core\Ldap\Connection, logged in as the block's user. It is closed when the request ends.

Errors

  • RuntimeErrorldap.connect does not grant $name, or no [ldap.<name>] block has that name.
  • IOErrorThis core already has the block's pool.max connections open, and none was free within the block's timeout.
  • Core\Ldap\LdapErrorNo URL in the block responded, or the server did not accept the block's user and password. $kind says why.

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

Sign in to the company directory

<?nvs
// Shows how to open a directory by its name and find one person in it.
// `Core\Ldap::connect` opens the `[ldap.corp]` block of `nvs.toml`.
// requires: samba-ad
try {
var $corp = Core\Ldap::connect('corp');
var $people = $corp->search(Core\Ldap\Filter::equals('sAMAccountName', 'anna.berg'), { select: ['mail'] });
// This prints `anna.berg@example.test`.
foreach ($people as Core\Ldap\Entry $person) {
echo $person->string('mail') ?? 'no mail address', "\n";
}
} catch (Core\Ldap\LdapError $error) {
// `kind` says why the call failed, for example `Unavailable` when the server does not answer.
echo "The directory did not answer.\n";
}
Output
anna.berg@example.test

A directory the operator did not allow

<?nvs
// Shows what happens when a program opens a directory the operator did not allow.
// `[capabilities.ldap] connect` in `nvs.toml` lists the names a program may open.
// This program may open `corp`, but not `hr`, so the call throws a `RuntimeError`.
try {
Core\Ldap::connect('hr');
echo "The directory is open.\n";
} catch (RuntimeError $error) {
echo $error->message, "\n";
}
Output
Core\Ldap::connect needs the capability `ldap.connect` for hr, which is not granted
help: add `connect = ["hr"]` under `[capabilities.ldap]` in nvs.toml

A page that works without the directory

<?nvs
// Shows how a page can still show a name when the directory has no answer for it.
// requires: samba-ad
class People
{
// Returns the full name of a person from the `[ldap.corp]` directory, or the login name.
// The result is `tainted`, because a value from the directory comes from outside the program.
public static function displayName(string $login): tainted string
{
try {
var $corp = Core\Ldap::connect('corp');
var $people = $corp->search(Core\Ldap\Filter::equals('sAMAccountName', $login), { select: ['displayName'] });
foreach ($people as Core\Ldap\Entry $person) {
return $person->string('displayName') ?? $login;
}
} catch (Core\Ldap\LdapError $error) {
// The directory did not answer. The login name below is the result.
}
return $login;
}
}
// This prints `Hello, Anna Berg`.
echo 'Hello, ', People::displayName('anna.berg'), "\n";
// The directory has no person with this login, so this prints `Hello, guest`.
echo 'Hello, ', People::displayName('guest'), "\n";
Output
Hello, Anna Berg
Hello, guest