From 47f535f7ae9f85f2805ba643419e46679380ed14 Mon Sep 17 00:00:00 2001
From: Gunnar Wrobel
Date: Wed, 8 Apr 2009 22:39:08 +0200
Subject: [PATCH] Allow to use different structure types for a server. This
should allow for more flexibility with different LDAP tree structures.
---
framework/Kolab_Server/lib/Horde/Kolab/Server.php | 90 ++++++++++------
.../Kolab_Server/lib/Horde/Kolab/Server/Ldap.php | 47 --------
.../lib/Horde/Kolab/Server/Structure.php | 119 +++++++++++++++++++++
.../Horde/Kolab/Server/{ => Structure}/Kolab.php | 58 +++++-----
.../lib/Horde/Kolab/Server/Structure/Ldap.php | 86 +++++++++++++++
.../Kolab_Server/lib/Horde/Kolab/Server/Test.php | 2 +-
framework/Kolab_Server/package.xml | 10 +-
.../test/Horde/Kolab/Server/ServerTest.php | 12 ++-
8 files changed, 314 insertions(+), 110 deletions(-)
create mode 100644 framework/Kolab_Server/lib/Horde/Kolab/Server/Structure.php
rename framework/Kolab_Server/lib/Horde/Kolab/Server/{ => Structure}/Kolab.php (69%)
create mode 100644 framework/Kolab_Server/lib/Horde/Kolab/Server/Structure/Ldap.php
diff --git a/framework/Kolab_Server/lib/Horde/Kolab/Server.php b/framework/Kolab_Server/lib/Horde/Kolab/Server.php
index 7ab4ebe4d..2bff58091 100644
--- a/framework/Kolab_Server/lib/Horde/Kolab/Server.php
+++ b/framework/Kolab_Server/lib/Horde/Kolab/Server.php
@@ -58,6 +58,13 @@ abstract class Horde_Kolab_Server
protected $searches;
/**
+ * The structure handler for this server.
+ *
+ * @var Horde_Kolab_Server_Structure
+ */
+ public $structure;
+
+ /**
* Construct a new Horde_Kolab_Server object.
*
* @param array $params Parameter array.
@@ -69,6 +76,15 @@ abstract class Horde_Kolab_Server
$this->uid = $params['uid'];
}
+ $structure = isset($params['structure']['driver'])
+ ? $params['structure']['driver'] : 'kolab';
+ $structure_params = isset($params['structure']['params'])
+ ? $params['structure']['params'] : array();
+
+ $this->structure = &Horde_Kolab_Server_Structure::factory($structure,
+ $this,
+ $structure_params);
+
// Initialize the search operations supported by this server.
$this->searches = $this->getSearchOperations();
}
@@ -328,16 +344,44 @@ abstract class Horde_Kolab_Server
}
/**
- * Returns the set of objects supported by this server type.
+ * Returns the set of objects supported by this server.
*
- * @return array An array of supported search operations.
+ * @return array An array of supported objects.
+ */
+ public function getSupportedObjects()
+ {
+ return $this->structure->getSupportedObjects();
+ }
+
+ /**
+ * Determine the type of an object by its tree position and other
+ * parameters.
+ *
+ * @param string $uid The UID of the object to examine.
+ *
+ * @return string The class name of the corresponding object type.
+ *
+ * @throws Horde_Kolab_Server_Exception If the object type is unknown.
+ */
+ public function determineType($uid)
+ {
+ return $this->structure->determineType($uid);
+ }
+
+ /**
+ * Generates a UID for the given information.
+ *
+ * @param string $type The class name of the object to create.
+ * @param string $id The id of the object.
+ * @param array $info Any additional information about the object to create.
+ *
+ * @return string The UID.
+ *
+ * @throws Horde_Kolab_Server_Exception
*/
- static public function getSupportedObjects()
+ protected function generateServerUid($type, $id, $info)
{
- $objects = array(
- 'Horde_Kolab_Server_Object',
- );
- return $objects;
+ return $this->structure->generateServerUid($type, $id, $info);
}
/**
@@ -528,9 +572,11 @@ abstract class Horde_Kolab_Server
{
$server_searches = array();
foreach ($this->getSupportedObjects() as $sobj) {
- $searches = call_user_func(array($sobj, 'getSearchOperations'));
- foreach ($searches as $search) {
- $server_searches[$search] = array('class' => $sobj);
+ if (in_array('getSearchOperations', get_class_methods($sobj))) {
+ $searches = call_user_func(array($sobj, 'getSearchOperations'));
+ foreach ($searches as $search) {
+ $server_searches[$search] = array('class' => $sobj);
+ }
}
}
return $server_searches;
@@ -586,17 +632,6 @@ abstract class Horde_Kolab_Server
abstract public function save($uid, $data, $exists = false);
/**
- * Determine the type of a Kolab object.
- *
- * @param string $uid The UID of the object to examine.
- *
- * @return string The corresponding Kolab object type.
- *
- * @throws Horde_Kolab_Server_Exception
- */
- abstract protected function determineType($uid);
-
- /**
* List all objects of a specific type
*
* @param string $type The type of the objects to be listed
@@ -609,19 +644,6 @@ abstract class Horde_Kolab_Server
abstract public function listObjects($type, $params = null);
/**
- * Generates a UID for the given information.
- *
- * @param string $type The type of the object to create.
- * @param string $id The id of the object.
- * @param array $info Any additional information about the object to create.
- *
- * @return string The UID.
- *
- * @throws Horde_Kolab_Server_Exception
- */
- abstract protected function generateServerUid($type, $id, $info);
-
- /**
* Return the root of the UID values on this server.
*
* @return string The base UID on this server (base DN on ldap).
diff --git a/framework/Kolab_Server/lib/Horde/Kolab/Server/Ldap.php b/framework/Kolab_Server/lib/Horde/Kolab/Server/Ldap.php
index f02934a73..e81407557 100644
--- a/framework/Kolab_Server/lib/Horde/Kolab/Server/Ldap.php
+++ b/framework/Kolab_Server/lib/Horde/Kolab/Server/Ldap.php
@@ -485,51 +485,4 @@ class Horde_Kolab_Server_Ldap extends Horde_Kolab_Server
return $clause;
}
-
- /**
- * Determine the type of a Kolab object.
- *
- * @param string $uid The UID of the object to examine.
- *
- * @return int The corresponding Kolab object type.
- *
- * @throws Horde_Kolab_Server_Exception If the object type is unknown.
- */
- public function determineType($uid)
- {
- $ocs = $this->getObjectClasses($uid);
- $ocs = array_reverse($ocs);
- foreach ($ocs as $oc) {
- try {
- $class_name = 'Horde_Kolab_Server_Object_' . ucfirst(strtolower($oc));
- Horde_Kolab_Server_Object::loadClass($class_name);
- return $class_name;
- } catch (Horde_Kolab_Server_Exception $e) {
- }
- }
- if ($oc == 'top') {
- return 'Horde_Kolab_Server_Object';
- }
- throw new Horde_Kolab_Server_Exception(sprintf(_("Unkown object type for UID %s."),
- $uid));
- }
-
- /**
- * Generates a UID for the given information.
- *
- * @param string $type The type of the object to create.
- * @param string $id The id of the object.
- * @param array $info Any additional information about the object to create.
- *
- * @return string The DN.
- *
- * @throws Horde_Kolab_Server_Exception If the given type is unknown.
- */
- public function generateServerUid($type, $id, $info)
- {
- switch ($type) {
- default:
- return sprintf('%s,%s', $id, $this->getBaseUid());
- }
- }
}
diff --git a/framework/Kolab_Server/lib/Horde/Kolab/Server/Structure.php b/framework/Kolab_Server/lib/Horde/Kolab/Server/Structure.php
new file mode 100644
index 000000000..54b40bac0
--- /dev/null
+++ b/framework/Kolab_Server/lib/Horde/Kolab/Server/Structure.php
@@ -0,0 +1,119 @@
+
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=Kolab_Server
+ */
+
+/**
+ * An abstract class definiing methods to deal with an object tree structure.
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Kolab
+ * @package Kolab_Server
+ * @author Gunnar Wrobel
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=Kolab_Server
+ */
+abstract class Horde_Kolab_Server_Structure
+{
+ /**
+ * A link to the server handler.
+ *
+ * @var Horde_Kolab_Server
+ */
+ protected $server;
+
+ /**
+ * Structure parameters.
+ *
+ * @var array
+ */
+ protected $params = array();
+
+ /**
+ * Construct a new Horde_Kolab_Server_Structure object.
+ *
+ * @param Horde_Kolab_Server &$server A link to the server handler.
+ * @param array $params Parameter array.
+ */
+ public function __construct(&$server, $params = array())
+ {
+ $this->server = &$server;
+ $this->params = $params;
+ }
+
+ /**
+ * Attempts to return a concrete Horde_Kolab_Server_Structure instance based
+ * on $driver.
+ *
+ * @param mixed $driver The type of concrete Horde_Kolab_Server
+ * subclass to return.
+ * @param Horde_Kolab_Server &$server A link to the server handler .
+ * @param array $params A hash containing any additional
+ * configuration or connection
+ * parameters a subclass might need.
+ *
+ * @return Horde_Kolab_Server_Structure The newly created concrete
+ * Horde_Kolab_Server_Structure instance.
+ *
+ * @throws Horde_Kolab_Server_Exception If the requested Horde_Kolab_Server_Structure
+ * subclass could not be found.
+ */
+ static public function &factory($driver, &$server, $params = array())
+ {
+ if (class_exists($driver)) {
+ $class = $driver;
+ } else {
+ $class = 'Horde_Kolab_Server_Structure_' . ucfirst(basename($driver));
+ if (!class_exists($class)) {
+ throw new Horde_Kolab_Server_Exception(
+ 'Structure type definition "' . $class . '" missing.');
+ }
+ }
+ $structure = new $class($server, $params);
+ return $structure;
+ }
+
+ /**
+ * Returns the set of objects supported by this structure.
+ *
+ * @return array An array of supported objects.
+ */
+ abstract public function getSupportedObjects();
+
+ /**
+ * Determine the type of an object by its tree position and other
+ * parameters.
+ *
+ * @param string $uid The UID of the object to examine.
+ *
+ * @return string The class name of the corresponding object type.
+ *
+ * @throws Horde_Kolab_Server_Exception If the object type is unknown.
+ */
+ abstract public function determineType($uid);
+
+ /**
+ * Generates a UID for the given information.
+ *
+ * @param string $type The class name of the object to create.
+ * @param string $id The id of the object.
+ * @param array $info Any additional information about the object to create.
+ *
+ * @return string The UID.
+ *
+ * @throws Horde_Kolab_Server_Exception If the given type is unknown.
+ */
+ abstract public function generateServerUid($type, $id, $info);
+}
diff --git a/framework/Kolab_Server/lib/Horde/Kolab/Server/Kolab.php b/framework/Kolab_Server/lib/Horde/Kolab/Server/Structure/Kolab.php
similarity index 69%
rename from framework/Kolab_Server/lib/Horde/Kolab/Server/Kolab.php
rename to framework/Kolab_Server/lib/Horde/Kolab/Server/Structure/Kolab.php
index 9445ae9fe..cffd82814 100644
--- a/framework/Kolab_Server/lib/Horde/Kolab/Server/Kolab.php
+++ b/framework/Kolab_Server/lib/Horde/Kolab/Server/Structure/Kolab.php
@@ -1,6 +1,6 @@
getObjectClasses($dn);
+ $oc = $this->server->getObjectClasses($uid);
// Not a user type?
if (!in_array('kolabinetorgperson', $oc)) {
// Is it a group?
@@ -66,25 +74,25 @@ class Horde_Kolab_Server_Kolab extends Horde_Kolab_Server_Ldap
if (in_array('kolabsharedfolder', $oc)) {
return 'Horde_Kolab_Server_Object_Kolabsharedfolder';
}
- return parent::determineType($dn);
+ return parent::determineType($uid);
}
- $groups = $this->getGroups($dn);
+ $groups = $this->server->getGroups($uid);
if (!empty($groups)) {
- if (in_array('cn=admin,cn=internal,' . $this->getBaseUid(), $groups)) {
+ if (in_array('cn=admin,cn=internal,' . $this->server->getBaseUid(), $groups)) {
return 'Horde_Kolab_Server_Object_Kolab_Administrator';
}
- if (in_array('cn=maintainer,cn=internal,' . $this->getBaseUid(),
+ if (in_array('cn=maintainer,cn=internal,' . $this->server->getBaseUid(),
$groups)) {
return 'Horde_Kolab_Server_Object_Kolab_Maintainer';
}
- if (in_array('cn=domain-maintainer,cn=internal,' . $this->getBaseUid(),
+ if (in_array('cn=domain-maintainer,cn=internal,' . $this->server->getBaseUid(),
$groups)) {
return 'Horde_Kolab_Server_Object_Kolab_Domainmaintainer';
}
}
- if (strpos($dn, 'cn=external') !== false) {
+ if (strpos($uid, 'cn=external') !== false) {
return 'Horde_Kolab_Server_Object_Kolab_Address';
}
@@ -94,11 +102,11 @@ class Horde_Kolab_Server_Kolab extends Horde_Kolab_Server_Ldap
/**
* Generates a UID for the given information.
*
- * @param string $type The type of the object to create.
+ * @param string $type The class name of the object to create.
* @param string $id The id of the object.
* @param array $info Any additional information about the object to create.
*
- * @return string The DN.
+ * @return string The UID.
*
* @throws Horde_Kolab_Server_Exception If the given type is unknown.
*/
@@ -109,22 +117,22 @@ class Horde_Kolab_Server_Kolab extends Horde_Kolab_Server_Ldap
if (empty($info['user_type'])) {
return parent::generateServerUid($type, $id, $info);
} else if ($info['user_type'] == Horde_Kolab_Server_Object_Kolab_User::USERTYPE_INTERNAL) {
- return sprintf('%s,cn=internal,%s', $id, $this->getBaseUid());
+ return sprintf('%s,cn=internal,%s', $id, $this->server->getBaseUid());
} else if ($info['user_type'] == Horde_Kolab_Server_Object_Kolab_User::USERTYPE_GROUP) {
- return sprintf('%s,cn=groups,%s', $id, $this->getBaseUid());
+ return sprintf('%s,cn=groups,%s', $id, $this->server->getBaseUid());
} else if ($info['user_type'] == Horde_Kolab_Server_Object_Kolab_User::USERTYPE_RESOURCE) {
- return sprintf('%s,cn=resources,%s', $id, $this->getBaseUid());
+ return sprintf('%s,cn=resources,%s', $id, $this->server->getBaseUid());
} else {
return parent::generateServerUid($type, $id, $info);
}
case 'Horde_Kolab_Server_Object_Kolab_Address':
- return sprintf('%s,cn=external,%s', $id, $this->getBaseUid());
+ return sprintf('%s,cn=external,%s', $id, $this->server->getBaseUid());
case 'Horde_Kolab_Server_Object_Kolabgroupofnames':
case 'Horde_Kolab_Server_Object_Kolab_Distlist':
if (!isset($info['visible']) || !empty($info['visible'])) {
return parent::generateServerUid($type, $id, $info);
} else {
- return sprintf('%s,cn=internal,%s', $id, $this->getBaseUid());
+ return sprintf('%s,cn=internal,%s', $id, $this->server->getBaseUid());
}
case 'Horde_Kolab_Server_Object_Kolabsharedfolder':
case 'Horde_Kolab_Server_Object_Kolab_Administrator':
diff --git a/framework/Kolab_Server/lib/Horde/Kolab/Server/Structure/Ldap.php b/framework/Kolab_Server/lib/Horde/Kolab/Server/Structure/Ldap.php
new file mode 100644
index 000000000..ed06de1bf
--- /dev/null
+++ b/framework/Kolab_Server/lib/Horde/Kolab/Server/Structure/Ldap.php
@@ -0,0 +1,86 @@
+
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=Kolab_Server
+ */
+
+/**
+ * This class provides methods to deal with the LDAP tree structure.
+ *
+ * Copyright 2008-2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Kolab
+ * @package Kolab_Server
+ * @author Gunnar Wrobel
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=Kolab_Server
+ */
+class Horde_Kolab_Server_Structure_Ldap extends Horde_Kolab_Server_Structure
+{
+ /**
+ * Returns the set of objects supported by this structure.
+ *
+ * @return array An array of supported objects.
+ */
+ public function getSupportedObjects()
+ {
+ return array(
+ 'Horde_Kolab_Server_Object',
+ );
+ }
+
+ /**
+ * Determine the type of an object by its tree position and other
+ * parameters.
+ *
+ * @param string $uid The UID of the object to examine.
+ *
+ * @return string The class name of the corresponding object type.
+ *
+ * @throws Horde_Kolab_Server_Exception If the object type is unknown.
+ */
+ public function determineType($uid)
+ {
+ $ocs = $this->server->getObjectClasses($uid);
+ $ocs = array_reverse($ocs);
+ foreach ($ocs as $oc) {
+ try {
+ $class_name = 'Horde_Kolab_Server_Object_' . ucfirst(strtolower($oc));
+ Horde_Kolab_Server_Object::loadClass($class_name);
+ return $class_name;
+ } catch (Horde_Kolab_Server_Exception $e) {
+ }
+ }
+ if ($oc == 'top') {
+ return 'Horde_Kolab_Server_Object';
+ }
+ throw new Horde_Kolab_Server_Exception(sprintf(_("Unkown object type for UID %s."),
+ $uid));
+ }
+
+ /**
+ * Generates a UID for the given information.
+ *
+ * @param string $type The class name of the object to create.
+ * @param string $id The id of the object.
+ * @param array $info Any additional information about the object to create.
+ *
+ * @return string The UID.
+ *
+ * @throws Horde_Kolab_Server_Exception If the given type is unknown.
+ */
+ public function generateServerUid($type, $id, $info)
+ {
+ return sprintf('%s,%s', $id, $this->server->getBaseUid());
+ }
+}
diff --git a/framework/Kolab_Server/lib/Horde/Kolab/Server/Test.php b/framework/Kolab_Server/lib/Horde/Kolab/Server/Test.php
index 75af6b912..b525b88dd 100644
--- a/framework/Kolab_Server/lib/Horde/Kolab/Server/Test.php
+++ b/framework/Kolab_Server/lib/Horde/Kolab/Server/Test.php
@@ -25,7 +25,7 @@
* @license http://www.fsf.org/copyleft/lgpl.html LGPL
* @link http://pear.horde.org/index.php?package=Kolab_Server
*/
-class Horde_Kolab_Server_Test extends Horde_Kolab_Server_Kolab
+class Horde_Kolab_Server_Test extends Horde_Kolab_Server_Ldap
{
/**
diff --git a/framework/Kolab_Server/package.xml b/framework/Kolab_Server/package.xml
index e443dd65c..2c7ee9524 100644
--- a/framework/Kolab_Server/package.xml
+++ b/framework/Kolab_Server/package.xml
@@ -50,7 +50,6 @@ http://pear.php.net/dtd/package-2.0.xsd">
-
@@ -73,6 +72,11 @@ http://pear.php.net/dtd/package-2.0.xsd">
+
+
+
+
+
@@ -143,7 +147,6 @@ http://pear.php.net/dtd/package-2.0.xsd">
-
@@ -162,6 +165,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
+
+
+
diff --git a/framework/Kolab_Server/test/Horde/Kolab/Server/ServerTest.php b/framework/Kolab_Server/test/Horde/Kolab/Server/ServerTest.php
index 8275b7b5e..5fdc68681 100644
--- a/framework/Kolab_Server/test/Horde/Kolab/Server/ServerTest.php
+++ b/framework/Kolab_Server/test/Horde/Kolab/Server/ServerTest.php
@@ -145,7 +145,7 @@ class Horde_Kolab_Server_None extends Horde_Kolab_Server
*
* @return string The corresponding Kolab object type.
*/
- protected function determineType($uid)
+ public function determineType($uid)
{
return 'Horde_Kolab_Server_Object_Kolab_User';
}
@@ -240,4 +240,14 @@ class Horde_Kolab_Server_None extends Horde_Kolab_Server
/* In the default class we just return an empty array */
return array();
}
+
+ /**
+ * Returns the set of objects supported by this server.
+ *
+ * @return array An array of supported objects.
+ */
+ public function getSupportedObjects()
+ {
+ return array('Horde_Kolab_Server_Object');
+ }
}
--
2.11.0