3 commits - lib/api lib/client lib/kolab_api_service.php lib/kolab_client_task.php public_html/js

Aleksander Machniak machniak at kolabsys.com
Mon Apr 2 12:52:31 CEST 2012


 lib/api/kolab_api_service_group.php    |   72 +++----------------
 lib/api/kolab_api_service_user.php     |   83 ++--------------------
 lib/client/kolab_client_task_group.php |    4 -
 lib/client/kolab_client_task_user.php  |    2 
 lib/kolab_api_service.php              |   51 ++++++++++++++
 lib/kolab_client_task.php              |   16 +---
 public_html/js/kolab_admin.js          |  120 +++------------------------------
 7 files changed, 95 insertions(+), 253 deletions(-)

New commits:
commit 80dfc4211d1b5f139e07c45d022319bf1b3d8ba8
Author: Aleksander Machniak <machniak at kolabsys.com>
Date:   Mon Apr 2 12:50:39 2012 +0200

    Simplify some code, don't check userpassword2 on API backend

diff --git a/lib/api/kolab_api_service_group.php b/lib/api/kolab_api_service_group.php
index f008086..bbbabe0 100644
--- a/lib/api/kolab_api_service_group.php
+++ b/lib/api/kolab_api_service_group.php
@@ -61,11 +61,7 @@ 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]) || empty($postdata[$key])) &&
-                        (!array_key_exists('optional', $value) || !$value['optional'])
-                    ) {
+                if (empty($postdata[$key]) && empty($value['optional'])) {
                     throw new Exception("Missing input value for $key", 345);
                 }
                 else {
@@ -77,7 +73,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 (empty($value['optional'])) {
                         $postdata['attributes'] = array($key);
                         $res                    = $form_service->generate($getdata, $postdata);
                         $postdata[$key]         = $res[$key];
@@ -172,10 +168,7 @@ 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])) &&
-                        (!array_key_exists('optional', $value) || !$value['optional'])
-                    ) {
+                if (empty($postdata[$key]) && empty($value['optional'])) {
                     throw new Exception("Missing input value for $key", 345);
                 }
                 else {
@@ -187,11 +180,11 @@ 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 (empty($value['optional'])) {
                         $postdata['attributes'] = array($key);
                         $res                    = $form_service->generate($getdata, $postdata);
                         $postdata[$key]         = $res[$key];
-                        $group_attributes[$key]  = $postdata[$key];
+                        $group_attributes[$key] = $postdata[$key];
                     }
                 } else {
                     $group_attributes[$key] = $postdata[$key];
diff --git a/lib/api/kolab_api_service_user.php b/lib/api/kolab_api_service_user.php
index 91c6c69..106fe33 100644
--- a/lib/api/kolab_api_service_user.php
+++ b/lib/api/kolab_api_service_user.php
@@ -67,10 +67,7 @@ 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])) &&
-                        (!array_key_exists('optional', $value) || !$value['optional'])
-                    ) {
+                if (empty($postdata[$key]) && empty($value['optional'])) {
                     throw new Exception("Missing input value for $key", 345);
                 }
                 else {
@@ -82,9 +79,8 @@ 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])) {
-                    console("Key $key empty in \$postdata");
                     // If the attribute is marked as optional, however...
-                    if (!array_key_exists('optional', $value) || !$value['optional']) {
+                    if (empty($value['optional'])) {
                         $postdata['attributes'] = array($key);
                         $res                    = $form_service->generate($getdata, $postdata);
                         $postdata[$key]         = $res[$key];
@@ -195,10 +191,7 @@ 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])) &&
-                        (!array_key_exists('optional', $value) || !$value['optional']) 
-                    ) {
+                if (empty($postdata[$key]) && empty($value['optional'])) {
                     throw new Exception("Missing input value for $key", 345);
                 }
                 else {
@@ -210,26 +203,11 @@ 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])) {
-                    switch ($key) {
-                        case "userpassword":
-                            if (!empty($postdata['userpassword']) && !empty($postdata['userpassword2'])) {
-                                if ($postdata['userpassword'] === $postdata['userpassword2']) {
-                                    $user_password = $postdata['userpassword'];
-                                } else {
-                                    throw new Exception("Password mismatch");
-                                }
-                            } else {
-                                $user_attributes[$key] = $_user[$key];
-                            }
-                            break;
-                        default:
-                            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];
-                            }
-                            break;
+                    if (empty($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];


commit 77b929decec283a2440a94656ba8b2e313d1656c
Author: Aleksander Machniak <machniak at kolabsys.com>
Date:   Mon Apr 2 12:40:29 2012 +0200

    Unify function names (remove duplicated code)

diff --git a/lib/kolab_client_task.php b/lib/kolab_client_task.php
index d24376a..c6e3b79 100644
--- a/lib/kolab_client_task.php
+++ b/lib/kolab_client_task.php
@@ -958,7 +958,7 @@ class kolab_client_task
         ));
 
         if (!empty($data['id'])) {
-            $id = $data[$name];
+            $id = $data['id'];
             $form->add_button(array(
                 'value'   => kolab_html::escape($this->translate('delete.button')),
                 'onclick' => "kadm.{$name}_delete('{$id}')",
diff --git a/public_html/js/kolab_admin.js b/public_html/js/kolab_admin.js
index fe398f8..45329be 100644
--- a/public_html/js/kolab_admin.js
+++ b/public_html/js/kolab_admin.js
@@ -1024,15 +1024,14 @@ function kolab_admin()
     this.command('user.list', {page: page});
   };
 
-  this.user_add = function(reload, section)
+  this.user_save = function(reload, section)
   {
-    var data = this.serialize_form('#'+this.env.form_id);
+    var data = this.serialize_form('#'+this.env.form_id),
+      action = data.id ? 'edit' : 'add';
 
-    if (reload || reload == '') {
-      // TODO: Not needed?
+    if (reload) {
       data.section = section;
-
-      this.http_post('user.add');
+      this.http_post('user.' + action, {data: data});
       return;
     }
 
@@ -1046,7 +1045,7 @@ function kolab_admin()
     }
 
     this.set_busy(true, 'saving');
-    this.api_post('user.add', data, 'user_add_response');
+    this.api_post('user.' + action, data, 'user_' + action + '_response');
   };
 
   this.user_add_response = function(response)
@@ -1058,29 +1057,6 @@ function kolab_admin()
     this.command('user.list', {page: this.env.list_page});
   };
 
-  this.user_edit = function(reload, section)
-  {
-    var data = this.serialize_form('#'+this.env.form_id);
-
-    if (reload) {
-      data.section = section;
-      this.http_post('user.edit', {data: data});
-      return;
-    }
-
-    this.form_error_clear();
-
-    // check password
-    if (data.userpassword != data.userpassword2) {
-      this.display_message('user.password.mismatch', 'error');
-      this.form_value_error('userpassword2');
-      return;
-    }
-
-    this.set_busy(true, 'saving');
-    this.api_post('user.edit', data, 'user_edit_response');
-  };
-
   this.user_edit_response = function(response)
   {
     if (!this.api_response(response))
@@ -1090,38 +1066,6 @@ function kolab_admin()
     this.command('user.list', {page: this.env.list_page});
   };
 
-  this.user_save = function(reload, section)
-  {
-    var data = this.serialize_form('#'+this.env.form_id);
-
-    if (reload) {
-      data.section = section;
-      this.http_post('user.add', {data: data});
-      return;
-    }
-
-    this.form_error_clear();
-
-    // check password
-    if (data.userpassword != data.userpassword2) {
-      this.display_message('user.password.mismatch', 'error');
-      this.form_value_error('userpassword2');
-      return;
-    }
-
-    this.set_busy(true, 'saving');
-    this.api_post('user.add', data, 'user_save_response');
-  };
-
-  this.user_save_response = function(response)
-  {
-    if (!this.api_response(response))
-      return;
-
-    this.display_message('user.add.success');
-    this.command('user.list', {page: this.env.list_page});
-  };
-
   this.group_info = function(id)
   {
     this.http_post('group.info', {id: id});
@@ -1159,20 +1103,21 @@ function kolab_admin()
     this.command('group.list', {page: page});
   };
 
-  this.group_add = function(reload, section)
+  this.group_save = function(reload, section)
   {
-    var data = this.serialize_form('#'+this.env.form_id);
+    var data = this.serialize_form('#'+this.env.form_id),
+      action = data.id ? 'edit' : 'add';
 
-    if (reload || reload == '') {
+    if (reload) {
       data.section = section;
-      this.http_post('group.add');
+      this.http_post('group.' + action, {data: data});
       return;
     }
 
     this.form_error_clear();
 
     this.set_busy(true, 'saving');
-    this.api_post('group.add', data, 'group_add_response');
+    this.api_post('group.' + action, data, 'group_' + action + '_response');
   };
 
   this.group_add_response = function(response)
@@ -1184,22 +1129,6 @@ function kolab_admin()
     this.command('group.list', {page: this.env.list_page});
   };
 
-  this.group_edit = function(reload, section)
-  {
-    var data = this.serialize_form('#'+this.env.form_id);
-
-    if (reload) {
-      data.section = section;
-      this.http_post('group.edit', {data: data});
-      return;
-    }
-
-    this.form_error_clear();
-
-    this.set_busy(true, 'saving');
-    this.api_post('group.edit', data, 'group_edit_response');
-  };
-
   this.group_edit_response = function(response)
   {
     if (!this.api_response(response))
@@ -1209,31 +1138,6 @@ function kolab_admin()
     this.command('group.list', {page: this.env.list_page});
   };
 
-  this.group_save = function(reload, section)
-  {
-    var data = this.serialize_form('#'+this.env.form_id);
-
-    if (reload) {
-      data.section = section;
-      this.http_post('group.add', {data: data});
-      return;
-    }
-
-    this.form_error_clear();
-
-    this.set_busy(true, 'saving');
-    this.api_post('group.add', data, 'group_save_response');
-  };
-
-  this.group_save_response = function(response)
-  {
-    if (!this.api_response(response))
-      return;
-
-    this.display_message('group.add.success');
-    this.command('group.list', {page: this.env.list_page});
-  };
-
 };
 
 // Add escape() method to RegExp object


commit 8b8c5d207d0b9ba1ea25319cd076efddbfd20635
Author: Aleksander Machniak <machniak at kolabsys.com>
Date:   Mon Apr 2 11:54:44 2012 +0200

    Use 'id' as unique identifier (and entrydn), so API client don't need to know what real attribute name is

diff --git a/lib/api/kolab_api_service_group.php b/lib/api/kolab_api_service_group.php
index 9ff1835..f008086 100644
--- a/lib/api/kolab_api_service_group.php
+++ b/lib/api/kolab_api_service_group.php
@@ -63,7 +63,7 @@ class kolab_api_service_group extends kolab_api_service
             foreach ($gta['form_fields'] as $key => $value) {
                 error_log("form field $key");
                 if (
-                        (!isset($postdata[$key]) || empty($postdata[$key]) &&
+                        (!isset($postdata[$key]) || empty($postdata[$key])) &&
                         (!array_key_exists('optional', $value) || !$value['optional'])
                     ) {
                     throw new Exception("Missing input value for $key", 345);
@@ -152,10 +152,14 @@ class kolab_api_service_group extends kolab_api_service
 
         $conf = Conf::get_instance();
 
+        // Group identifier
         $unique_attr = $conf->get('unique_attribute');
         if (!$unique_attr) {
             $unique_attr = 'nsuniqueid';
         }
+        $group_attributes[$unique_attr] = $postdata['id'];
+        unset($postdata['id']);
+
         // TODO: "rdn" is somewhat LDAP specific, but not used as something
         // LDAP specific...?
         $rdn_attr = $conf->get($type_str . '_group_name_attribute');
@@ -203,8 +207,6 @@ class kolab_api_service_group extends kolab_api_service
                     $group_attributes[$key] = $postdata[$key];
                 }
             }
-
-            $group_attributes[$unique_attr] = $postdata[$unique_attr];
         }
 
         $auth = Auth::get_instance();
@@ -212,7 +214,7 @@ class kolab_api_service_group extends kolab_api_service
 
         // Now that values have been re-generated where necessary, compare
         // the new group attributes to the original group attributes.
-        $_group = $auth->group_find_by_attribute(Array($unique_attr => $postdata[$unique_attr]));
+        $_group = $auth->group_find_by_attribute(array($unique_attr => $postdata['id']));
 
         if (!$_group) {
             console("Could not find group");
@@ -230,7 +232,6 @@ class kolab_api_service_group extends kolab_api_service
         }
 
         return false;
-
     }
 
     /**
@@ -244,60 +245,20 @@ class kolab_api_service_group extends kolab_api_service
     public function group_info($getdata, $postdata)
     {
         if (empty($getdata['group'])) {
-            return FALSE;
+            return false;
         }
 
         $auth   = Auth::get_instance();
-        $conf   = Conf::get_instance();
         $result = $auth->group_info($getdata['group']);
 
         // normalize result
-        $dn                = key($result);
-        $result            = $result[$dn];
-        $result['entrydn'] = $dn;
-
-        // add group type id to the result                                                                                                                       
-        $result['type_id'] = $this->object_type_id('group', $result['objectclass']);
-
-        // Search for attributes associated with the type_id that are not part
-        // of the results returned earlier. Example: nsrole / nsroledn / aci, etc.
-        if ($result['type_id']) {
-            $uta   = $this->object_type_attributes('group', $result['type_id']);
-            $attrs = array();
-
-            foreach ($uta as $field_type => $attributes) {
-                foreach ($attributes as $attribute => $data) {
-                    if (!array_key_exists($attribute, $result)) {
-                        $attrs[] = $attribute;
-                    }
-                }
-            }
-
-            // Insert the persistent, unique attribute
-            $unique_attr = $conf->get('unique_attribute');
-            if (!$unique_attr) {
-                $unique_attr = 'nsuniqueid';
-            }
-
-            if (!array_key_exists($unique_attr, $attrs)) {
-                $attrs[] = 'nsuniqueid';
-            }
-
-            if (!empty($attrs)) {
-                $attrs = $auth->get_attributes($result['entrydn'], $attrs);
-                if (!empty($attrs)) {
-                    $result = array_merge($result, $attrs);
-                }
-            }
-        }
-
-        //console($result);
+        $result = $this->parse_result_attributes('group', $result);
 
         if ($result) {
             return $result;
         }
 
-        return FALSE;
+        return false;
     }
 
     /**
diff --git a/lib/api/kolab_api_service_user.php b/lib/api/kolab_api_service_user.php
index 847646d..91c6c69 100644
--- a/lib/api/kolab_api_service_user.php
+++ b/lib/api/kolab_api_service_user.php
@@ -274,51 +274,10 @@ class kolab_api_service_user extends kolab_api_service
         }
 
         $auth   = Auth::get_instance();
-        $conf   = Conf::get_instance();
-        $user   = $getdata['user'];
-        $result = $auth->user_info($user);
+        $result = $auth->user_info($getdata['user']);
 
         // normalize result
-        $dn                = key($result);
-        $result            = $result[$dn];
-        $result['entrydn'] = $dn;
-
-        // add user type id to the result
-        $result['type_id'] = $this->object_type_id('user', $result['objectclass']);
-
-        // Search for attributes associated with the type_id that are not part
-        // of the results returned earlier. Example: nsrole / nsroledn / aci, etc.
-        if ($result['type_id']) {
-            $uta   = $this->object_type_attributes('user', $result['type_id']);
-            $attrs = array();
-
-            foreach ($uta as $field_type => $attributes) {
-                foreach ($attributes as $attribute => $data) {
-                    if (!array_key_exists($attribute, $result)) {
-                        $attrs[] = $attribute;
-                    }
-                }
-            }
-
-            // Insert the persistent, unique attribute
-            $unique_attr = $conf->get('unique_attribute');
-            if (!$unique_attr) {
-                $unique_attr = 'nsuniqueid';
-            }
-
-            if (!array_key_exists($unique_attr, $attrs)) {
-                $attrs[] = 'nsuniqueid';
-            }
-
-            if (!empty($attrs)) {
-                $attrs = $auth->get_attributes($result['entrydn'], $attrs);
-                if (!empty($attrs)) {
-                    $result = array_merge($result, $attrs);
-                }
-            }
-        }
-
-        console($result);
+        $result = $this->parse_result_attributes('group', $result); 
 
         if ($result) {
             return $result;
diff --git a/lib/client/kolab_client_task_group.php b/lib/client/kolab_client_task_group.php
index 0e04e4a..5816408 100644
--- a/lib/client/kolab_client_task_group.php
+++ b/lib/client/kolab_client_task_group.php
@@ -214,7 +214,7 @@ class kolab_client_task_group extends kolab_client_task
         // Prepare fields
         list($fields, $types, $type) = $this->form_prepare('group', $data);
 
-        $add_mode  = empty($data['entrydn']);
+        $add_mode  = empty($data['id']);
         $accttypes = array();
 
         foreach ($types as $idx => $elem) {
@@ -260,7 +260,7 @@ class kolab_client_task_group extends kolab_client_task
                 }
             }
             if (!empty($attr_name)) {
-                $result = $this->api->get('group.members_list', array('group' => $data['entrydn']));
+                $result = $this->api->get('group.members_list', array('group' => $data['id']));
                 $list   = (array) $result->get('list');
                 $data[$attr_name] = $this->parse_members($list);
             }
diff --git a/lib/client/kolab_client_task_user.php b/lib/client/kolab_client_task_user.php
index 1672136..3ee64bc 100644
--- a/lib/client/kolab_client_task_user.php
+++ b/lib/client/kolab_client_task_user.php
@@ -248,7 +248,7 @@ class kolab_client_task_user extends kolab_client_task
         // Prepare fields
         list($fields, $types, $type) = $this->form_prepare('user', $data, array('userpassword2'));
 
-        $add_mode  = empty($data['entrydn']);
+        $add_mode  = empty($data['id']);
         $accttypes = array();
 
         foreach ($types as $idx => $elem) {
diff --git a/lib/kolab_api_service.php b/lib/kolab_api_service.php
index 26d9e39..cf95c38 100644
--- a/lib/kolab_api_service.php
+++ b/lib/kolab_api_service.php
@@ -185,4 +185,55 @@ abstract class kolab_api_service
         return $this->cache['object_types'][$object_name] = $object_types;
     }
 
+    /**
+     * Detects object type ID for specified objectClass attribute value
+     *
+     * @param string $object_name  Name of the object (user, group, etc.)
+     * @param array  $attrs        Entry attributes
+     *
+     * @return array Entry attributes
+     */
+    protected function parse_result_attributes($object_name, $attrs = array())
+    {
+        if (empty($attrs) || !is_array($attrs)) {
+            return $attrs;
+        }
+
+        $conf  = Conf::get_instance();
+        $auth  = Auth::get_instance();
+        $dn    = key($attrs);                                                                                                                                 
+        $attrs = $attrs[$dn];
+
+        // Insert the persistent, unique attribute
+        $unique_attr = $conf->get('unique_attribute');
+        if (!$unique_attr) {
+            $unique_attr = 'nsuniqueid';
+        }
+
+        if (!array_key_exists($unique_attr, $attrs)) {
+            $u_attrs = $auth->get_attributes($dn, array($unique_attr));
+            if (!empty($u_attrs)) {
+                $attrs['id'] = $u_attrs[$unique_attr];
+            }
+        }
+
+        // add group type id to the result                                                                                                                       
+        $attrs['type_id'] = $this->object_type_id($object_name, $attrs['objectclass']);
+
+        // Search for attributes associated with the type_id that are not part
+        // of the results returned earlier. Example: nsrole / nsroledn / aci, etc.
+        if ($attrs['type_id']) {
+            $uta = $this->object_type_attributes($object_name, $attrs['type_id']);
+
+            foreach ((array)$uta as $field_type => $attributes) {
+                foreach ($attributes as $attribute => $data) {
+                    if (!array_key_exists($attribute, $attrs)) {
+                        $attrs[$attribute] = $data;
+                    }
+                }
+            }
+        }
+
+        return $attrs;
+    }
 }
diff --git a/lib/kolab_client_task.php b/lib/kolab_client_task.php
index 14fe71f..d24376a 100644
--- a/lib/kolab_client_task.php
+++ b/lib/kolab_client_task.php
@@ -691,7 +691,7 @@ class kolab_client_task
     {
         $types        = (array) $this->{$name . '_types'}();
         $form_id      = $attribs['id'];
-        $add_mode     = empty($data['entrydn']);
+        $add_mode     = empty($data['id']);
 
         $event_fields = array();
         $auto_fields  = array();
@@ -836,16 +836,12 @@ class kolab_client_task
                 'value'    => $name,
             );
 
+        // Add entry identifier
         if (!$add_mode) {
-            $unique_attr = $this->config->get('unique_attribute');
-            if (!$unique_attr) {
-                $unique_attr = 'nsuniqueid';
-            }
-
-            $fields[$unique_attr] = Array(
+            $fields['id'] = array(
                     'section'   => 'system',
                     'type'      => kolab_form::INPUT_HIDDEN,
-                    'value'     => $data[$unique_attr]
+                    'value'     => $data['id']
                 );
         }
 
@@ -961,7 +957,7 @@ class kolab_client_task
             'onclick' => $add_mode ? "kadm.{$name}_add()" : "kadm.{$name}_edit()",
         ));
 
-        if (!empty($data['entrydn'])) {
+        if (!empty($data['id'])) {
             $id = $data[$name];
             $form->add_button(array(
                 'value'   => kolab_html::escape($this->translate('delete.button')),





More information about the commits mailing list