diff --git a/source/fusiondirectory/libraries/ldap.rst b/source/fusiondirectory/libraries/ldap.rst index ab7212a8f47fac63cdf4ceee5a6ac4f393007abb..21c43d2274b42d04147ebe4603153a34f5e35c9e 100644 --- a/source/fusiondirectory/libraries/ldap.rst +++ b/source/fusiondirectory/libraries/ldap.rst @@ -17,48 +17,105 @@ You must put the src/FusionDirectory folder in the include_path of your PHP conf Example ------- +- Connect and bind to LDAP as external + .. code-block:: php <?php require 'FusionDirectory/Ldap/autoload.php'; use \FusionDirectory\Ldap; + /* Open a connection */ + $ldap = new Ldap\Link('ldapi:///'); + + /* External bind */ + $ldap->saslBind('', '', 'EXTERNAL'); + +- Connect and bind to LDAP as user + +.. code-block:: php + <?php + require 'FusionDirectory/Ldap/autoload.php'; + + use \FusionDirectory\Ldap; /* Open a connection */ - $link = new Ldap\Link('ldapi:///'); + $ldap = new Ldap\Link('ldap://localhost:389/'); + /* Simple bind */ - $link->bind('cn=admin,dc=base', 'password'); - /* Run a search on the server */ - $list = $link->search( - 'ou=branch,dc=base', - "(&(objectClass=myClass)(myAttribute=*))", - ['myAttribute'], - 'subtree' - ); - /* Throw Ldap\Exception if the search returned an error */ - $list->assert(); + $ldap->bind('cn=admin,dc=fusiondirectory', 'password'); - /* Iterate on search results */ - foreach ($list as $dn => $entry) { - echo $dn.': '.$entry['myAttribute'][0]."\n"; - } - /* Throw if there was an error while iterating */ - $list->assertIterationWentFine(); +- Add an entry + +.. code-block:: php - /* Read root DSE information */ - $dse = $link->getDSE(); + <?php + require 'FusionDirectory/Ldap/autoload.php'; + use \FusionDirectory\Ldap; + /* Open a connection */ + $ldap = new Ldap\Link('ldap://localhost:389/'); + + /* Simple bind */ + $ldap->bind('cn=admin,dc=fusiondirectory', 'password'); + /* Add an entry */ - $add = $link->add( - 'ou=entry,ou=branch,dc=base', + $add = $ldap->add( + 'ou=entry,ou=branch,dc=fusiondirectory', [ 'objectClass' => 'organizationalUnit', 'ou' => 'entry' ] ); + /* Throw Ldap\Exception if the add operation returned an error */ $add->assert(); +- Delete an entry + +.. code-block:: php + + <?php + require 'FusionDirectory/Ldap/autoload.php'; + + use \FusionDirectory\Ldap; + /* Open a connection */ + $ldap = new Ldap\Link('ldap://localhost:389/'); + + /* Simple bind */ + $ldap->bind('cn=admin,dc=fusiondirectory', 'password'); + + /* Delete an entry */ + $delete = $ldap->delete('ou=entry,ou=branch,dc=fusiondirectory'); + + /* Throw Ldap\Exception if the delete operation returned an error */ + $delete->assert(); + +- Make a search + +.. code-block:: php + + <?php + require 'FusionDirectory/Ldap/autoload.php'; + + use \FusionDirectory\Ldap; + /* Open a connection */ + $ldap = new Ldap\Link('ldap://localhost:389/'); + + /* Simple bind */ + $ldap->bind('cn=admin,dc=fusiondirectory', 'password'); + + /* Make a search */ + $list = $ldap->search('dc=fusiondirectory', '(ou=*)', ['ou'], 'subtree'); + + /* Throw FusionDirectory\Ldap\Exception if there was an error */ + $list->assert(); + + /* Browse results, Ldap\Result is Traversable */ + foreach ($list as $dn => $attributes) { + echo $dn.': '.$attributes['ou'][0]."\n"; + } + Other useful helpers --------------------