From: Gunnar Wrobel Date: Fri, 1 May 2009 13:01:26 +0000 (+0200) Subject: Rewrote the logic used to store attribute changes. The old approach did not work... X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=f3e0cf92a153ae4fdf6893077eed369de82c7bd6;p=horde.git Rewrote the logic used to store attribute changes. The old approach did not work well with adding/deleting values of multi-value attributes. --- diff --git a/framework/Kolab_Server/lib/Horde/Kolab/Server/Ldap.php b/framework/Kolab_Server/lib/Horde/Kolab/Server/Ldap.php index 27990d9bb..4faef08e6 100644 --- a/framework/Kolab_Server/lib/Horde/Kolab/Server/Ldap.php +++ b/framework/Kolab_Server/lib/Horde/Kolab/Server/Ldap.php @@ -227,9 +227,9 @@ class Horde_Kolab_Server_Ldap extends Horde_Kolab_Server /** * Save an object. * - * @param string $uid The UID of the object to be added. - * @param array $data The attributes of the object to be added. - * @param boolean $exists Does the object already exist on the server? + * @param string $uid The UID of the object to be added. + * @param array $data The attributes of the object to be changed. + * @param boolean $exists Does the object already exist on the server? * * @return boolean True if saving succeeded. */ @@ -238,34 +238,16 @@ class Horde_Kolab_Server_Ldap extends Horde_Kolab_Server $this->mapAttributes($data); if ($exists === false) { - $entry = Net_LDAP2_Entry::createFresh($uid, $data); + $entry = Net_LDAP2_Entry::createFresh($uid, $data['add']); $result = $this->_ldap->add($entry); if ($result instanceOf PEAR_Error) { throw new Horde_Kolab_Server_Exception($result, Horde_Kolab_Server_Exception::SYSTEM); } } else { - $deletes = array(); - foreach ($data as $key => $values) { - $empty = true; - if (!is_array($values)) { - $values = array($values); - } - foreach ($values as $value) { - if (!($value === null || $info[$key] === '')) { - $empty = false; - break; - } - } - if ($empty === true) { - $deletes[] = $key; - unset($data[$key]); - } - } /* Net_LDAP2 will work on this as a reference */ $mod_uid = $uid; - $result = $this->_ldap->modify($mod_uid, array('delete' => $deletes, - 'replace' => $data)); + $result = $this->_ldap->modify($mod_uid, $data); if ($result instanceOf PEAR_Error) { throw new Horde_Kolab_Server_Exception($result, Horde_Kolab_Server_Exception::SYSTEM); diff --git a/framework/Kolab_Server/lib/Horde/Kolab/Server/Test.php b/framework/Kolab_Server/lib/Horde/Kolab/Server/Test.php index 94dae0040..4d8516d17 100644 --- a/framework/Kolab_Server/lib/Horde/Kolab/Server/Test.php +++ b/framework/Kolab_Server/lib/Horde/Kolab/Server/Test.php @@ -508,9 +508,9 @@ class Horde_Kolab_Server_Test extends Horde_Kolab_Server_Ldap /** * Save an object. * - * @param string $uid The UID of the object to be added. - * @param array $data The attributes of the object to be added. - * @param boolean $exists Does the object already exist on the server? + * @param string $uid The UID of the object to be added. + * @param array $data The attributes of the object to be added/replaced. + * @param boolean $exists Does the object already exist on the server? * * @return boolean True if saving succeeded. */ @@ -520,30 +520,71 @@ class Horde_Kolab_Server_Test extends Horde_Kolab_Server_Ldap $result = $this->bind(); } - $this->mapAttributes($data); + if ($exists === false) { - $ldap_data = array(); - foreach ($data as $key => $val) { - if (!is_array($val)) { - $val = array($val); - } - $ldap_data[$key] = $val; - } + $ldap_data = $this->_toStorage($data['add']); - if ($exists === false) { $this->data[$uid] = array( 'dn' => $uid, 'data' => array_merge($ldap_data, array('dn' => $uid)), ); } else { - $this->data[$uid] = array( - 'dn' => $uid, - 'data' => array_merge($this->data[$uid]['data'], - $ldap_data, - array('dn' => $uid)), - ); + + if (isset($data['delete'])) { + foreach ($data['delete'] as $k => $v) { + if (is_int($k)) { + $w = $this->mapField($v); + if (isset($this->data[$uid]['data'][$w])) { + /** Delete a complete attribute */ + unset($this->data[$uid]['data'][$w]); + } + } else { + $l = $this->mapField($k); + if (isset($this->data[$uid]['data'][$l])) { + if (!is_array($v)) { + $v = array($v); + } + foreach ($v as $w) { + $key = array_search($w, $this->data[$uid]['data'][$l]); + if ($key !== false) { + /** Delete a single value */ + unset($this->data[$uid]['data'][$l][$key]); + } + } + } + } + } + } + + if (isset($data['replace'])) { + $ldap_data = $this->_toStorage($data['replace']); + + $this->data[$uid] = array( + 'dn' => $uid, + 'data' => array_merge($this->data[$uid]['data'], + $ldap_data, + array('dn' => $uid)), + ); + } + + if (isset($data['add'])) { + $ldap_data = $this->_toStorage($data['add']); + + foreach ($ldap_data as $k => $v) { + if (is_array($v)) { + foreach ($v as $w) { + $this->data[$uid]['data'][$k][] = $w; + } + } else { + $this->data[$uid]['data'][$k][] = $v; + } + $this->data[$uid]['data'][$k] = array_values($this->data[$uid]['data'][$k]); + } + } } + + Horde::logMessage(sprintf('The object \"%s\" has been successfully saved!', $uid), __FILE__, __LINE__, PEAR_LOG_DEBUG); @@ -551,6 +592,27 @@ class Horde_Kolab_Server_Test extends Horde_Kolab_Server_Ldap } /** + * Rewrite a data array to our internal storage format. + * + * @param array $data The attributes of the object to be added/replaced. + * + * @return array The transformed data set. + */ + private function _toStorage($data) + { + $this->mapAttributes($data); + + $ldap_data = array(); + foreach ($data as $key => $val) { + if (!is_array($val)) { + $val = array($val); + } + $ldap_data[$key] = $val; + } + return $ldap_data; + } + + /** * Delete an object. * * @param string $uid The UID of the object to be deleted.