From 260afeab19811e6c09e8a9f229007d32029ad34f Mon Sep 17 00:00:00 2001
From: Jonathan Swaelens <swaelens.jonathan@opensides.be>
Date: Sat, 6 Aug 2022 15:15:04 +0200
Subject: [PATCH] :sparkles: feat(doc): Add examples for fusiondirectory-ldap
 library

Add examples for fusiondirectory-ldap library

Signed-off-by: Jonathan Swaelens <swaelens.jonathan@opensides.be>
---
 source/fusiondirectory/libraries/ldap.rst | 99 ++++++++++++++++++-----
 1 file changed, 78 insertions(+), 21 deletions(-)

diff --git a/source/fusiondirectory/libraries/ldap.rst b/source/fusiondirectory/libraries/ldap.rst
index ab7212a..21c43d2 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
 --------------------
 
-- 
GitLab