lib/api lib/client lib/kolab_api_service.php lib/locale public_html/js

Aleksander Machniak machniak at kolabsys.com
Wed Mar 14 10:01:03 CET 2012


 lib/api/kolab_api_service_form_value.php |   82 +++++++++++++++++++------------
 lib/api/kolab_api_service_group.php      |   12 ----
 lib/api/kolab_api_service_user.php       |    6 +-
 lib/client/kolab_client_task_user.php    |    3 -
 lib/kolab_api_service.php                |   30 +++++++++++
 lib/locale/en_US.api.php                 |    6 ++
 public_html/js/kolab_admin.js            |    4 -
 7 files changed, 97 insertions(+), 46 deletions(-)

New commits:
commit fa7994160bb410c1e8f35a0e91883b43b2ab9cfa
Author: Aleksander Machniak <alec at alec.pl>
Date:   Wed Mar 14 10:00:10 2012 +0100

    Unified form_value.generate API method (#625)

diff --git a/lib/api/kolab_api_service_form_value.php b/lib/api/kolab_api_service_form_value.php
index d21a23f..0e481a4 100644
--- a/lib/api/kolab_api_service_form_value.php
+++ b/lib/api/kolab_api_service_form_value.php
@@ -24,7 +24,7 @@
 */
 
 /**
- *
+ * Service providing functionality related to HTML forms generation/validation.
  */
 class kolab_api_service_form_value extends kolab_api_service
 {
@@ -32,23 +32,51 @@ class kolab_api_service_form_value extends kolab_api_service
     public function capabilities($domain)
     {
         return array(
-            'generate_cn' => 'w',
-            'generate_displayname' => 'w',
-            'generate_mail' => 'w',
-            'generate_password' => 'r',
-            'generate_uid' => 'w',
-            'generate_userpassword' => 'r',
-//            'info' => 'r',
+            'generate' => 'r',
         );
     }
 
-    public function generate_cn($getdata, $postdata)
+    /**
+     * Generation of auto-filled field values.
+     *
+     * @param array $getdata   GET parameters
+     * @param array $postdata  POST parameters. Required parameters:
+     *                         - attribute: attribute name
+     *                         - user_type_id or group_type_id: Type identifier
+     *
+     * @return array Response with attribute name as a key
+     */
+    public function generate($getdata, $postdata)
     {
-        $uta = $this->user_type_attributes($postdata['user_type_id']);
+        if (empty($postdata['attribute'])) {
+            throw new Exception($this->controller->translate('form_value.noattribute'), 35);
+        }
+
+        $method_name = 'generate_' . strtolower($postdata['attribute']);
+
+        if (!method_exists($this, $method_name)) {
+            throw new Exception($this->controller->translate('form_value.unknownattribute'), 36);
+        }
+
+        if (isset($postdata['user_type_id'])) {
+            $attribs = $this->user_type_attributes($postdata['user_type_id']);
+        }
+        else if (isset($postdata['group_type_id'])) {
+            $attribs = $this->group_type_attributes($postdata['group_type_id']);
+        }
+        else {
+            $attribs = array();
+        }
 
-        if (isset($uta['auto_form_fields']) && isset($uta['auto_form_fields']['cn'])) {
+        return $this->{$method_name}($postdata, $attribs);
+    }
+
+
+    private function generate_cn($postdata, $attribs = array())
+    {
+        if (isset($attribs['auto_form_fields']) && isset($attribs['auto_form_fields']['cn'])) {
             // Use Data Please
-            foreach ($uta['auto_form_fields']['cn']['data'] as $key) {
+            foreach ($attribs['auto_form_fields']['cn']['data'] as $key) {
                 if (!isset($postdata[$key])) {
                     throw new Exception("Key not set: " . $key, 12356);
                 }
@@ -60,13 +88,11 @@ class kolab_api_service_form_value extends kolab_api_service
         }
     }
 
-    public function generate_displayname($getdata, $postdata)
+    private function generate_displayname($postdata, $attribs = array())
     {
-        $uta = $this->user_type_attributes($postdata['user_type_id']);
-
-        if (isset($uta['auto_form_fields']) && isset($uta['auto_form_fields']['displayname'])) {
+        if (isset($attribs['auto_form_fields']) && isset($attribs['auto_form_fields']['displayname'])) {
             // Use Data Please
-            foreach ($uta['auto_form_fields']['displayname']['data'] as $key) {
+            foreach ($attribs['auto_form_fields']['displayname']['data'] as $key) {
                 if (!isset($postdata[$key])) {
                     throw new Exception("Key not set: " . $key, 12356);
                 }
@@ -81,13 +107,11 @@ class kolab_api_service_form_value extends kolab_api_service
         }
     }
 
-    public function generate_mail($getdata, $postdata)
+    private function generate_mail($postdata, $attribs = array())
     {
-        $uta = $this->user_type_attributes($postdata['user_type_id']);
-
-        if (isset($uta['auto_form_fields']) && isset($uta['auto_form_fields']['mail'])) {
+        if (isset($attribs['auto_form_fields']) && isset($attribs['auto_form_fields']['mail'])) {
             // Use Data Please
-            foreach ($uta['auto_form_fields']['mail']['data'] as $key) {
+            foreach ($attribs['auto_form_fields']['mail']['data'] as $key) {
                 if (!isset($postdata[$key])) {
                     throw new Exception("Key not set: " . $key, 12356);
                 }
@@ -119,20 +143,18 @@ class kolab_api_service_form_value extends kolab_api_service
         }
     }
 
-    public function generate_password($getdata, $postdata)
+    private function generate_password($postdata, $attribs = array())
     {
         exec("head -c 200 /dev/urandom | tr -dc _A-Z-a-z-0-9 | head -c15", $userpassword_plain);
         $userpassword_plain = $userpassword_plain[0];
         return array('password' => $userpassword_plain);
     }
 
-    public function generate_uid($getdata, $postdata)
+    private function generate_uid($postdata, $attribs = array())
     {
-        $uta = $this->user_type_attributes($postdata['user_type_id']);
-
-        if (isset($uta['auto_form_fields']) && isset($uta['auto_form_fields']['uid'])) {
+        if (isset($attribs['auto_form_fields']) && isset($attribs['auto_form_fields']['uid'])) {
             // Use Data Please
-            foreach ($uta['auto_form_fields']['uid']['data'] as $key) {
+            foreach ($attribs['auto_form_fields']['uid']['data'] as $key) {
                 if (!isset($postdata[$key])) {
                     throw new Exception("Key not set: " . $key, 12356);
                 }
@@ -156,9 +178,9 @@ class kolab_api_service_form_value extends kolab_api_service
         }
     }
 
-    public function generate_userpassword($getdata, $postdata)
+    private function generate_userpassword($postdata, $attribs = array())
     {
-        $password = $this->generate_password($getdata, $postdata);
+        $password = $this->generate_password($getdata, $postdata, $attribs);
         return array('userpassword' => $password['password']);
     }
 
diff --git a/lib/api/kolab_api_service_group.php b/lib/api/kolab_api_service_group.php
index 9bc1bc1..7496202 100644
--- a/lib/api/kolab_api_service_group.php
+++ b/lib/api/kolab_api_service_group.php
@@ -40,16 +40,8 @@ class kolab_api_service_group extends kolab_api_service
 
     public function group_add($getdata, $postdata)
     {
-        if (!isset($postdata['group_type_id'])) {
-            throw new Exception("No group type ID specified", 346781);
-        }
-
-        $sql_result = $this->db->query("SELECT attributes FROM group_types WHERE id = ?", $postdata['group_type_id']);
-        $group_type = $this->db->fetch_assoc($sql_result);
-
-        $gta = json_decode(unserialize($group_type['attributes']), true);
-
-        $group_attributes = Array();
+        $gta = $this->user_type_attributes($postdata['group_type_id']);
+        $group_attributes = array();
 
         if (isset($gta['form_fields'])) {
             foreach ($gta['form_fields'] as $key => $value) {
diff --git a/lib/api/kolab_api_service_user.php b/lib/api/kolab_api_service_user.php
index 4c0b53a..903771e 100644
--- a/lib/api/kolab_api_service_user.php
+++ b/lib/api/kolab_api_service_user.php
@@ -62,9 +62,9 @@ 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])) {
-                    $method         = 'generate_' . $key;
-                    $res            = $form_service->$method($getdata, $postdata);
-                    $postdata[$key] = $res[$key];
+                    $postdata['attribute'] = $key;
+                    $res                   = $form_service->generate($getdata, $postdata);
+                    $postdata[$key]        = $res[$key];
                 }
                 $user_attributes[$key] = $postdata[$key];
             }
diff --git a/lib/client/kolab_client_task_user.php b/lib/client/kolab_client_task_user.php
index 7b21aef..3fdbb66 100644
--- a/lib/client/kolab_client_task_user.php
+++ b/lib/client/kolab_client_task_user.php
@@ -469,7 +469,8 @@ class kolab_client_task_user extends kolab_client_task
         if ($add_mode) {
             if (empty($data['userpassword'])) {
                 // Pre-populate password fields
-                $pass = $this->api->get('form_value.generate_userpassword');
+                $post = array('attribute' => 'userpassword');
+                $pass = $this->api->post('form_value.generate', null, $post);
                 $data['userpassword'] = $data['userpassword2'] = $pass->get('userpassword');
             }
 
diff --git a/lib/kolab_api_service.php b/lib/kolab_api_service.php
index 3612c69..5d144ff 100644
--- a/lib/kolab_api_service.php
+++ b/lib/kolab_api_service.php
@@ -77,4 +77,34 @@ abstract class kolab_api_service
         return $uta;
     }
 
+    /**
+     * Returns attributes of specified group type.
+     *
+     * @param int  $type_id  Group type identifier
+     * @param bool $required Throws exception on empty ID
+     *
+     * @return array Group type attributes
+     */
+    protected function group_type_attributes($type_id, $required = true)
+    {
+        if (empty($type_id)) {
+            if ($required) {
+                throw new Exception($this->controller->translate('group.notypeid'), 34);
+            }
+
+            return array();
+        }
+
+        $sql_result = $this->db->query("SELECT attributes FROM group_types WHERE id = ?", $type_id);
+        $group_type = $this->db->fetch_assoc($sql_result);
+
+        if (empty($group_type)) {
+            throw new Exception($this->controller->translate('group.invalidtypeid'), 35);
+        }
+
+        $uta = json_decode(unserialize($group_type['attributes']), true);
+
+        return $uta;
+    }
+
 }
diff --git a/lib/locale/en_US.api.php b/lib/locale/en_US.api.php
index fba4687..65a3896 100644
--- a/lib/locale/en_US.api.php
+++ b/lib/locale/en_US.api.php
@@ -2,3 +2,9 @@
 
 $LANG['user.notypeid'] = 'No user type ID specified!';
 $LANG['user.invalidtypeid'] = 'Invalid user type ID!';
+
+$LANG['group.notypeid'] = 'No group type ID specified!';
+$LANG['group.invalidtypeid'] = 'Invalid group type ID!';
+
+$LANG['form_value.noattribute'] = 'No attribute name specified!';
+$LANG['form_value.unknownattribute'] = 'Unknown attribute!';
diff --git a/public_html/js/kolab_admin.js b/public_html/js/kolab_admin.js
index 29801ef..d0c7361 100644
--- a/public_html/js/kolab_admin.js
+++ b/public_html/js/kolab_admin.js
@@ -451,14 +451,14 @@ function kolab_admin()
       if (!e)
         continue;
 
-      data = {user_type_id: type_id};
+      data = {user_type_id: type_id, attribute: name};
       for (j=0; j<e.data.length; j++) {
         elem_name = e.data[j];
         if (elem = $('[name="'+elem_name+'"]', form))
           data[elem_name] = elem.val();
       }
 
-      this.api_post('form_value.generate_'+name, data, 'form_value_response');
+      this.api_post('form_value.generate', data, 'form_value_response');
     }
 
     this.set_busy(false);





More information about the commits mailing list