From 4235a4a5a9c509f8bd14a155ba21962c9773b869 Mon Sep 17 00:00:00 2001 From: Gunnar Wrobel Date: Sun, 29 Mar 2009 21:08:46 +0000 Subject: [PATCH] Some fixes for the LDAP handling of Kolab_Server. It was still broken in some places because of the switch to Net_LDAP2. --- .../Kolab_Server/lib/Horde/Kolab/Server/ldap.php | 28 +++++++--------- .../Kolab_Server/lib/Horde/Kolab/Server/test.php | 38 ++++++++++++++++++++++ 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/framework/Kolab_Server/lib/Horde/Kolab/Server/ldap.php b/framework/Kolab_Server/lib/Horde/Kolab/Server/ldap.php index ea9f7605b..d00982965 100644 --- a/framework/Kolab_Server/lib/Horde/Kolab/Server/ldap.php +++ b/framework/Kolab_Server/lib/Horde/Kolab/Server/ldap.php @@ -63,7 +63,7 @@ class Horde_Kolab_Server_ldap extends Horde_Kolab_Server $base_config = array('host' => 'localhost', 'port' => 389, 'version' => 3, - 'starttls' => true, + 'starttls' => false, 'uid' => '', 'pass' => '', 'basedn' => '', @@ -79,7 +79,10 @@ class Horde_Kolab_Server_ldap extends Horde_Kolab_Server $config['binddn'] = $config['uid']; $config['bindpw'] = $config['pass']; - $this->_ldap = new Net_LDAP2($config); + $this->_ldap = Net_LDAP2::connect($config); + if (is_a($this->_ldap, 'PEAR_Error')) { + throw new Horde_Kolab_Server_Exception($this->_ldap); + } parent::__construct($params); } @@ -98,20 +101,16 @@ class Horde_Kolab_Server_ldap extends Horde_Kolab_Server */ public function read($dn, $attrs = null) { - $params = array('scope' => 'one'); + $params = array('scope' => 'base'); if (!empty($attrs)) { - $params['attributes'] = $attr; + $params['attributes'] = $attrs; } - $result = $this->search(null, $params, $dn); - if (empty($result) || !($result instanceOf Net_LDAP2_Search)) { - throw new Horde_Kolab_Server_Exception(_("Empty or invalid result!")); + $data = $this->search(null, $params, $dn); + if (empty($data)) { + throw new Horde_Kolab_Server_Exception(_("Empty result!")); } - $data = $result->as_struct(); - if (is_a($data, 'PEAR_Error')) { - throw new Horde_Kolab_Server_Exception($data); - } if (!isset($data[$dn])) { throw new Horde_Kolab_Server_Exception(sprintf(_("No result found for %s"), $dn)); @@ -418,7 +417,7 @@ class Horde_Kolab_Server_ldap extends Horde_Kolab_Server if (is_a($result, 'PEAR_Error')) { throw new Horde_Kolab_Server_Exception($result->getMessage()); } - return $result; + return $result->as_struct(); } /** @@ -533,10 +532,7 @@ class Horde_Kolab_Server_ldap extends Horde_Kolab_Server if (empty($result)) { return false; } - $dns = array(); - foreach ($result as $entry) { - $dns[] = $entry['dn']; - } + $dns = array_keys($result); switch ($restrict) { case self::RESULT_STRICT: diff --git a/framework/Kolab_Server/lib/Horde/Kolab/Server/test.php b/framework/Kolab_Server/lib/Horde/Kolab/Server/test.php index 86cb55f9f..eabf557cd 100644 --- a/framework/Kolab_Server/lib/Horde/Kolab/Server/test.php +++ b/framework/Kolab_Server/lib/Horde/Kolab/Server/test.php @@ -664,4 +664,42 @@ class Horde_Kolab_Server_test extends Horde_Kolab_Server_ldap return $this->_error; } + /** + * Identify the DN of the first result entry. + * + * @todo Check if this could be reintegrated with the code in the LDAP handler + * again. + * + * @param array $result The LDAP search result. + * @param int $restrict A Horde_Kolab_Server::RESULT_* result restriction. + * + * @return boolean|string|array The DN(s) or false if there was no result. + * + * @throws Horde_Kolab_Server_Exception If the number of results did not + * meet the expectations. + */ + protected function dnFromResult($result, + $restrict = Horde_Kolab_Server::RESULT_SINGLE) + { + if (empty($result)) { + return false; + } + $dns = array(); + foreach ($result as $entry) { + $dns[] = $entry['dn']; + } + + switch ($restrict) { + case self::RESULT_STRICT: + if (count($dns) > 1) { + throw new Horde_Kolab_Server_Exception(sprintf(_("Found %s results when expecting only one!"), + $count)); + } + case self::RESULT_SINGLE: + return $dns[0]; + case self::RESULT_MANY: + return $dns; + } + } + } -- 2.11.0