/** * Checks if a group exists. * * @param mixed $gid A group ID. * * @return boolean True if the group exists. * @throws Horde_Group_Exception */ public function exists($gid) { try { return $this->_ldap->exists($gid); } catch (Horde_Ldap_Exception $e) { throw new Horde_Group_Exception($e); } }
/** * Renames or moves an entry. * * This method will instantly carry out an update() after the * move, so the entry is moved instantly. * * You can pass an optional Horde_Ldap object. In this case, a * cross directory move will be performed which deletes the entry * in the source (THIS) directory and adds it in the directory * $target_ldap. * * A cross directory move will switch the entry's internal LDAP * reference so updates to the entry will go to the new directory. * * If you want to do a cross directory move, you need to pass an * Horde_Ldap_Entry object, otherwise the attributes will be * empty. * * @param string|Horde_Ldap_Entry $entry An LDAP entry. * @param string $newdn The new location. * @param Horde_Ldap $target_ldap Target directory for cross * server move. * * @throws Horde_Ldap_Exception */ public function move($entry, $newdn, $target_ldap = null) { if (is_string($entry)) { if ($target_ldap && $target_ldap !== $this) { throw new Horde_Ldap_Exception('Unable to perform cross directory move: operation requires a Horde_Ldap_Entry object'); } $entry = $this->getEntry($entry); } if (!$entry instanceof Horde_Ldap_Entry) { throw new Horde_Ldap_Exception('Parameter $entry is expected to be a Horde_Ldap_Entry object! (If DN was passed, conversion failed)'); } if ($target_ldap && !$target_ldap instanceof Horde_Ldap) { throw new Horde_Ldap_Exception('Parameter $target_ldap is expected to be a Horde_Ldap object!'); } if (!$target_ldap || $target_ldap === $this) { /* Local move. */ $entry->dn($newdn); $entry->setLDAP($this); $entry->update(); return; } /* Cross directory move. */ if ($target_ldap->exists($newdn)) { throw new Horde_Ldap_Exception('Unable to perform cross directory move: entry does exist in target directory'); } $entry->dn($newdn); try { $target_ldap->add($entry); } catch (Exception $e) { throw new Horde_Ldap_Exception('Unable to perform cross directory move: ' . $e->getMessage() . ' in target directory'); } try { $this->delete($entry->currentDN()); } catch (Exception $e) { try { $add_error_string = ''; /* Undo add. */ $target_ldap->delete($entry); } catch (Exception $e) { $add_error_string = ' Additionally, the deletion (undo add) of $entry in target directory failed.'; } throw new Horde_Ldap_Exception('Unable to perform cross directory move: ' . $e->getMessage() . ' in source directory.' . $add_error_string); } $entry->setLDAP($target_ldap); }
/** * Tests SPL iterator. */ public function testSPLIterator() { $ldap = new Horde_Ldap(self::$ldapcfg['server']); // Some testdata, so we have some entries to search for. $base = self::$ldapcfg['server']['basedn']; $ou1 = Horde_Ldap_Entry::createFresh('ou=Horde_Ldap_Test_search1,' . $base, array('objectClass' => array('top', 'organizationalUnit'), 'ou' => 'Horde_Ldap_Test_search1')); $ou2 = Horde_Ldap_Entry::createFresh('ou=Horde_Ldap_Test_search2,' . $base, array('objectClass' => array('top', 'organizationalUnit'), 'ou' => 'Horde_Ldap_Test_search2')); $ldap->add($ou1); $this->assertTrue($ldap->exists($ou1->dn())); $ldap->add($ou2); $this->assertTrue($ldap->exists($ou2->dn())); /* Search and test each method. */ $search = $ldap->search(null, '(ou=Horde_Ldap*)'); $this->assertInstanceOf('Horde_Ldap_Search', $search); $this->assertEquals(2, $search->count()); // current() is supposed to return first valid element. $e1 = $search->current(); $this->assertInstanceOf('Horde_Ldap_Entry', $e1); $this->assertEquals($e1->dn(), $search->key()); $this->assertTrue($search->valid()); // Shift to next entry. $search->next(); $e2 = $search->current(); $this->assertInstanceOf('Horde_Ldap_Entry', $e2); $this->assertEquals($e2->dn(), $search->key()); $this->assertTrue($search->valid()); // Shift to non existent third entry. $search->next(); $this->assertFalse($search->current()); $this->assertFalse($search->key()); $this->assertFalse($search->valid()); // Rewind and test, which should return the first entry a second time. $search->rewind(); $e1_1 = $search->current(); $this->assertInstanceOf('Horde_Ldap_Entry', $e1_1); $this->assertEquals($e1_1->dn(), $search->key()); $this->assertTrue($search->valid()); $this->assertEquals($e1->dn(), $e1_1->dn()); // Don't rewind but call current, should return first entry again. $e1_2 = $search->current(); $this->assertInstanceOf('Horde_Ldap_Entry', $e1_2); $this->assertEquals($e1_2->dn(), $search->key()); $this->assertTrue($search->valid()); $this->assertEquals($e1->dn(), $e1_2->dn()); // Rewind again and test, which should return the first entry a third // time. $search->rewind(); $e1_3 = $search->current(); $this->assertInstanceOf('Horde_Ldap_Entry', $e1_3); $this->assertEquals($e1_3->dn(), $search->key()); $this->assertTrue($search->valid()); $this->assertEquals($e1->dn(), $e1_3->dn()); /* Try methods on empty search result. */ $search = $ldap->search(null, '(ou=Horde_LdapTest_NotExistentEntry)'); $this->assertInstanceOf('Horde_Ldap_Search', $search); $this->assertEquals(0, $search->count()); $this->assertFalse($search->current()); $this->assertFalse($search->key()); $this->assertFalse($search->valid()); $search->next(); $this->assertFalse($search->current()); $this->assertFalse($search->key()); $this->assertFalse($search->valid()); /* Search and simple iterate through the test entries. Then, rewind * and do it again several times. */ $search2 = $ldap->search(null, '(ou=Horde_Ldap*)'); $this->assertInstanceOf('Horde_Ldap_Search', $search2); $this->assertEquals(2, $search2->count()); for ($i = 0; $i <= 5; $i++) { $counter = 0; foreach ($search2 as $dn => $entry) { $counter++; // Check on type. $this->assertInstanceOf('Horde_Ldap_Entry', $entry); // Check on key. $this->assertThat(strlen($dn), $this->greaterThan(1)); $this->assertEquals($dn, $entry->dn()); } $this->assertEquals($search2->count(), $counter, "Failed at loop {$i}"); // Revert to start. $search2->rewind(); } }
/** * Test copy(). */ public function testCopy() { $ldap = new Horde_Ldap(self::$ldapcfg['server']); // Some testdata. $base = self::$ldapcfg['server']['basedn']; $ou1 = Horde_Ldap_Entry::createFresh('ou=Horde_Ldap_Test_pool,' . $base, array('objectClass' => array('top', 'organizationalUnit'), 'ou' => 'Horde_Ldap_Test_copy')); $ou2 = Horde_Ldap_Entry::createFresh('ou=Horde_Ldap_Test_tgt,' . $base, array('objectClass' => array('top', 'organizationalUnit'), 'ou' => 'Horde_Ldap_Test_copy')); $ldap->add($ou1); $this->assertTrue($ldap->exists($ou1->dn())); $ldap->add($ou2); $this->assertTrue($ldap->exists($ou2->dn())); $entry = Horde_Ldap_Entry::createFresh('l=cptest,' . $ou1->dn(), array('objectClass' => array('top', 'locality'), 'l' => 'cptest')); $ldap->add($entry); $ldap->exists($entry->dn()); // Copy over the entry to another tree with rename. $entrycp = $ldap->copy($entry, 'l=test_copied,' . $ou2->dn()); $this->assertInstanceOf('Horde_Ldap_Entry', $entrycp); $this->assertNotEquals($entry->dn(), $entrycp->dn()); $this->assertTrue($ldap->exists($entrycp->dn())); // Copy same again (fails, entry exists). try { $entrycp_f = $ldap->copy($entry, 'l=test_copied,' . $ou2->dn()); $this->fail('Horde_Ldap_Exception expected.'); } catch (Horde_Ldap_Exception $e) { } // Use only DNs to copy (fails). try { $entrycp = $ldap->copy($entry->dn(), 'l=test_copied2,' . $ou2->dn()); $this->fail('Horde_Ldap_Exception expected.'); } catch (Horde_Ldap_Exception $e) { } }