Changeset 1570
- Timestamp:
- 05/15/08 13:22:25 (2 months ago)
- Files:
-
- trunk/src/main/php/net/stubbles/peer/ldap/stubLDAPConnection.php (modified) (4 diffs)
- trunk/src/main/php/net/stubbles/peer/ldap/stubLDAPEntry.php (modified) (5 diffs)
- trunk/src/main/php/net/stubbles/peer/ldap/stubLDAPSearchResult.php (modified) (2 diffs)
- trunk/src/main/php/net/stubbles/peer/ldap/stubLDAPURL.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/peer/PeerTestSuite.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/peer/ldap/stubLDAPConnectionTestCase.php (added)
- trunk/src/test/php/net/stubbles/peer/ldap/stubLDAPEntryTestCase.php (added)
- trunk/src/test/php/net/stubbles/peer/ldap/stubLDAPSearchResultTestCase.php (added)
- trunk/src/test/php/net/stubbles/peer/ldap/stubLDAPURLTestCase.php (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/peer/ldap/stubLDAPConnection.php
r1559 r1570 24 24 * @var stubLDAPURL 25 25 */ 26 p ublic$ldap;26 protected $ldap; 27 27 /** 28 28 * ldap connection identifier … … 30 30 * @var resource 31 31 */ 32 p ublic$handle;32 protected $handle; 33 33 34 34 /** … … 94 94 public function unbind() 95 95 { 96 ldap_unbind($this->handle); 96 if(gettype($this->handle) !== 'unknown type' ) { 97 ldap_unbind($this->handle); 98 } 97 99 } 98 100 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 99 111 /** 100 112 * Retrieves the LDAP entries. … … 129 141 } 130 142 143 // TODO isParamValid() nötig? 131 144 switch($scope) { 132 145 case 'base': trunk/src/main/php/net/stubbles/peer/ldap/stubLDAPEntry.php
r1559 r1570 18 18 * attributes 19 19 * 20 * @var array (attrName => array(v[, v ...])20 * @var array<string,array<string>> 21 21 */ 22 22 protected $attributes; … … 24 24 * objectClass attribute 25 25 * 26 * @var array (v[, v ...])26 * @var array<string> 27 27 */ 28 28 protected $objectClass; … … 70 70 71 71 /** 72 * Gets the objectClass values. 73 * 74 * @return array<string> 75 */ 76 public function getObjectClassValues() 77 { 78 return $this->objectClass; 79 } 80 81 /** 72 82 * Gets the attributes. 73 83 * 74 * @return array (k,v)84 * @return array<string,array<string>> 75 85 */ 76 86 public function getAttributes() … … 87 97 public function getAttributeValuesByName($name) 88 98 { 89 $name = strtolower($name);90 return $this->attributes[$name];99 // new ArrayAccessor 100 return (isset($this->attributes[$name]) !== false) ? $this->attributes[$name] : null; 91 101 } 92 102 … … 94 104 * Gets the attribute names. 95 105 * 96 * @return array (string)106 * @return array<string> 97 107 */ 98 108 public function getAttributeNames() trunk/src/main/php/net/stubbles/peer/ldap/stubLDAPSearchResult.php
r1559 r1570 14 14 * @subpackage peer_ldap 15 15 */ 16 class stubLDAPSearchResult extends stubBaseObject 16 class stubLDAPSearchResult extends stubBaseObject implements Iterator 17 17 { 18 18 /** 19 * entries19 * ldap connection identifier 20 20 * 21 * @var array(stubLDAPEntry)21 * @var resource 22 22 */ 23 protected $ entries;23 protected $handle; 24 24 /** 25 * entries amount25 * ldap result identifier 26 26 * 27 * @var int27 * @var resource 28 28 */ 29 protected $entriesCount; 29 protected $result; 30 /** 31 * current ldap entryId 32 * 33 * @var ressource 34 */ 35 protected $current; 30 36 31 37 /** 32 38 * constructor (creates stubLDAPEntry objects) 39 * 40 * Unset calls remove the count value for better looping 41 * and because this value is ascertainable later. 33 42 * 34 43 * @param resource $handle ldap connection identifier … … 37 46 public function __construct($handle, $result) 38 47 { 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; 67 52 } 68 53 69 54 /** 70 * Gets an entry by dn (distinguished name).55 * Provides a stubLDAPEntry. 71 56 * 72 * @param string $dn57 * @param int $entryId ldap entry identifier 73 58 * @return stubLDAPEntry 74 59 */ 75 p ublic function getEntryByDn($dn)60 protected function provideLDAPEntry($entryId) 76 61 { 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; 78 100 } 79 101 80 102 /** 81 * Gets all entries.103 * Gets the current entry. 82 104 * 83 * @return array(stubLDAPEntry)105 * @return stubLDAPEntry 84 106 */ 85 public function getEntr ies()107 public function getEntry() 86 108 { 87 return $this->entries;109 return ($this->current !== null ? $this->provideLDAPEntry($this->current()) : null); 88 110 } 89 111 90 112 /** 91 * Gets entries amount.113 * Returns the current entry identifier. 92 114 * 93 * @return int115 * @return ressource 94 116 */ 95 public function getEntriesCount()117 public function current() 96 118 { 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; 98 156 } 99 157 } trunk/src/main/php/net/stubbles/peer/ldap/stubLDAPURL.php
r1559 r1570 10 10 * @see RFC 4514 LDAP: String Representation of Distinguished Names http://tools.ietf.org/html/rfc4514 11 11 */ 12 stubClassLoader::load('net::stubbles::peer::stubURL'); 12 stubClassLoader::load('net::stubbles::peer::stubURL', 13 'net::stubbles::peer::ldap::stubLDAPConnection' 14 ); 13 15 /** 14 16 * Entrace point for LDAP usage (via stubLDAPURL::fromString(urlString)). trunk/src/test/php/net/stubbles/peer/PeerTestSuite.php
r1556 r1570 39 39 // ldap classes 40 40 $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'); 41 44 42 45 return $suite; trunk/src/test/php/net/stubbles/peer/ldap/stubLDAPURLTestCase.php
r1556 r1570 1 1 <?php 2 2 /** 3 * Test for net::stubbles::peer::http::stubLDAPURL.3 * Tests for net::stubbles::peer::http::stubLDAPURL. 4 4 * 5 5 * @author Richard Sternagel <richard.sternagel@1und1.de> … … 9 9 stubClassLoader::load('net::stubbles::peer::ldap::stubLDAPURL'); 10 10 /** 11 * Test for net::stubbles::peer::http::stubLDAPURL.11 * Tests for net::stubbles::peer::http::stubLDAPURL. 12 12 * 13 13 * @package stubbles … … 17 17 { 18 18 /** 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() 22 49 { 23 50 $url = 'ldap://ldap.example.com:389/dc=example,dc=com'; … … 25 52 $this->assertEquals($url, $ldap->get(true)); 26 53 } 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 * 29 70 * @test 30 71 */ … … 35 76 $this->assertEquals($url, $ldap->get(true)); 36 77 } 37 38 /** 78 79 /** 80 * assure url roundtrip with localhost 81 * 39 82 * @test 40 83 */ … … 45 88 $this->assertEquals($url, $ldap->get(true)); 46 89 } 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 * 59 94 * @test 60 95 */ 61 96 public function urlWithoutHostAndPort() 62 { 97 { 63 98 $url = 'ldap:///dc=example,dc=com'; 64 99 $expected = 'ldap://localhost:389/dc=example,dc=com'; … … 66 101 $this->assertEquals($expected, $ldap->get(true)); 67 102 } 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() 73 110 { 74 111 $url = 'ldap://user:password@ldap.example.com:389/dc=example,dc=com'; … … 76 113 $this->assertEquals($url, $ldap->get(true)); 77 114 } 78 79 /** 115 116 /** 117 * assure url roundtrip with params (all possibilitiess) 118 * 80 119 * @test 81 120 */ 82 121 public function urlWithParams() 83 122 { 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 92 133 $url = 'ldap://localhost:389/dc=example,dc=com?cn?sub?(cn=John Doe)'; 93 134 $ldap = stubLDAPURL::fromString($url); … … 97 138 $ldap = stubLDAPURL::fromString($url); 98 139 $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 * 124 173 * @test 125 174 * TODO more complex filter 126 175 */ 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 * 153 210 * @test 154 211 */ … … 164 221 $this->assertEquals($expected, $ldap->get(true)); 165 222 } 166 167 /** 168 * TODO 223 224 /** 225 * assure url roundtrip with ssl port 226 * 227 * @test 169 228 * @expectedException stubMalformedURLException 170 229 */ 171 230 public function urlBadFormat() 172 231 { 173 232 // TODO 233 throw new stubMalformedURLException(); 174 234 } 175 235 }
