Finding Entries

Find a Single Entry

You can easily find a single record by passing an LDAP search filter to the find() method. This method will return the first entry from the results.

<?php
$searchfilter = 'cn=juser';

$row = $ldap->find($searchfilter);

// This will return an array of data or an empty array. The array will be
// 'cleaned' up to be more user-friendly.
// The returned result will ALWAYS include the user's DN.

// $row might look like:

[
    'cn'=>'juser',
    'fullname'=>'Joe User',
    'email'=>'juser@mycompany.org',
    'dn'=>'CN=juser,OU=users,OU=mycompany,OU=org'
]

Find All Entries

You can also find all entries matching a search filter.

<?php
// $searchfilter can be as complex as you need
// $searchfilter = (|(sn=smit*)(givenname=jo*));

$searchfilter = 'locality=Edmonton';
$rows = $ldap->findAll($searchfilter);

// $rows will be an multi-dimensional array of entries.

Finding Entries by Attribute

The LdapSearch() class comes with convenience methods for finding entries by any attribute in the directory. The default attributes are:

<?php
[
    'lastname'=>'sn',
    'firstname'=>'givenname',
    'userid'=>'cn',
    'locality'=>'l'
]

The keys in this array matches the name of the method you are calling. The value in the array matches the actual directory attribute. See the example below.

<?php
// Find all entries by last name
// The method name is the upper case first of the key in the array.
// findAllBy + ucfirst(lastname)

$rows = $ldap->findAllByLastname('smith');

// Find a single entry by lastname (ldap_first_entry is used)
$row = $ldap->findOneByLastname('Smith');

// More likely you would use something like the cn (userid)
$row = $ldap->findOneByUserid('juser');

As with the find() and findAll() methods, you can pass more arguments as required. See the PHP documentation on the additional attributes.

<?php
$row = $ldap->findAllByLastname(
    'smith*',
    $attributes,
    $dn,
    $attrsonly,
    $sizelimit,
    $timelimit,
    $deref
);

Additionally, you can find entries by multiple attributes using booleans (and/or). For example:

<?php
// Boolean And
$rows = $ldap->findAllByLastnameAndFirstname('User', 'J*');

// Boolean Or
$rows = $ldap->findAllByLocalityOrPostalcode('City', 'T6H 3S5');

// Boolean Or using the same attribute 2x
$rows = $ldap->findAllByLastnameOrLastname('User', 'Doe');

// The same methods work with `findOneBy()` as well, though this may be less useful, depending on
// what you are searching for

$row = $ldap->findOneByLastnameAndFirstname('User', 'Joe');

Setting FindBy Attributes

You can customize the ‘findBy’ attributes to suit your directory set up. The findByAttributes are a key value pair where the key is your findBy method name, and the value is the actual attribute in the directory.

<?php
$ldap->setFindByAttribute('fullName', 'displayname');
// The , character is escaped by default. Don't escape for now :)
$ldap->setEscapeChars();
$rows = $ldap->findOneByFullName('User, Joe');

// Set all findBy attributes

$findby = [
    'userid'=>'samaccountname',
    'surname'=>'sn',
    'commonName'=>'cn'
];
$ldap->setFindByAttributes($findby);

// This will set the search filter to cn=myname
$rows = $ldap->findAllByCommonName('myname');