Branch 'dev/edit-existing-entries' - 3 commits - lib/api lib/Auth

Jeroen van Meeuwen vanmeeuwen at kolabsys.com
Sun Apr 1 00:18:41 CEST 2012


 lib/Auth/LDAP.php                   |   38 ++++++++++++++++++++-----
 lib/api/kolab_api_service_group.php |   47 +++++++++++++++++--------------
 lib/api/kolab_api_service_user.php  |   54 ++++++++++++++++++++++--------------
 3 files changed, 91 insertions(+), 48 deletions(-)

New commits:
commit 5c72e4a7010e681dc18cae6f8ade0225cc21c53b
Author: Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen at kolabsys.com>
Date:   Sun Apr 1 00:18:13 2012 +0200

    Correct the comparison of an attribute being marked as optional

diff --git a/lib/api/kolab_api_service_group.php b/lib/api/kolab_api_service_group.php
index 700ff25..3e82f0d 100644
--- a/lib/api/kolab_api_service_group.php
+++ b/lib/api/kolab_api_service_group.php
@@ -64,7 +64,7 @@ class kolab_api_service_group extends kolab_api_service
                 error_log("form field $key");
                 if (
                         (!isset($postdata[$key]) || empty($postdata[$key]) &&
-                        !(array_key_exists('optional', $value) || $value['optional'])
+                        (!array_key_exists('optional', $value) || !$value['optional'])
                     ) {
                     throw new Exception("Missing input value for $key", 345);
                 }
@@ -77,7 +77,7 @@ class kolab_api_service_group extends kolab_api_service
         if (isset($gta['auto_form_fields'])) {
             foreach ($gta['auto_form_fields'] as $key => $value) {
                 if (empty($postdata[$key])) {
-                    if (!array_key_exists('optional', $value) || $value['optional']) {
+                    if (!array_key_exists('optional', $value) || !$value['optional']) {
                         $postdata['attributes'] = array($key);
                         $res                    = $form_service->generate($getdata, $postdata);
                         $postdata[$key]         = $res[$key];
@@ -166,7 +166,7 @@ class kolab_api_service_group extends kolab_api_service
             foreach ($gta['form_fields'] as $key => $value) {
                 if (
                         (!isset($postdata[$key]) || empty($postdata[$key])) &&
-                        !(array_key_exists('optional', $value) && $value['optional'])
+                        (!array_key_exists('optional', $value) || !$value['optional'])
                     ) {
                     throw new Exception("Missing input value for $key", 345);
                 }
@@ -179,7 +179,7 @@ class kolab_api_service_group extends kolab_api_service
         if (isset($gta['auto_form_fields'])) {
             foreach ($gta['auto_form_fields'] as $key => $value) {
                 if (empty($postdata[$key])) {
-                    if (!array_key_exists('optional', $value) || $value['optional']) {
+                    if (!array_key_exists('optional', $value) || !$value['optional']) {
                         $postdata['attributes'] = array($key);
                         $res                    = $form_service->generate($getdata, $postdata);
                         $postdata[$key]         = $res[$key];
diff --git a/lib/api/kolab_api_service_user.php b/lib/api/kolab_api_service_user.php
index 21276e2..a5c58e2 100644
--- a/lib/api/kolab_api_service_user.php
+++ b/lib/api/kolab_api_service_user.php
@@ -59,6 +59,8 @@ class kolab_api_service_user extends kolab_api_service
      */
     public function user_add($getdata, $postdata)
     {
+        console("user_add()", $postdata);
+
         $uta             = $this->object_type_attributes('user', $postdata['type_id']);
         $form_service    = $this->controller->get_service('form_value');
         $user_attributes = array();
@@ -67,7 +69,7 @@ class kolab_api_service_user extends kolab_api_service
             foreach ($uta['form_fields'] as $key => $value) {
                 if (
                         (!isset($postdata[$key]) || empty($postdata[$key])) &&
-                        !(array_key_exists('optional', $value) && $value['optional'])
+                        (!array_key_exists('optional', $value) || !$value['optional'])
                     ) {
                     throw new Exception("Missing input value for $key", 345);
                 }
@@ -80,26 +82,28 @@ class kolab_api_service_user extends kolab_api_service
         if (isset($uta['auto_form_fields'])) {
             foreach ($uta['auto_form_fields'] as $key => $value) {
                 if (empty($postdata[$key])) {
-                    if (!array_key_exists('optional', $value) || $value['optional']) {
+                    console("Key $key empty in \$postdata");
+                    // If the attribute is marked as optional, however...
+                    if (!array_key_exists('optional', $value) || !$value['optional']) {
                         $postdata['attributes'] = array($key);
                         $res                    = $form_service->generate($getdata, $postdata);
                         $postdata[$key]         = $res[$key];
                         $user_attributes[$key] = $postdata[$key];
                     }
+                } else {
+                    $user_attributes[$key] = $postdata[$key];
                 }
             }
         }
 
         if (isset($uta['fields'])) {
             foreach ($uta['fields'] as $key => $value) {
-                if (!isset($postdata[$key]) || empty($postdata[$key])) {
-                    $user_attributes[$key] = $uta['fields'][$key];
-                } else {
-                    $user_attributes[$key] = $postdata[$key];
-                }
+                $user_attributes[$key] = $uta['fields'][$key];
             }
         }
 
+        console("user_add()", $user_attributes);
+
         $auth = Auth::get_instance();
         $result = $auth->user_add($user_attributes, $postdata['type_id']);
 
@@ -137,6 +141,8 @@ class kolab_api_service_user extends kolab_api_service
 
     public function user_edit($getdata, $postdata)
     {
+        console("\$postdata to user_edit()", $postdata);
+
         $uta             = $this->object_type_attributes('user', $postdata['type_id']);
         $form_service    = $this->controller->get_service('form_value');
         $user_attributes = array();
@@ -171,7 +177,7 @@ class kolab_api_service_user extends kolab_api_service
             foreach ($uta['form_fields'] as $key => $value) {
                 if (
                         (!isset($postdata[$key]) || empty($postdata[$key])) &&
-                        !(array_key_exists('optional', $value) && $value['optional']) 
+                        (!array_key_exists('optional', $value) || !$value['optional']) 
                     ) {
                     throw new Exception("Missing input value for $key", 345);
                 }
@@ -184,7 +190,7 @@ class kolab_api_service_user extends kolab_api_service
         if (isset($uta['auto_form_fields'])) {
             foreach ($uta['auto_form_fields'] as $key => $value) {
                 if (empty($postdata[$key])) {
-                    if (!array_key_exists('optional', $value) || $value['optional']) {
+                    if (!array_key_exists('optional', $value) || !$value['optional']) {
                         $postdata['attributes'] = array($key);
                         $res                    = $form_service->generate($getdata, $postdata);
                         $postdata[$key]         = $res[$key];


commit f5e7a053fe74f4b00d7a68c6ea06472c400bdba6
Author: Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen at kolabsys.com>
Date:   Sat Mar 31 23:55:24 2012 +0200

    Improve handling of optional form fields

diff --git a/lib/api/kolab_api_service_group.php b/lib/api/kolab_api_service_group.php
index e7fef8a..700ff25 100644
--- a/lib/api/kolab_api_service_group.php
+++ b/lib/api/kolab_api_service_group.php
@@ -62,7 +62,10 @@ class kolab_api_service_group extends kolab_api_service
         if (isset($gta['form_fields'])) {
             foreach ($gta['form_fields'] as $key => $value) {
                 error_log("form field $key");
-                if (!isset($postdata[$key]) || $postdata[$key] === '') {
+                if (
+                        (!isset($postdata[$key]) || empty($postdata[$key]) &&
+                        !(array_key_exists('optional', $value) || $value['optional'])
+                    ) {
                     throw new Exception("Missing input value for $key", 345);
                 }
                 else {
@@ -73,10 +76,14 @@ class kolab_api_service_group extends kolab_api_service
 
         if (isset($gta['auto_form_fields'])) {
             foreach ($gta['auto_form_fields'] as $key => $value) {
-                if (!isset($postdata[$key])) {
-                    throw new Exception("Key not set: " . $key, 12356);
-                }
-                else {
+                if (empty($postdata[$key])) {
+                    if (!array_key_exists('optional', $value) || $value['optional']) {
+                        $postdata['attributes'] = array($key);
+                        $res                    = $form_service->generate($getdata, $postdata);
+                        $postdata[$key]         = $res[$key];
+                        $group_attributes[$key]  = $postdata[$key];
+                    }
+                } else {
                     $group_attributes[$key] = $postdata[$key];
                 }
             }
@@ -84,12 +91,7 @@ class kolab_api_service_group extends kolab_api_service
 
         if (isset($gta['fields'])) {
             foreach ($gta['fields'] as $key => $value) {
-                if (!isset($postdata[$key]) || empty($postdata[$key])) {
-                    $group_attributes[$key] = $gta['fields'][$key];
-                }
-                else {
-                    $group_attributes[$key] = $postdata[$key];
-                }
+                $group_attributes[$key] = $gta['fields'][$key];
             }
         }
 
@@ -162,7 +164,10 @@ class kolab_api_service_group extends kolab_api_service
 
         if (isset($gta['form_fields'])) {
             foreach ($gta['form_fields'] as $key => $value) {
-                if (!isset($postdata[$key]) || empty($postdata[$key])) {
+                if (
+                        (!isset($postdata[$key]) || empty($postdata[$key])) &&
+                        !(array_key_exists('optional', $value) && $value['optional'])
+                    ) {
                     throw new Exception("Missing input value for $key", 345);
                 }
                 else {
@@ -174,21 +179,21 @@ class kolab_api_service_group extends kolab_api_service
         if (isset($gta['auto_form_fields'])) {
             foreach ($gta['auto_form_fields'] as $key => $value) {
                 if (empty($postdata[$key])) {
-                    $postdata['attributes'] = array($key);
-                    $res                    = $form_service->generate($getdata, $postdata);
-                    $postdata[$key]         = $res[$key];
+                    if (!array_key_exists('optional', $value) || $value['optional']) {
+                        $postdata['attributes'] = array($key);
+                        $res                    = $form_service->generate($getdata, $postdata);
+                        $postdata[$key]         = $res[$key];
+                        $group_attributes[$key]  = $postdata[$key];
+                    }
+                } else {
+                    $group_attributes[$key] = $postdata[$key];
                 }
-                $group_attributes[$key] = $postdata[$key];
             }
         }
 
         if (isset($gta['fields'])) {
             foreach ($gta['fields'] as $key => $value) {
-                if (!isset($postdata[$key]) || empty($postdata[$key])) {
-                    $group_attributes[$key] = $gta['fields'][$key];
-                } else {
-                    $group_attributes[$key] = $postdata[$key];
-                }
+                $group_attributes[$key] = $gta['fields'][$key];
             }
 
             $group_attributes[$unique_attr] = $postdata[$unique_attr];
diff --git a/lib/api/kolab_api_service_user.php b/lib/api/kolab_api_service_user.php
index 4695fdf..21276e2 100644
--- a/lib/api/kolab_api_service_user.php
+++ b/lib/api/kolab_api_service_user.php
@@ -65,7 +65,10 @@ class kolab_api_service_user extends kolab_api_service
 
         if (isset($uta['form_fields'])) {
             foreach ($uta['form_fields'] as $key => $value) {
-                if (!isset($postdata[$key]) || empty($postdata[$key])) {
+                if (
+                        (!isset($postdata[$key]) || empty($postdata[$key])) &&
+                        !(array_key_exists('optional', $value) && $value['optional'])
+                    ) {
                     throw new Exception("Missing input value for $key", 345);
                 }
                 else {
@@ -77,11 +80,13 @@ class kolab_api_service_user extends kolab_api_service
         if (isset($uta['auto_form_fields'])) {
             foreach ($uta['auto_form_fields'] as $key => $value) {
                 if (empty($postdata[$key])) {
-                    $postdata['attributes'] = array($key);
-                    $res                    = $form_service->generate($getdata, $postdata);
-                    $postdata[$key]         = $res[$key];
+                    if (!array_key_exists('optional', $value) || $value['optional']) {
+                        $postdata['attributes'] = array($key);
+                        $res                    = $form_service->generate($getdata, $postdata);
+                        $postdata[$key]         = $res[$key];
+                        $user_attributes[$key] = $postdata[$key];
+                    }
                 }
-                $user_attributes[$key] = $postdata[$key];
             }
         }
 
@@ -164,7 +169,10 @@ class kolab_api_service_user extends kolab_api_service
 
         if (isset($uta['form_fields'])) {
             foreach ($uta['form_fields'] as $key => $value) {
-                if (!isset($postdata[$key]) || empty($postdata[$key])) {
+                if (
+                        (!isset($postdata[$key]) || empty($postdata[$key])) &&
+                        !(array_key_exists('optional', $value) && $value['optional']) 
+                    ) {
                     throw new Exception("Missing input value for $key", 345);
                 }
                 else {
@@ -176,21 +184,21 @@ class kolab_api_service_user extends kolab_api_service
         if (isset($uta['auto_form_fields'])) {
             foreach ($uta['auto_form_fields'] as $key => $value) {
                 if (empty($postdata[$key])) {
-                    $postdata['attributes'] = array($key);
-                    $res                    = $form_service->generate($getdata, $postdata);
-                    $postdata[$key]         = $res[$key];
+                    if (!array_key_exists('optional', $value) || $value['optional']) {
+                        $postdata['attributes'] = array($key);
+                        $res                    = $form_service->generate($getdata, $postdata);
+                        $postdata[$key]         = $res[$key];
+                        $user_attributes[$key]  = $postdata[$key];
+                    }
+                } else {
+                    $user_attributes[$key] = $postdata[$key];
                 }
-                $user_attributes[$key] = $postdata[$key];
             }
         }
 
         if (isset($uta['fields'])) {
             foreach ($uta['fields'] as $key => $value) {
-                if (!isset($postdata[$key]) || empty($postdata[$key])) {
-                    $user_attributes[$key] = $uta['fields'][$key];
-                } else {
-                    $user_attributes[$key] = $postdata[$key];
-                }
+                $user_attributes[$key] = $uta['fields'][$key];
             }
 
             $user_attributes[$unique_attr] = $postdata[$unique_attr];


commit 9abe372a0aa14187f1a3e098d2f2b7c99f746c09
Author: Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen at kolabsys.com>
Date:   Sat Mar 31 23:52:46 2012 +0200

    Only replace attributes that are not empty.
    Delete attributes that are empty.

diff --git a/lib/Auth/LDAP.php b/lib/Auth/LDAP.php
index 2a26c0a..506e3e5 100644
--- a/lib/Auth/LDAP.php
+++ b/lib/Auth/LDAP.php
@@ -401,8 +401,13 @@ class LDAP
                     if ($attr === $rdn_attr) {
                         $mod_array['rename'][$subject_dn] = $rdn_attr . '=' . $new_attrs[$attr];
                     } else {
-                        console("Adding to replace: $attr");
-                        $mod_array['replace'][$attr] = (array)($new_attrs[$attr]);
+                        if (empty($new_attrs[$attr])) {
+                            console("Adding to del: $attr");
+                            $mod_array['del'][$attr] = (array)($old_attr_value);
+                        } else {
+                            console("Adding to replace: $attr");
+                            $mod_array['replace'][$attr] = (array)($new_attrs[$attr]);
+                        }
                     }
                 } else {
                     console("Attribute $attr unchanged");
@@ -416,14 +421,23 @@ class LDAP
 
         foreach ($new_attrs as $attr => $value) {
             if (array_key_exists($attr, $old_attrs)) {
-                if (!($old_attrs[$attr] === $value) && !($attr === $rdn_attr)) {
-                    if (!array_key_exists($attr, $mod_array['replace'])) {
-                        console("Adding to replace(2): $attr");
-                        $mod_array['replace'][$attr] = $value;
+                if (empty($value)) {
+                    if (!array_key_exists($attr, $mod_array['del'])) {
+                        console("Adding to del(2): $attr");
+                        $mod_array['del'][$attr] = (array)($old_attrs[$attr]);
+                    }
+                } else {
+                    if (!($old_attrs[$attr] === $value) && !($attr === $rdn_attr)) {
+                        if (!array_key_exists($attr, $mod_array['replace'])) {
+                            console("Adding to replace(2): $attr");
+                            $mod_array['replace'][$attr] = $value;
+                        }
                     }
                 }
             } else {
-                $mod_array['add'][$attr] = $value;
+                if (!empty($value)) {
+                    $mod_array['add'][$attr] = $value;
+                }
             }
         }
 
@@ -453,6 +467,16 @@ class LDAP
             return false;
         }
 
+        if (is_array($attributes['del']) && !empty($attributes['del'])) {
+            $result = ldap_mod_del($this->conn, $subject_dn, $attributes['del']);
+        }
+
+        if (!$result) {
+            console("Failed to delete the following attributes", $attributes['del']);
+            return false;
+        }
+
+
         if (is_array($attributes['add']) && !empty($attributes['add'])) {
             $result = ldap_mod_add($this->conn, $subject_dn, $attributes['add']);
         }





More information about the commits mailing list