Skip to content

Fneufneu/php-ldap-react

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

php-ldap-react

Asynchronous LDAP client built on top of ReactPHP

Quickstart example

$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();

Client usage

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

Supported options

option description
connector a custom React\Socket\ConnectorInterface
timeout timeout in sec for default Connector, connect() and bind() request

Events

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()

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;
});

unbind()

Send an unbind() request to the server.

The server will usually disconnect the client just after.

search()

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
typesonly bool false set to true if only attribute types are wanted (not supported)
derefaliases enum Ldap::never Specifies how aliases should be handled during the search (Ldap::never, ldap::searching, Ldap::finding, Ldap:always)
resultfilter ? ? ?

Paging

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.

add()

not tested

add(string $dn, array entry): Result

modify()

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()

delete(string $dn): Result

modDN()

not tested

modDN(string $dn, string $newDn, bool $deleteOldDn, string $newSuperior): Result

compare()

not tested

compare(string $dn, string $attribute, string $value): Result

Result usage

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;
});

Server usage

See examples.

About

Asynchronous LDAP client built on top of ReactPHP

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages