Core\Ldap\Connection::modify
$connection->modify(
Core\Ldap\Dn|string $dn,
array<Core\Ldap\Change> $changes,
): voidChanges the attributes of one entry in the directory. It returns nothing.
You give it the DN of the entry and a list of changes. Each change is made with one of four functions of Core\Ldap\Change:
Change::addadds values to an attribute.Change::removeremoves some values from an attribute.Change::removeAllremoves an attribute with all of its values.Change::replaceremoves the old values and writes the new ones.
The server makes the changes in the order of the list. If one change fails, the server makes none of them, and modify throws Core\Ldap\LdapError. Its $kind says why, such as ConstraintViolation for a value that is not there to remove.
The connection must log in as an account that may change the entry. A value is written in the same form the readers of Core\Ldap\Entry return it, so a value you read can be written back.
Good to know: the list cannot be empty. Some attributes belong to the server, such as objectGUID and objectSid, and a change to one of them throws an error.
Parameters
$dnCore\Ldap\Dn|stringThe entry's DN, as aDnor as text. Text cannot betainted. Build a DN from user input withDn::oforchild.$changesarray<Core\Ldap\Change>The changes, each made withChange::add,remove,removeAllorreplace. The list cannot be empty.
Return value
Returns void. Nothing.
Errors
LogicErrorAn attribute name is not a valid name, a value has no LDAP form, such as afloat, or the connection is closed.Core\Ldap\LdapErrorThe server returned an error.$kindsays why, such asNoSuchObject,AlreadyExists,InsufficientAccessorConstraintViolation.
Examples
The examples below run with this configuration file.
# 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"]Change the description of a unit
<?nvs// Shows how to change one attribute of an entry.// requires: samba-ad
var $corp = Core\Ldap::connect('corp-accounts');string $unit = 'OU=Garden 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) {}$corp->add($unit, ['objectClass' => 'organizationalUnit', 'description' => 'Plants and seeds']);
// `Change::replace` removes the old value and writes the new one.$corp->modify($unit, [Core\Ldap\Change::replace('description', 'Plants, seeds and tools')]);
// This prints `Plants, seeds and tools`.echo $corp->read($unit, { select: ['description'] })?->string('description') ?? 'not found', "\n";
$corp->delete($unit);Plants, seeds and toolsUpdate a person who changes jobs
<?nvs// Shows how to change several attributes of a user account in one call.// requires: samba-ad
var $corp = Core\Ldap::connect('corp-accounts');string $unit = 'OU=Bakery 2,DC=example,DC=test';string $person = 'CN=Lena Vogt,' . $unit;
// A run that stopped early can leave the accounts 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']);$corp->add($person, [ 'objectClass' => 'user', 'sAMAccountName' => 'lena.vogt', 'title' => 'Baker', 'telephoneNumber' => '+43 1 555 0101',]);
// The list has three changes. The server makes them in this order.$corp->modify($person, [ Core\Ldap\Change::replace('title', 'Head Baker'), Core\Ldap\Change::replace('department', 'Kitchen'), Core\Ldap\Change::removeAll('telephoneNumber'),]);
// This prints `Head Baker`, `Kitchen` and `no phone`.var $entry = $corp->read($person, { select: ['title', 'department', 'telephoneNumber'] });if ($entry != null) { echo $entry->string('title') ?? '', "\n"; echo $entry->string('department') ?? '', "\n"; echo $entry->has('telephoneNumber') ? 'has a phone' : 'no phone', "\n";}
$corp->delete($person);$corp->delete($unit);Head BakerKitchenno phoneOne change fails
<?nvs// Shows that `modify` makes all of its changes or none of them.// requires: samba-ad
var $corp = Core\Ldap::connect('corp-accounts');string $unit = 'OU=Library 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 first change is fine. The second removes a value the entry does not have,// so `modify` throws an error. This prints `ConstraintViolation`.try { $corp->modify($unit, [ Core\Ldap\Change::add('description', 'Films'), Core\Ldap\Change::remove('description', 'Games'), ]);} catch (Core\Ldap\LdapError $error) { echo $error->kind == Core\Ldap\LdapError::Kind::ConstraintViolation ? 'ConstraintViolation' : 'another error', "\n";}
// The first change was not made either. This prints `Books`.var $values = $corp->read($unit, { select: ['description'] })?->strings('description') ?? [];echo Core\Str::join($values, ', '), "\n";
$corp->delete($unit);ConstraintViolationBooks