Changeset 1570

Show
Ignore:
Timestamp:
05/15/08 13:22:25 (2 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)). 
  • trunk/src/test/php/net/stubbles/peer/PeerTestSuite.php

    r1556 r1570  
    3939        // ldap classes 
    4040        $suite->addTestFile($dir . '/ldap/stubLDAPURLTestCase.php'); 
     41        $suite->addTestFile($dir . '/ldap/stubLDAPConnectionTestCase.php'); 
     42        $suite->addTestFile($dir . '/ldap/stubLDAPSearchResultTestCase.php'); 
     43        $suite->addTestFile($dir . '/ldap/stubLDAPEntryTestCase.php'); 
    4144 
    4245        return $suite; 
  • trunk/src/test/php/net/stubbles/peer/ldap/stubLDAPURLTestCase.php

    r1556 r1570  
    11<?php 
    22/** 
    3  * Test for net::stubbles::peer::http::stubLDAPURL. 
     3 * Tests for net::stubbles::peer::http::stubLDAPURL. 
    44 * 
    55 * @author      Richard Sternagel <richard.sternagel@1und1.de> 
     
    99stubClassLoader::load('net::stubbles::peer::ldap::stubLDAPURL'); 
    1010/** 
    11  * Test for net::stubbles::peer::http::stubLDAPURL. 
     11 * Tests for net::stubbles::peer::http::stubLDAPURL. 
    1212 * 
    1313 * @package     stubbles 
     
    1717{ 
    1818    /** 
    19      * @test 
    20      */ 
    21     public function url() 
     19     * set up test envioronment 
     20     */ 
     21    public function setUp() 
     22    { 
     23        if (extension_loaded('ldap') === false) { 
     24            $this->markTestSkipped('The LDAP extension is not available.'); 
     25        } 
     26    } 
     27     
     28    /** 
     29     * assure url roundtrip without port 
     30     * 
     31     * @test 
     32     */ 
     33    public function urlWithoutPort() 
     34    { 
     35        $url = 'ldap://ldap.example.com/dc=example,dc=com'; 
     36        $ldap = stubLDAPURL::fromString($url); 
     37        $this->assertEquals($url, $ldap->get()); 
     38 
     39        $expected = 'ldap://ldap.example.com:389/dc=example,dc=com'; 
     40        $this->assertEquals($expected, $ldap->get(true)); 
     41    } 
     42 
     43    /** 
     44     * assure url roundtrip with standard port 
     45     *  
     46     * @test 
     47     */ 
     48    public function urlWithPort() 
    2249    { 
    2350        $url = 'ldap://ldap.example.com:389/dc=example,dc=com'; 
     
    2552        $this->assertEquals($url, $ldap->get(true)); 
    2653    } 
    27      
    28     /** 
     54 
     55    /** 
     56     * assure url roundtrip with arbitrary port 
     57     *  
     58     * @test 
     59     */ 
     60    public function urlWithArbitraryPort() 
     61    { 
     62        $url = 'ldap://ldap.example.com:123/dc=example,dc=com'; 
     63        $ldap = stubLDAPURL::fromString($url); 
     64        $this->assertEquals($url, $ldap->get(true)); 
     65    } 
     66 
     67    /** 
     68     * assure url roundtrip with host ip 
     69     *  
    2970     * @test 
    3071     */ 
     
    3576        $this->assertEquals($url, $ldap->get(true)); 
    3677    } 
    37      
    38     /** 
     78 
     79    /** 
     80     * assure url roundtrip with localhost 
     81     *  
    3982     * @test 
    4083     */ 
     
    4588        $this->assertEquals($url, $ldap->get(true)); 
    4689    } 
    47      
    48     /** 
    49      * @test 
    50      */ 
    51     public function urlWithoutPort() 
    52     { 
    53         $url = 'ldap://ldap.example.com/dc=example,dc=com'; 
    54         $ldap = stubLDAPURL::fromString($url); 
    55         $this->assertEquals($url, $ldap->get(false)); 
    56     } 
    57      
    58     /** 
     90 
     91    /** 
     92     * assure url roundtrip without host and port 
     93     *  
    5994     * @test 
    6095     */ 
    6196    public function urlWithoutHostAndPort() 
    62     {    
     97    { 
    6398        $url      = 'ldap:///dc=example,dc=com'; 
    6499        $expected = 'ldap://localhost:389/dc=example,dc=com'; 
     
    66101        $this->assertEquals($expected, $ldap->get(true)); 
    67102    } 
    68      
    69     /** 
    70      * @test 
    71      */ 
    72     public function urlWithAuth()  
     103 
     104    /** 
     105     * assure url roundtrip without authentication data 
     106     *  
     107     * @test 
     108     */ 
     109    public function urlWithAuth() 
    73110    { 
    74111        $url = 'ldap://user:password@ldap.example.com:389/dc=example,dc=com'; 
     
    76113        $this->assertEquals($url, $ldap->get(true)); 
    77114    } 
    78      
    79     /** 
     115 
     116    /** 
     117     * assure url roundtrip with params (all possibilitiess) 
     118     *  
    80119     * @test 
    81120     */ 
    82121    public function urlWithParams() 
    83122    { 
    84         $url = 'ldap://localhost:389/dc=example,dc=com?cn'; 
    85         $ldap = stubLDAPURL::fromString($url); 
    86         $this->assertEquals($url, $ldap->get(true)); 
    87                  
    88         $url = 'ldap://localhost:389/dc=example,dc=com?cn?sub'; 
    89         $ldap = stubLDAPURL::fromString($url); 
    90         $this->assertEquals($url, $ldap->get(true)); 
    91          
     123        $url      = 'ldap://localhost:389/dc=example,dc=com?cn'; 
     124        $expected = 'ldap://localhost:389/dc=example,dc=com?cn?base?objectClass=*'; 
     125        $ldap = stubLDAPURL::fromString($url); 
     126        $this->assertEquals($expected, $ldap->get(true)); 
     127 
     128        $url      = 'ldap://localhost:389/dc=example,dc=com?cn?sub'; 
     129        $expected = 'ldap://localhost:389/dc=example,dc=com?cn?sub?objectClass=*'; 
     130        $ldap = stubLDAPURL::fromString($url); 
     131        $this->assertEquals($expected, $ldap->get(true)); 
     132 
    92133        $url = 'ldap://localhost:389/dc=example,dc=com?cn?sub?(cn=John Doe)'; 
    93134        $ldap = stubLDAPURL::fromString($url); 
     
    97138        $ldap = stubLDAPURL::fromString($url); 
    98139        $this->assertEquals($url, $ldap->get(true)); 
    99          
    100         $url = 'ldap://localhost:389/dc=example,dc=com?cn??(cn=John Doe)'; 
    101         $ldap = stubLDAPURL::fromString($url); 
    102         $this->assertEquals($url, $ldap->get(true)); 
    103          
    104         $url = 'ldap://localhost:389/dc=example,dc=com???(cn=John Doe)'; 
    105         $ldap = stubLDAPURL::fromString($url); 
    106         $this->assertEquals($url, $ldap->get(true)); 
    107     } 
    108      
    109     /** 
    110      * @test 
    111      */ 
    112     public function urlWithParamsAttributesValues()  
    113     { 
    114         $url = 'ldap://localhost:389/dc=example,dc=com?cn,uid'; 
    115         $ldap = stubLDAPURL::fromString($url); 
    116         $this->assertEquals($url, $ldap->get(true)); 
    117          
    118         $url = 'ldap://localhost:389/dc=example,dc=com?cn,uid,sn'; 
    119         $ldap = stubLDAPURL::fromString($url); 
    120         $this->assertEquals($url, $ldap->get(true)); 
    121     } 
    122      
    123     /** 
     140 
     141        $url      = 'ldap://localhost:389/dc=example,dc=com?cn??(cn=John Doe)'; 
     142        $expected = 'ldap://localhost:389/dc=example,dc=com?cn?base?(cn=John Doe)'; 
     143        $ldap = stubLDAPURL::fromString($url); 
     144        $this->assertEquals($expected, $ldap->get(true)); 
     145 
     146        $url      = 'ldap://localhost:389/dc=example,dc=com???(cn=John Doe)'; 
     147        $expected = 'ldap://localhost:389/dc=example,dc=com??base?(cn=John Doe)'; 
     148        $ldap = stubLDAPURL::fromString($url); 
     149        $this->assertEquals($expected, $ldap->get(true)); 
     150    } 
     151 
     152    /** 
     153     * assure url roundtrip with params (attributes) 
     154     *  
     155     * @test 
     156     */ 
     157    public function urlWithParamsAttributesValues() 
     158    { 
     159        $url      = 'ldap://localhost:389/dc=example,dc=com?cn'; 
     160        $expected = 'ldap://localhost:389/dc=example,dc=com?cn?base?objectClass=*'; 
     161        $ldap = stubLDAPURL::fromString($url); 
     162        $this->assertEquals($expected, $ldap->get(true)); 
     163 
     164        $url      = 'ldap://localhost:389/dc=example,dc=com?cn,uid'; 
     165        $expected = 'ldap://localhost:389/dc=example,dc=com?cn,uid?base?objectClass=*'; 
     166        $ldap = stubLDAPURL::fromString($url); 
     167        $this->assertEquals($expected, $ldap->get(true)); 
     168    } 
     169 
     170    /** 
     171     * assure url roundtrip with params (filter) 
     172     *  
    124173     * @test 
    125174     * TODO more complex filter 
    126175     */ 
    127     public function urlWithParamsFilterValues()  
    128     { 
    129         $url = 'ldap://ldap.example.com:389/dc=example,dc=com???(cn=A*)'; 
    130         $ldap = stubLDAPURL::fromString($url); 
    131         $this->assertEquals($url, $ldap->get(true)); 
    132     } 
    133      
    134     /** 
    135      * @test 
    136      */ 
    137     public function urlWithParamsScopeValues()  
    138     { 
    139         $url = 'ldap://ldap.example.com:389/dc=example,dc=com??base'; 
    140         $ldap = stubLDAPURL::fromString($url); 
    141         $this->assertEquals($url, $ldap->get(true)); 
    142          
    143         $url = 'ldap://ldap.example.com:389/dc=example,dc=com??one'; 
    144         $ldap = stubLDAPURL::fromString($url); 
    145         $this->assertEquals($url, $ldap->get(true)); 
    146          
    147         $url = 'ldap://ldap.example.com:389/dc=example,dc=com??sub'; 
    148         $ldap = stubLDAPURL::fromString($url); 
    149         $this->assertEquals($url, $ldap->get(true)); 
    150     } 
    151      
    152     /** 
     176    public function urlWithParamsFilterValues() 
     177    { 
     178        $url      = 'ldap://ldap.example.com:389/dc=example,dc=com???(cn=A*)'; 
     179        $expected = 'ldap://ldap.example.com:389/dc=example,dc=com??base?(cn=A*)'; 
     180        $ldap = stubLDAPURL::fromString($url); 
     181        $this->assertEquals($expected, $ldap->get(true)); 
     182    } 
     183 
     184    /** 
     185     * assure url roundtrip with params (scope) 
     186     *  
     187     * @test 
     188     */ 
     189    public function urlWithParamsScopeValues() 
     190    { 
     191        $url      = 'ldap://ldap.example.com:389/dc=example,dc=com??base'; 
     192        $expected = 'ldap://ldap.example.com:389/dc=example,dc=com??base?objectClass=*'; 
     193        $ldap = stubLDAPURL::fromString($url); 
     194        $this->assertEquals($expected, $ldap->get(true)); 
     195 
     196        $url      = 'ldap://ldap.example.com:389/dc=example,dc=com??one'; 
     197        $expected = 'ldap://ldap.example.com:389/dc=example,dc=com??one?objectClass=*'; 
     198        $ldap = stubLDAPURL::fromString($url); 
     199        $this->assertEquals($expected, $ldap->get(true)); 
     200 
     201        $url      = 'ldap://ldap.example.com:389/dc=example,dc=com??sub'; 
     202        $expected = 'ldap://ldap.example.com:389/dc=example,dc=com??sub?objectClass=*'; 
     203        $ldap = stubLDAPURL::fromString($url); 
     204        $this->assertEquals($expected, $ldap->get(true)); 
     205    } 
     206 
     207    /** 
     208     * assure url roundtrip with ssl port 
     209     *  
    153210     * @test 
    154211     */ 
     
    164221        $this->assertEquals($expected, $ldap->get(true)); 
    165222    } 
    166      
    167     /** 
    168      * TODO 
     223 
     224    /** 
     225     * assure url roundtrip with ssl port 
     226     *  
     227     * @test 
    169228     * @expectedException  stubMalformedURLException 
    170229     */ 
    171230    public function urlBadFormat() 
    172231    { 
    173          
     232        // TODO 
     233        throw new stubMalformedURLException(); 
    174234    } 
    175235}