Introduction au Zend Framework

 Apprendre Zend Framework


 Guide de référence Zend Framework

  •  Zend_Gdata
  •  Zend_Http
  •  Zend_InfoCard
  •  Zend_Json
  •  Zend_Layout
  •  Zend_Ldap
  •  Zend_Loader
  •  Zend_Locale
  •  Zend_Log
  •  Zend_Mail
  •  Zend_Markup
  •  Zend_Measure
  •  Zend_Memory
  •  Zend_Mime
  •  Zend_Navigation
  •  Zend_Oauth
  •  Zend_OpenId
  •  Zend_Paginator
  •  Zend_Pdf
  •  Zend_ProgressBar
  •  Zend_Queue
  •  Zend_Reflection
  •  Zend_Registry
  •  Zend_Rest

  •  Zend_Search_Lucene
  •  Zend_Serializer
  •  Zend_Server
  •  Zend_Service
  •  Zend_Session
  •  Zend_Soap
  •  Zend_Tag
  •  Zend_Test
  •  Zend_Text
  •  Zend_TimeSync
  •  Zend_Tool
  •  Zend_Tool_Framework
  •  Zend_Tool_Project
  •  Zend_Translate
  •  Zend_Uri
  •  Zend_Validate
  •  Zend_Version
  •  Zend_View
  •  Zend_Wildfire
  •  Zend_XmlRpc
  • ZendX_Console_Process_Unix
  • ZendX_JQuery
  • Translation 61.2% Update 2010-11-28 - Revision 23149 - Version ZF 1.11.x

    Chapitre 41. Zend_Ldap

    Table des matières

    41.1. Introduction
    41.1.1. Theory of operation Automatic Username Canonicalization When Binding Account Name Canonicalization Multi-domain Authentication and Failover
    41.2. API overview
    41.2.1. Configuration / options
    41.2.2. API Reference Zend_Ldap Zend_Ldap_Collection Zend_Ldap_Attribute Zend_Ldap_Dn Zend_Ldap_Filter Zend_Ldap_Node Zend_Ldap_Node_RootDse OpenLDAP ActiveDirectory eDirectory Zend_Ldap_Node_Schema OpenLDAP ActiveDirectory Zend_Ldif_Encoder
    41.3. Scénarios d'utilisation
    41.3.1. Scénarios d'authentification OpenLDAP ActiveDirectory
    41.3.2. Opérations CRUD basiques Récupérer des données depuis LDAP Ajouter des données à LDAP Supprimer de LDAP Mettre à jour LDAP
    41.3.3. Opérations avancées Copier et déplacer des entrées LDAP
    41.4. Outils
    41.4.1. Création et modification de chaines DN
    41.4.2. Utilisation de l'API des filtres pour créer des filtres de recherche
    41.4.3. Modifier les entrées LDAP en utilisant l'API Attribute
    41.5. Accès à l'arbre LDAP de manière orientée objet avec Zend_Ldap_Node
    41.5.1. Opérations CRUD basiques Récupérer des données depuis LDAP Récupérer un noeud par son DN Chercher les sous-arbres d'un noeud Ajouter un nouveau noeud à LDAP Supprimer un noeud de LDAP Mettre à jour un noeud de LDAP
    41.5.2. Opérations avancées Copier et déplacer des noeuds dans LDAP
    41.5.3. Parcours d'un arbre
    41.6. Récupérer des informations depuis un serveur LDAP
    41.6.1. RootDSE
    41.6.2. Parcours de Schema OpenLDAP ActiveDirectory
    41.7. Serialiser des données LDAP depuis/vers LDIF
    41.7.1. Serialiser une entrée LDAP vers LDIF
    41.7.2. Desérialiser une chaine LDIF dans une entrée LDAP

    41.1. Introduction

    Zend_Ldap is a class for performing LDAP operations including but not limited to binding, searching and modifying entries in an LDAP directory.

    41.1.1. Theory of operation

    This component currently consists of the main Zend_Ldap class, that conceptually represents a binding to a single LDAP server and allows for executing operations against a LDAP server such as OpenLDAP or ActiveDirectory (AD) servers. The parameters for binding may be provided explicitly or in the form of an options array. Zend_Ldap_Node provides an object-oriented interface for single LDAP nodes and can be used to form a basis for an active-record-like interface for a LDAP-based domain model.

    The component provides several helper classes to perform operations on LDAP entries (Zend_Ldap_Attribute) such as setting and retrieving attributes (date values, passwords, boolean values, ...), to create and modify LDAP filter strings (Zend_Ldap_Filter) and to manipulate LDAP distinguished names (DN) (Zend_Ldap_Dn).

    Additionally the component abstracts LDAP schema browsing for OpenLDAP and ActiveDirectoy servers Zend_Ldap_Node_Schema and server information retrieval for OpenLDAP-, ActiveDirectory- and Novell eDirectory servers (Zend_Ldap_Node_RootDse).

    Using the Zend_Ldap class depends on the type of LDAP server and is best summarized with some simple examples.

    If you are using OpenLDAP, a simple example looks like the following (note that the bindRequiresDn option is important if you are not using AD):

    $options = array(
    'host'              => '',
    'username'          => 'CN=user1,DC=foo,DC=net',
    'password'          => 'pass1',
    'bindRequiresDn'    => true,
    'accountDomainName' => '',
    'baseDn'            => 'OU=Sales,DC=foo,DC=net',
    $ldap = new Zend_Ldap($options);
    $acctname $ldap->getCanonicalAccountName('abaker',

    If you are using Microsoft AD a simple example is:

    $options = array(
    'host'                   => '',
    'useStartTls'            => true,
    'username'               => '',
    'password'               => 'pass1',
    'accountDomainName'      => '',
    'accountDomainNameShort' => 'W',
    'baseDn'                 => 'CN=Users,DC=w,DC=net',
    $ldap = new Zend_Ldap($options);
    $acctname $ldap->getCanonicalAccountName('bcarter',

    Note that we use the getCanonicalAccountName() method to retrieve the account DN here only because that is what exercises the most of what little code is currently present in this class. Automatic Username Canonicalization When Binding

    If bind() is called with a non-DN username but bindRequiresDN is TRUE and no username in DN form was supplied as an option, the bind will fail. However, if a username in DN form is supplied in the options array, Zend_Ldap will first bind with that username, retrieve the account DN for the username supplied to bind() and then re-bind with that DN.

    This behavior is critical to Zend_Auth_Adapter_Ldap, which passes the username supplied by the user directly to bind().

    The following example illustrates how the non-DN username 'abaker' can be used with bind():

    $options = array(
    'host'              => '',
    'username'          => 'CN=user1,DC=foo,DC=net',
    'password'          => 'pass1',
    'bindRequiresDn'    => true,
    'accountDomainName' => '',
    'baseDn'            => 'OU=Sales,DC=foo,DC=net',
    $ldap = new Zend_Ldap($options);
    $acctname $ldap->getCanonicalAccountName('abaker',

    The bind() call in this example sees that the username 'abaker' is not in DN form, finds bindRequiresDn is TRUE, uses 'CN=user1,DC=foo,DC=net' and 'pass1' to bind, retrieves the DN for 'abaker', unbinds and then rebinds with the newly discovered 'CN=Alice Baker,OU=Sales,DC=foo,DC=net'. Account Name Canonicalization

    The accountDomainName and accountDomainNameShort options are used for two purposes: (1) they facilitate multi-domain authentication and failover capability, and (2) they are also used to canonicalize usernames. Specifically, names are canonicalized to the form specified by the accountCanonicalForm option. This option may one of the following values:

    Tableau 41.1. Options for accountCanonicalForm

    Name Value Example
    ACCTNAME_FORM_DN 1 CN=Alice Baker,CN=Users,DC=example,DC=com

    The default canonicalization depends on what account domain name options were supplied. If accountDomainNameShort was supplied, the default accountCanonicalForm value is ACCTNAME_FORM_BACKSLASH. Otherwise, if accountDomainName was supplied, the default is ACCTNAME_FORM_PRINCIPAL.

    Account name canonicalization ensures that the string used to identify an account is consistent regardless of what was supplied to bind(). For example, if the user supplies an account name of or just abaker and the accountCanonicalForm is set to 3, the resulting canonicalized name would be EXAMPLE\abaker. Multi-domain Authentication and Failover

    The Zend_Ldap component by itself makes no attempt to authenticate with multiple servers. However, Zend_Ldap is specifically designed to handle this scenario gracefully. The required technique is to simply iterate over an array of arrays of serve options and attempt to bind with each server. As described above bind() will automatically canonicalize each name, so it does not matter if the user passes or W\bcarter or cdavis - the bind() method will only succeed if the credentials were successfully used in the bind.

    Consider the following example that illustrates the technique required to implement multi-domain authentication and failover:

    $acctname 'W\\user2';
    $password 'pass2';

    $multiOptions = array(
    'server1' => array(
    'host'                   => '',
    'username'               => 'CN=user1,DC=foo,DC=net',
    'password'               => 'pass1',
    'bindRequiresDn'         => true,
    'accountDomainName'      => '',
    'accountDomainNameShort' => 'FOO',
    'accountCanonicalForm'   => 4// ACCT_FORM_PRINCIPAL
    'baseDn'                 => 'OU=Sales,DC=foo,DC=net',
    'server2' => array(
    'host'                   => '',
    'useSsl'                 => true,
    'username'               => '',
    'password'               => 'pass1',
    'accountDomainName'      => '',
    'accountDomainNameShort' => 'W',
    'accountCanonicalForm'   => 4// ACCT_FORM_PRINCIPAL
    'baseDn'                 => 'CN=Users,DC=w,DC=net',

    $ldap = new Zend_Ldap();

    foreach (
    $multiOptions as $name => $options) {

    "Trying to bind using server options for '$name'\n";

        try {
    $acctname $ldap->getCanonicalAccountName($acctname);
    "SUCCESS: authenticated $acctname\n";
        } catch (
    Zend_Ldap_Exception $zle) {
    '  ' $zle->getMessage() . "\n";
            if (
    $zle->getCode() === Zend_Ldap_Exception::LDAP_X_DOMAIN_MISMATCH) {

    If the bind fails for any reason, the next set of server options is tried.

    The getCanonicalAccountName() call gets the canonical account name that the application would presumably use to associate data with such as preferences. The accountCanonicalForm = 4 in all server options ensures that the canonical form is consistent regardless of which server was ultimately used.

    The special LDAP_X_DOMAIN_MISMATCH exception occurs when an account name with a domain component was supplied (e.g., or FOO\abaker and not just abaker) but the domain component did not match either domain in the currently selected server options. This exception indicates that the server is not an authority for the account. In this case, the bind will not be performed, thereby eliminating unnecessary communication with the server. Note that the continue instruction has no effect in this example, but in practice for error handling and debugging purposes, you will probably want to check for LDAP_X_DOMAIN_MISMATCH as well as LDAP_NO_SUCH_OBJECT and LDAP_INVALID_CREDENTIALS.

    The above code is very similar to code used within Zend_Auth_Adapter_Ldap. In fact, we recommend that you simply use that authentication adapter for multi-domain + failover LDAP based authentication (or copy the code).

    digg delicious meneame google twitter technorati facebook