Skip to content

Core\Ldap\Connection::searchChanges

$connection->searchChanges(
    Core\Ldap\Filter $filter,
    {base?: Core\Ldap\Dn|string, select?: array<string>, cookie?: tainted bytes},
): Core\Ldap\Changes

Finds the entries that changed since an earlier call. A program uses it to keep its own copy of the directory up to date, without reading every entry each time. It uses DirSync, a feature of Active Directory.

The first call has no cookie, so it returns every entry that matches the filter. After the foreach loop, cookie() returns a cookie. Store it, for example in a database. The next call gives that cookie, and returns only the entries that changed after it.

A changed entry has only the attributes that changed. select names the attributes to watch. A deleted entry is in the result too, and its isDeleted() returns true.

Good to know: the cookie covers only the entries the loop read. If the loop stops early, the next call returns the other entries again. base must be the root of the domain. A server without DirSync throws Core\Ldap\LdapError with the kind Unsupported.

Parameters

  • $filterCore\Ldap\FilterWhich entries to return, such as Filter::equals('objectClass', 'user').

Options

  • baseCore\Ldap\Dn|stringoptionalThe root of the domain, as a Dn or as text. Left out, it is the base of the [ldap] block. Active Directory throws an error for any other DN. Text cannot be tainted.
  • selectarray<string>optionalThe attributes to watch, such as ['cn', 'mail']. A changed entry has only the attributes that changed. The attributes filter uses and isDeleted are always watched too. Left out, every attribute is watched.

Return value

Returns Core\Ldap\Changes. A Core\Ldap\Changes. Use it in a foreach loop to get each changed Core\Ldap\Entry. A deleted entry has isDeleted() set to true.

Errors

  • LogicErrorA name in select is not an attribute name, there is no base, or the connection is closed.
  • Core\Ldap\LdapErrorThe server returned an error. A server without DirSync throws Unsupported.

Changelog

VersionChange
0.0.6Core\Ldap\Connection->hasValue() checks whether an entry has a value, and searchChanges() reads the entries that changed. Core\Ldap\Filter::approximately builds an approximate match. The old names were compare, changes and approx.

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

The first sync

<?nvs
// Shows how to start a sync. The first call returns every person who matches the filter.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $people = Core\Ldap\Filter::all(
Core\Ldap\Filter::equals('objectClass', 'user'),
Core\Ldap\Filter::equals('department', 'Sales'),
);
// Without a `cookie`, the result has every entry that matches the filter.
var $changes = $corp->searchChanges($people, { select: ['department'] });
// A DN comes from the server, so its text is tainted.
array<tainted string> $names = [];
foreach ($changes as Core\Ldap\Entry $person) {
$names[] = $person->dn()->toString();
}
// The server returns the changes in no fixed order, so the program sorts the DNs.
// This prints the DNs of Anna Berg and Carla Diaz.
foreach (Core\Arr::sort($names) as tainted string $name) {
echo $name, "\n";
}
// After the loop, `cookie()` returns the cookie for the next call.
echo 'the cookie is ready: ', Core\Bytes::length($changes->cookie()) > 0 ? 'yes' : 'no', "\n";
Output
CN=Anna Berg,OU=Company,DC=example,DC=test
CN=Carla Diaz,OU=Company,DC=example,DC=test
the cookie is ready: yes

Only what changed

<?nvs
// Shows how a second call with the cookie returns only the entries that changed.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $sales = Core\Ldap\Filter::all(
Core\Ldap\Filter::equals('objectClass', 'user'),
Core\Ldap\Filter::equals('department', 'Sales'),
);
// The first call reads every person in Sales. The loop reads them all, so the cookie is complete.
var $first = $corp->searchChanges($sales, { select: ['department'] });
foreach ($first as Core\Ldap\Entry $person) {
}
// Nothing changed since the first call, so this prints `changed: 0`.
var $next = $corp->searchChanges($sales, { select: ['department'], cookie: $first->cookie() });
uint $changed = 0;
foreach ($next as Core\Ldap\Entry $person) {
$changed = $changed + 1;
}
echo 'changed: ', $changed, "\n";
Output
changed: 0

A loop that stops early

<?nvs
// Shows what happens to the cookie when a loop stops before the last entry.
// requires: samba-ad
class Count
{
// Reads every entry in `$changes` and returns how many there were.
public static function all(Core\Ldap\Changes $changes): uint
{
uint $count = 0;
foreach ($changes as Core\Ldap\Entry $entry) {
$count = $count + 1;
}
return $count;
}
}
var $corp = Core\Ldap::connect('corp');
var $sales = Core\Ldap\Filter::all(
Core\Ldap\Filter::equals('objectClass', 'user'),
Core\Ldap\Filter::equals('department', 'Sales'),
);
// This loop stops after the first of the two people in Sales.
var $first = $corp->searchChanges($sales, { select: ['department'] });
foreach ($first as Core\Ldap\Entry $person) {
break;
}
// The cookie covers only what the loop read. So the next call returns both people again.
var $next = $corp->searchChanges($sales, { select: ['department'], cookie: $first->cookie() });
echo 'people returned again: ', Count::all($next), "\n";
// That loop read every entry, so this cookie is complete. This prints `0`.
echo 'people after that: ', Count::all($corp->searchChanges($sales, { select: ['department'], cookie: $next->cookie() })), "\n";
Output
people returned again: 2
people after that: 0