Documentation

Usage Scenarios - Zend_Ldap

Usage Scenarios

Authentication scenarios

OpenLDAP

ActiveDirectory

Basic CRUD operations

Retrieving data from the LDAP

Example #1 Getting an entry by its DN

  1. $options = array(/* ... */);
  2. $ldap = new Zend_Ldap($options);
  3. $ldap->bind();
  4. $hm = $ldap->getEntry('cn=Hugo Müller,ou=People,dc=my,dc=local');
  5. /*
  6. $hm is an array of the following structure
  7. array(
  8.     'dn'          => 'cn=Hugo Müller,ou=People,dc=my,dc=local',
  9.     'cn'          => array('Hugo Müller'),
  10.     'sn'          => array('Müller'),
  11.     'objectclass' => array('inetOrgPerson', 'top'),
  12.     ...
  13. )
  14. */

Example #2 Check for the existence of a given DN

  1. $options = array(/* ... */);
  2. $ldap = new Zend_Ldap($options);
  3. $ldap->bind();
  4. $isThere = $ldap->exists('cn=Hugo Müller,ou=People,dc=my,dc=local');

Example #3 Count children of a given DN

  1. $options = array(/* ... */);
  2. $ldap = new Zend_Ldap($options);
  3. $ldap->bind();
  4. $childrenCount = $ldap->countChildren(
  5.                             'cn=Hugo Müller,ou=People,dc=my,dc=local');

Example #4 Searching the LDAP tree

  1. $options = array(/* ... */);
  2. $ldap = new Zend_Ldap($options);
  3. $ldap->bind();
  4. $result = $ldap->search('(objectclass=*)',
  5.                         'ou=People,dc=my,dc=local',
  6.                         Zend_Ldap_Ext::SEARCH_SCOPE_ONE);
  7. foreach ($result as $item) {
  8.     echo $item["dn"] . ': ' . $item['cn'][0] . PHP_EOL;
  9. }

Adding data to the LDAP

Example #5 Add a new entry to the LDAP

  1. $options = array(/* ... */);
  2. $ldap = new Zend_Ldap($options);
  3. $ldap->bind();
  4. $entry = array();
  5. Zend_Ldap_Attribute::setAttribute($entry, 'cn', 'Hans Meier');
  6. Zend_Ldap_Attribute::setAttribute($entry, 'sn', 'Meier');
  7. Zend_Ldap_Attribute::setAttribute($entry, 'objectClass', 'inetOrgPerson');
  8. $ldap->add('cn=Hans Meier,ou=People,dc=my,dc=local', $entry);

Deleting from the LDAP

Example #6 Delete an existing entry from the LDAP

  1. $options = array(/* ... */);
  2. $ldap = new Zend_Ldap($options);
  3. $ldap->bind();
  4. $ldap->delete('cn=Hans Meier,ou=People,dc=my,dc=local');

Updating the LDAP

Example #7 Update an existing entry on the LDAP

  1. $options = array(/* ... */);
  2. $ldap = new Zend_Ldap($options);
  3. $ldap->bind();
  4. $hm = $ldap->getEntry('cn=Hugo Müller,ou=People,dc=my,dc=local');
  5. Zend_Ldap_Attribute::setAttribute($hm, 'mail', 'mueller@my.local');
  6. Zend_Ldap_Attribute::setPassword($hm,
  7.                                  'newPa$$w0rd',
  8.                                  Zend_Ldap_Attribute::PASSWORD_HASH_SHA1);
  9. $ldap->update('cn=Hugo Müller,ou=People,dc=my,dc=local', $hm);

Extended operations

Copy and move entries in the LDAP

Example #8 Copy a LDAP entry recursively with all its descendants

  1. $options = array(/* ... */);
  2. $ldap = new Zend_Ldap($options);
  3. $ldap->bind();
  4. $ldap->copy('cn=Hugo Müller,ou=People,dc=my,dc=local',
  5.             'cn=Hans Meier,ou=People,dc=my,dc=local',
  6.             true);

Example #9 Move a LDAP entry recursively with all its descendants to a different subtree

  1. $options = array(/* ... */);
  2. $ldap = new Zend_Ldap($options);
  3. $ldap->bind();
  4. $ldap->moveToSubtree('cn=Hugo Müller,ou=People,dc=my,dc=local',
  5.                      'ou=Dismissed,dc=my,dc=local',
  6.                      true);

Copyright

© 2006-2021 by Zend by Perforce. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts