Skip to content

Core\Ldap\Connection::search

$connection->search(Core\Ldap\Filter $filter, {…}): Core\Ldap\Entries$connection->search(
    Core\Ldap\Filter $filter,
    {…},
): Core\Ldap\Entries

Finds the entries in the directory that match a filter. It returns a Core\Ldap\Entries, and a foreach loop over it gives one Core\Ldap\Entry at a time.

You build the filter with Core\Ldap\Filter, such as Filter::equals('sAMAccountName', $login). A value from a request can go into a filter as it is. The filter escapes it, so the value cannot change what the filter matches.

The options say where and how to search. base is the DN the search starts from, and scope is how far below it the search looks. select names the attributes each entry has. sort, offset and window return one sorted page of a long list, and total() then returns the size of the whole list.

The server sends the entries in pages of up to pageSize entries. The loop reads the next page only when it reaches it, so a large result never needs all of its entries in memory at once.

Good to know: the values of an entry are tainted. A search that finds more than sizeLimit entries throws Core\Ldap\LdapError inside the loop.

Parameters

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

Options

  • baseCore\Ldap\Dn|stringoptionalThe DN the search starts from, as a Dn or as text. Left out, it is the base of the [ldap] block. Text cannot be tainted.
  • scopeCore\Ldap\Connection::ScopeoptionalHow far below base the search looks. The default is Scope::Subtree.
  • selectarray<string>optionalThe attributes each entry has, such as ['cn', 'mail']. Left out, an entry has every attribute the connection's account can read.
  • pageSizeintoptionalHow many entries the server sends at once. The default is 1000.
  • sizeLimitintoptionalThe most entries the search may find. The default, 0, is the server's own limit.
  • sortstringoptionalThe attribute the server sorts the entries by, such as cn. Left out, the entries come in the server's own order.
  • descendingbooloptionaltrue sorts from the largest value to the smallest. It needs sort. The default is false.
  • offsetintoptionalHow many sorted entries the window skips. It needs window. The default is 0.
  • windowintoptionalHow many sorted entries to return, starting after offset. It needs sort. With { sort: 'cn', offset: 20, window: 10 }, the search returns entries 21 to 30. total() then returns how many entries there are in all. The default, 0, returns every entry.
  • showDeletedbooloptionaltrue also finds deleted entries. Active Directory keeps a deleted entry for some time, with most of its attributes removed. isDeleted() checks whether an entry is one of them. The account needs the right to read deleted entries. The default is false.

Return value

Returns Core\Ldap\Entries. A Core\Ldap\Entries. Use it in a foreach loop to get each Core\Ldap\Entry.

Errors

  • LogicErrorA name in select or sort is not an attribute name, a number is out of range, window, offset or descending is given without the option it needs, the search has no base, or the connection is closed.
  • Core\Ldap\LdapErrorThe server returned an error, such as NoSuchObject for a base that does not exist. A search that finds more entries than sizeLimit throws SizeLimitExceeded in the loop.

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

Find the people in a department

<?nvs
// Shows how to find every person in one department.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
// `Filter::all` matches an entry only when every filter inside it matches.
var $sales = Core\Ldap\Filter::all(
Core\Ldap\Filter::equals('objectClass', 'user'),
Core\Ldap\Filter::equals('department', 'Sales'),
);
// `base` is where the search starts, and `select` names the attributes each entry has.
// The server returns entries in no fixed order. `sort` sorts them by `cn`.
var $people = $corp->search($sales, { base: 'OU=Company,DC=example,DC=test', select: ['cn', 'mail'], sort: 'cn' });
// The `foreach` loop reads one entry at a time. This prints Anna Berg and Carla Diaz.
foreach ($people as Core\Ldap\Entry $person) {
echo $person->string('cn') ?? '', ': ', $person->string('mail') ?? 'no address', "\n";
}
Output
Anna Berg: anna.berg@example.test
Carla Diaz: carla.diaz@example.test

One page of a sorted list

<?nvs
// Shows how to show a long list of people one page at a time, sorted by name.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $people = Core\Ldap\Filter::equals('objectClass', 'user');
// The server sorts the entries by `cn`. `offset` skips the first two,
// and `window` returns the next two.
var $page = $corp->search($people, {
base: 'OU=Company,DC=example,DC=test',
select: ['cn'],
sort: 'cn',
offset: 2,
window: 2,
});
// This prints `Carla Diaz` and `Dora Lind`.
foreach ($page as Core\Ldap\Entry $person) {
echo $person->string('cn') ?? '', "\n";
}
// `total()` returns how many entries there are in all, on every page together.
echo 'people in all: ', $page->total() ?? 0, "\n";
Output
Carla Diaz
Dora Lind
people in all: 5

A login from a request

<?nvs
// Shows how to find a user by a login name that comes from a request.
// requires: samba-ad
class Accounts
{
// `Filter::equals` escapes the login, so the login cannot change the filter.
public static function find(Core\Ldap\Connection $corp, tainted string $login): tainted string
{
var $users = $corp->search(Core\Ldap\Filter::equals('sAMAccountName', $login), { select: ['cn'] });
foreach ($users as Core\Ldap\Entry $user) {
return $user->dn()->toString();
}
return '(no such user)';
}
}
var $corp = Core\Ldap::connect('corp');
// This prints the DN of the user `Reader`.
echo Accounts::find($corp, 'Reader'), "\n";
// `*` is a wildcard in a filter that is written as text. Here it is a normal character,
// so this prints `(no such user)`.
echo Accounts::find($corp, '*'), "\n";
Output
CN=Reader,OU=Company,DC=example,DC=test
(no such user)