API概要

利用シナリオ

認証シナリオ

OpenLDAP

ActiveDirectory

基本的なCRUD操作

LDAPからデータを取得

Example #1 その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 は下記の構造の配列
  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 与えられた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 与えられた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 LDAPツリーを検索

  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. }

LDAPにデータを追加

Example #5 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);

LDAPからデータを削除

Example #6 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');

LDAPを更新

Example #7 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);

拡張された操作

LDAPで項目をコピーまたは移動

Example #8 LDAP項目をその全ての派生物と共に再帰的にコピー

  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 LDAP項目をその全ての派生物と共に再帰的に異なるサブツリーに移動

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

API概要