Asynchronous LDAP client built on top of ReactPHP
$loop = React\EventLoop\Factory::create();
$client = new Fneufneu\React\Ldap\Client($loop, 'ldap://myhost.com');
$client->bind('user', 'password')->then(function ($client) {
$res = $client->search([
'base' => 'cn=foo, o=example',
'filter' => 'mail=*',
]);
$res->on('data', function ($data) {
echo json_encode($data) . PHP_EOL;
});
$client->unbind();
});
$loop->run();
The Client
class is the main class in this package that let you connect to
a LDAP Server.
$client = new Client($loop, 'ldap://host', $options);
The constructor needs
- an
EventLoop
- an URI to the LDAP host (
ldap://myhost.com:389
,ldaptls://yourhost.fr
,ldaps://mycomp.com
) - an optional array of options
option | description |
---|---|
connector | a custom React\Socket\ConnectorInterface |
timeout | timeout in sec for default Connector, connect() and bind() request |
The client emit usual event: end, close and error:
$client->on('end', function () {
echo "client's connection ended" . PHP_EOL;
});
$client->on('close', function () {
echo "client's connection closed" . PHP_EOL;
});
$client->on('error', function (Exception $e) {
echo 'error: '.$e->getMessage() . PHP_EOL;
});
bind call connect() and return a promise.
$client->bind('toto', 'password')->done(function ($client) {
echo 'successfuly binded' . PHP_EOL;
}, function (Exception $e) {
echo 'bind failed with error: ' . $e->getMessage() . PHP_EOL;
});
Send an unbind() request to the server.
The server will usually disconnect the client just after.
Performs a ldap_search and return a Result object see Result usage
The search(array): Result
method takes an array of options.
option | type | default value | description |
---|---|---|---|
base | string | no default | mandatory The base DN |
filter | string | (objectclass=*) | The search filter |
attributes | array | [] | An array of the required attributes |
scope | enum | Ldap::wholeSubtree | Ldap::wholeSubtree, Ldap::singleLevel, Ldap::baseObject |
pagesize | int | 0 | enable automatic paging |
sizelimit | int | 0 | Enables you to limit the count of entries fetched. Setting this to 0 means no limit |
timelimit | int | 0 | Sets the number of seconds how long is spend on the search. Setting this to 0 means no limit |
bool | false | ||
derefaliases | enum | Ldap::never | Specifies how aliases should be handled during the search (Ldap::never , ldap::searching , Ldap::finding , Ldap:always ) |
resultfilter | ? | ? | ? |
In order to retrieve results beyond the usual 1000 limits, you can set pagesize to an int > 0 to page results.
When enabled, the Client use an internal mechanisms to automate the process and perform as many search() as necessary.
❗ not tested
add(string $dn, array entry): Result
❗ not tested
modify(string $dn, array changes): Result
example:
$result = $client->modify('cn=test', [
['add' => ['mail' => ['[email protected]']],
['delete' => ['email' => ['[email protected]']],
['replace' => ['sn' => ['John']],
],
]);
delete(string $dn): Result
❗ not tested
modDN(string $dn, string $newDn, bool $deleteOldDn, string $newSuperior): Result
❗ not tested
compare(string $dn, string $attribute, string $value): Result
Result
emit usual event: data, end and error:
$result->on('data', function ($data) {
// one search entry
});
$result->on('end', function ($data) {
// all search entries or an empty array if none
});
$result->on('error', function (Exception $e) {
echo 'error: '.$e->getMessage() . PHP_EOL;
});
See examples.