Show
Ignore:
Timestamp:
05/15/08 13:22:25 (5 months ago)
Author:
richi
Message:

peer::ldap: added functionality and test cases

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/peer/ldap/stubLDAPConnection.php

    r1559 r1570  
    2424     * @var  stubLDAPURL 
    2525     */ 
    26     public $ldap; 
     26    protected $ldap; 
    2727    /** 
    2828     * ldap connection identifier 
     
    3030     * @var  resource 
    3131     */ 
    32     public $handle; 
     32    protected $handle; 
    3333 
    3434    /** 
     
    9494    public function unbind() 
    9595    { 
    96         ldap_unbind($this->handle); 
     96        if(gettype($this->handle) !== 'unknown type' ) { 
     97            ldap_unbind($this->handle); 
     98        } 
    9799    } 
    98100 
     101    /** 
     102     * Changes the originally used base dn (distinguished name). 
     103     * 
     104     * @param  string  $newBaseDn 
     105     */ 
     106    public function setBaseDn($newBaseDn) 
     107    { 
     108        $this->ldap->setBaseDn($newBaseDn); 
     109    } 
     110     
    99111    /** 
    100112     * Retrieves the LDAP entries. 
     
    129141        } 
    130142 
     143        // TODO isParamValid() nötig? 
    131144        switch($scope) { 
    132145            case 'base': 
  • trunk/src/main/php/net/stubbles/peer/ldap/stubLDAPEntry.php

    r1559 r1570  
    1818     * attributes 
    1919     * 
    20      * @var  array(attrName => array(v[, v ...]) 
     20     * @var  array<string,array<string>> 
    2121     */ 
    2222    protected $attributes; 
     
    2424     * objectClass attribute 
    2525     * 
    26      * @var  array(v[, v ...]) 
     26     * @var  array<string> 
    2727     */ 
    2828    protected $objectClass; 
     
    7070 
    7171    /** 
     72     * Gets the objectClass values. 
     73     * 
     74     * @return  array<string> 
     75     */ 
     76    public function getObjectClassValues() 
     77    { 
     78        return $this->objectClass; 
     79    } 
     80 
     81    /** 
    7282     * Gets the attributes. 
    7383     * 
    74      * @return  array(k,v) 
     84     * @return  array<string,array<string>> 
    7585     */ 
    7686    public function getAttributes() 
     
    8797    public function getAttributeValuesByName($name) 
    8898    { 
    89         $name = strtolower($name); 
    90         return $this->attributes[$name]
     99        // new ArrayAccessor 
     100        return (isset($this->attributes[$name]) !== false) ? $this->attributes[$name] : null
    91101    } 
    92102 
     
    94104     * Gets the attribute names. 
    95105     * 
    96      * @return  array(string) 
     106     * @return  array<string> 
    97107     */ 
    98108    public function getAttributeNames() 
  • trunk/src/main/php/net/stubbles/peer/ldap/stubLDAPSearchResult.php

    r1559 r1570  
    1414 * @subpackage  peer_ldap 
    1515 */ 
    16 class stubLDAPSearchResult extends stubBaseObject 
     16class stubLDAPSearchResult extends stubBaseObject implements Iterator 
    1717{ 
    1818    /** 
    19      * entries 
     19     * ldap connection identifier 
    2020     * 
    21      * @var  array(stubLDAPEntry) 
     21     * @var  resource 
    2222     */ 
    23     protected $entries
     23    protected $handle
    2424    /** 
    25      * entries amount 
     25     * ldap result identifier 
    2626     * 
    27      * @var  int 
     27     * @var  resource 
    2828     */ 
    29     protected $entriesCount; 
     29    protected $result; 
     30    /** 
     31     * current ldap entryId 
     32     * 
     33     * @var  ressource 
     34     */ 
     35    protected $current; 
    3036 
    3137    /** 
    3238     * constructor (creates stubLDAPEntry objects) 
     39     * 
     40     * Unset calls remove the count value for better looping 
     41     * and because this value is ascertainable later. 
    3342     * 
    3443     * @param  resource  $handle  ldap connection identifier 
     
    3746    public function __construct($handle, $result) 
    3847    { 
    39         $info = ldap_get_entries($handle, $result); 
    40         $this->entriesCount = $info['count']; 
    41         unset($info['count']); 
    42  
    43         foreach ($info as $numKey => $attributes) { 
    44  
    45             $entryAttr = array(); 
    46             foreach ($attributes as $name => $values) { 
    47                 if($name === 'objectclass' 
    48                         || $name === 'count' 
    49                         || $name === 'dn' 
    50                         || is_int($name)) { 
    51                     continue; 
    52                 } 
    53  
    54                 unset($values['count']); 
    55                 $entryAttr[$name] = $values; 
    56             } 
    57  
    58             $ldapEntry = new stubLDAPEntry( 
    59                                $attributes['dn'], 
    60                                $attributes['objectclass'], 
    61                                $entryAttr, 
    62                                $attributes['count'] 
    63                              ); 
    64  
    65             $this->entries[$attributes['dn']] = $ldapEntry; 
    66         } 
     48        $this->handle  = $handle; 
     49        $this->result  = $result; 
     50        $entry = ldap_first_entry($handle, $result); 
     51        $this->current = ($entry !== false) ? $entry : null; 
    6752    } 
    6853 
    6954    /** 
    70      * Gets an entry by dn (distinguished name)
     55     * Provides a stubLDAPEntry
    7156     * 
    72      * @param   string         $dn 
     57     * @param   int  $entryId  ldap entry identifier 
    7358     * @return  stubLDAPEntry 
    7459     */ 
    75     public function getEntryByDn($dn
     60    protected function provideLDAPEntry($entryId
    7661    { 
    77         return $this->entries[$dn]; 
     62        $attributes = ldap_get_attributes($this->handle, $entryId); 
     63 
     64        $entryAttr   = array(); 
     65        $objectClass = array(); 
     66        foreach ($attributes as $name => $values) { 
     67            // reject special attributes 
     68            if($name === 'count' || is_int($name)) { 
     69                continue; 
     70            } 
     71 
     72            // get rid of amount 
     73            if(isset($values['count'])) { 
     74                unset($values['count']); 
     75            } 
     76 
     77            // get objec class values 
     78            if($name === 'objectClass') { 
     79                $objectClass = $values; 
     80                continue; 
     81            } 
     82 
     83            // save 'normal' attributes 
     84            $entryAttr[$name] = $values; 
     85        } 
     86 
     87        $dn = ldap_get_dn($this->handle, $entryId); 
     88        if(empty($objectClass)) { 
     89            $objectClass = ldap_read($this->handle, $dn, 'objectClass=*', array('objectclass')); 
     90        } 
     91 
     92        $ldapEntry = new stubLDAPEntry( 
     93                           $dn, 
     94                           $objectClass, 
     95                           $entryAttr, 
     96                           $attributes['count'] 
     97                         ); 
     98 
     99        return $ldapEntry; 
    78100    } 
    79101 
    80102    /** 
    81      * Gets all entries
     103     * Gets the current entry
    82104     * 
    83      * @return  array(stubLDAPEntry) 
     105     * @return  stubLDAPEntry 
    84106     */ 
    85     public function getEntries() 
     107    public function getEntry() 
    86108    { 
    87         return $this->entries
     109        return ($this->current !== null ? $this->provideLDAPEntry($this->current()) : null)
    88110    } 
    89111 
    90112    /** 
    91      * Gets entries amount
     113     * Returns the current entry identifier
    92114     * 
    93      * @return  int 
     115     * @return  ressource 
    94116     */ 
    95     public function getEntriesCount() 
     117    public function current() 
    96118    { 
    97         return $this->entriesCount; 
     119        return $this->current; 
     120    } 
     121 
     122    /** 
     123     * Returns the key of the current element. 
     124     * 
     125     * @return  ressource 
     126     */ 
     127    public function key() 
     128    { 
     129        return $this->current(); 
     130    } 
     131 
     132    /** 
     133     * Moves forward to next element. 
     134     */ 
     135    public function next() 
     136    { 
     137        $this->current = ($result = ldap_next_entry($this->handle, $this->current())) ? $result : null; 
     138    } 
     139 
     140    /** 
     141     * Rewinds the Iterator to the first element. 
     142     */ 
     143    public function rewind() 
     144    { 
     145        $this->current = ldap_first_entry($this->handle, $this->result); 
     146    } 
     147 
     148    /** 
     149     * Checks if there is a current element after calls to rewind() or next(). 
     150     * 
     151     * @return  boolean 
     152     */ 
     153    public function valid() 
     154    { 
     155        return ($this->current() !== null) ? true : false; 
    98156    } 
    99157} 
  • trunk/src/main/php/net/stubbles/peer/ldap/stubLDAPURL.php

    r1559 r1570  
    1010 * @see         RFC 4514  LDAP: String Representation of Distinguished Names  http://tools.ietf.org/html/rfc4514 
    1111 */ 
    12 stubClassLoader::load('net::stubbles::peer::stubURL'); 
     12stubClassLoader::load('net::stubbles::peer::stubURL', 
     13                      'net::stubbles::peer::ldap::stubLDAPConnection' 
     14); 
    1315/** 
    1416 * Entrace point for LDAP usage (via stubLDAPURL::fromString(urlString)).