Skip to content

Core\Ldap\Connection::add

Adds a new entry to the directory. It returns nothing.

You give it the DN of the new entry and its attributes. The attributes are an array keyed by the attribute name, such as ["objectClass" => "organizationalUnit", "description" => "Sales"]. A list as the value writes one value per element, such as "objectClass" => ["top", "group"].

A value is written in the same form the readers of Core\Ldap\Entry return it. A string is written as it is. An int, a bool and a Core\Ldap\Dn are written as the text the server expects, so a value you read can be written back. A float has no LDAP form, and add throws a LogicError for it.

If the server does not accept the entry, add throws Core\Ldap\LdapError. Its $kind says why: AlreadyExists when an entry has that DN, NoSuchObject when the parent entry does not exist, and InsufficientAccess when the account may not add it.

Good to know: the connection must log in as an account that may add entries below the parent. Every entry needs an objectClass, and the server adds attributes of its own, such as objectGUID and whenCreated.

Parameters

  • $dnCore\Ldap\Dn|stringThe entry's DN, as a Dn or as text. Text cannot be tainted. Build a DN from user input with Dn::of or child.
  • $attributesarray<mixed>The entry's attributes, keyed by name, such as ['objectClass' => ['top', 'group'], 'cn' => 'Staff']. A value is written in the form the methods of Entry return it, and a list writes one value per element. A value may be tainted. A name that can come from outside the program does not compile.

Return value

Returns void. Nothing.

Errors

  • LogicErrorAn attribute name is not a valid name, a value has no LDAP form, such as a float, or the connection is closed.
  • Core\Ldap\LdapErrorThe server returned an error. $kind says why, such as NoSuchObject, AlreadyExists, InsufficientAccess or ConstraintViolation.

Examples

The examples below run with this configuration file.

nvs.toml
# These examples use the LDAP directory server on this computer at port 16636.
# They log in as the administrator, because they change accounts in the directory.
# `tls_ca_file` is the certificate that the server's certificate is checked against.
[ldap.corp-accounts]
url = "ldaps://127.0.0.1:16636"
base = "DC=example,DC=test"
user = "CN=Administrator,CN=Users,DC=example,DC=test"
password = "Novis-test-1"
tls_ca_file = "../../../../../tests/db/ca.crt"
timeout = "5s"
# The program may open the `corp-accounts` directory.
[capabilities.ldap]
connect = ["corp-accounts"]

Add a unit

<?nvs
// Shows how to add a new unit to the directory and read it back.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp-accounts');
string $unit = 'OU=Garden Add 1,DC=example,DC=test';
// A run that stopped early can leave the unit behind, so the program deletes it first.
try {
$corp->delete($unit);
} catch (Core\Ldap\LdapError $missing) {
}
// The array has one attribute per key. Every entry needs an `objectClass`.
$corp->add($unit, ['objectClass' => 'organizationalUnit', 'description' => 'Plants and seeds']);
// This prints `Plants and seeds`.
echo $corp->read($unit, { select: ['description'] })?->string('description') ?? 'not found', "\n";
$corp->delete($unit);
Output
Plants and seeds

Add a user with several values

<?nvs
// Shows how to add a user account with a list of values and a DN as a value.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp-accounts');
string $unit = 'OU=Bakery Add 2,DC=example,DC=test';
string $person = 'CN=Lena Vogt,' . $unit;
// A run that stopped early can leave the entries behind, so the program deletes them first.
array<string> $leftovers = [$person, $unit];
foreach ($leftovers as string $left) {
try {
$corp->delete($left);
} catch (Core\Ldap\LdapError $missing) {
}
}
$corp->add($unit, ['objectClass' => 'organizationalUnit']);
// A list writes one value per element. A `Dn` is written as its text.
$corp->add($person, [
'objectClass' => 'user',
'sAMAccountName' => 'lena.add',
'title' => 'Baker',
'otherTelephone' => ['+43 1 555 0101', '+43 1 555 0102'],
'manager' => Core\Ldap\Dn::parse('CN=Anna Berg,OU=Company,DC=example,DC=test'),
]);
// This prints `Baker`, `2 phone numbers` and the DN of the manager.
var $entry = $corp->read($person, { select: ['title', 'otherTelephone', 'manager'] });
if ($entry != null) {
echo $entry->string('title') ?? '', "\n";
echo Core\Arr::count($entry->strings('otherTelephone') ?? []), " phone numbers\n";
echo $entry->string('manager') ?? '', "\n";
}
$corp->delete($person);
$corp->delete($unit);
Output
Baker
2 phone numbers
CN=Anna Berg,OU=Company,DC=example,DC=test

The entry already exists

<?nvs
// Shows what happens when an entry with the same DN is already in the directory.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp-accounts');
string $unit = 'OU=Library Add 3,DC=example,DC=test';
// A run that stopped early can leave the unit behind, so the program deletes it first.
try {
$corp->delete($unit);
} catch (Core\Ldap\LdapError $missing) {
}
$corp->add($unit, ['objectClass' => 'organizationalUnit', 'description' => 'Books']);
// The second `add` uses the same DN, so it throws an error. This prints `AlreadyExists`.
try {
$corp->add($unit, ['objectClass' => 'organizationalUnit', 'description' => 'Films']);
} catch (Core\Ldap\LdapError $error) {
echo $error->kind == Core\Ldap\LdapError::Kind::AlreadyExists ? 'AlreadyExists' : 'another error', "\n";
}
// The first entry did not change. This prints `Books`.
echo $corp->read($unit, { select: ['description'] })?->string('description') ?? 'not found', "\n";
$corp->delete($unit);
Output
AlreadyExists
Books