From 462b589bd11e1bab55c0cb4a6dcb37e19d44093f Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Thu, 15 Mar 2012 09:56:41 +0100 Subject: Improve form_value.generate to support many attributes at a time --- lib/api/kolab_api_service_form_value.php | 60 +++++++++++++++++--------------- lib/api/kolab_api_service_user.php | 6 ++-- lib/client/kolab_client_task_user.php | 8 +++-- lib/kolab_api_controller.php | 2 -- public_html/js/kolab_admin.js | 12 +++---- 5 files changed, 47 insertions(+), 41 deletions(-) diff --git a/lib/api/kolab_api_service_form_value.php b/lib/api/kolab_api_service_form_value.php index c778e5e..fa7ee28 100644 --- a/lib/api/kolab_api_service_form_value.php +++ b/lib/api/kolab_api_service_form_value.php @@ -48,16 +48,6 @@ class kolab_api_service_form_value extends kolab_api_service */ public function generate($getdata, $postdata) { - 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']); } @@ -68,7 +58,24 @@ class kolab_api_service_form_value extends kolab_api_service $attribs = array(); } - return $this->{$method_name}($postdata, $attribs); + $attributes = (array) $postdata['attributes']; + $result = array(); + + foreach ($attributes as $attr_name) { + if (empty($attr_name)) { + continue; + } + + $method_name = 'generate_' . strtolower($attr_name); + + if (!method_exists($this, $method_name)) { + continue; + } + + $result[$attr_name] = $this->{$method_name}($postdata, $attribs); + } + + return $result; } @@ -84,7 +91,7 @@ class kolab_api_service_form_value extends kolab_api_service $cn = trim($postdata['givenname'] . " " . $postdata['sn']); - return array("cn" => $cn); + return $cn; } } @@ -103,7 +110,7 @@ class kolab_api_service_form_value extends kolab_api_service $displayname = $postdata['sn'] . ", " . $displayname; } - return array("displayname" => $displayname); + return $displayname; } } @@ -114,7 +121,7 @@ class kolab_api_service_form_value extends kolab_api_service // TODO: Take a policy to use a known group ID, a known group (by name?) // and/or create user private groups. - return array('gidnumber' => 500); + return 500; } } @@ -143,8 +150,7 @@ class kolab_api_service_form_value extends kolab_api_service } // TODO: Home directory base path from configuration? - - return array('homedirectory' => '/home/'.$uid); + return '/home/' . $uid; } } @@ -181,7 +187,7 @@ class kolab_api_service_form_value extends kolab_api_service $x++; } - return array('mail' => $mail); + return $mail; } } @@ -189,15 +195,19 @@ class kolab_api_service_form_value extends kolab_api_service { if (isset($attribs['auto_form_fields']) && isset($attribs['auto_form_fields']['uidnumber'])) { // This value is determined by the Kolab Daemon - return array('mailhost' => ''); + return ''; } } 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); + return $userpassword_plain[0]; + } + + private function generate_userpassword($postdata, $attribs = array()) + { + return $this->generate_password($postdata, $attribs); } private function generate_uid($postdata, $attribs = array()) @@ -224,7 +234,7 @@ class kolab_api_service_form_value extends kolab_api_service $x++; } - return array('uid' => $uid); + return $uid; } } @@ -235,14 +245,8 @@ class kolab_api_service_form_value extends kolab_api_service // TODO: Actually poll $auth for users with a uidNumber set, and take the next one. - return array('uidnumber' => 500); + return 500; } } - private function generate_userpassword($postdata, $attribs = array()) - { - $password = $this->generate_password($getdata, $postdata, $attribs); - return array('userpassword' => $password['password']); - } - } diff --git a/lib/api/kolab_api_service_user.php b/lib/api/kolab_api_service_user.php index 32f6b22..d8e79e7 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])) { - $postdata['attribute'] = $key; - $res = $form_service->generate($getdata, $postdata); - $postdata[$key] = $res[$key]; + $postdata['attributes'] = array($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 b18a2b3..51ced0c 100644 --- a/lib/client/kolab_client_task_user.php +++ b/lib/client/kolab_client_task_user.php @@ -476,7 +476,7 @@ class kolab_client_task_user extends kolab_client_task 'maxlength' => 50, ); - if (!empty($field['data'])) { + if (is_array($field) && !empty($field['data'])) { foreach ($field['data'] as $fd) { $event_fields[$fd][] = $idx; } @@ -505,7 +505,11 @@ class kolab_client_task_user extends kolab_client_task if (!empty($field['values'])) { $_fields[$idx]['options'] = array_combine($field['values'], $field['values']); } + else { + $_fields[$idx]['options'] = array(''); + } break; + default: $_fields[$idx]['type'] = kolab_form::INPUT_TEXT; if (isset($field['maxlength'])) { @@ -545,7 +549,7 @@ class kolab_client_task_user extends kolab_client_task if ($add_mode) { if (empty($data['userpassword'])) { // Pre-populate password fields - $post = array('attribute' => 'userpassword'); + $post = array('attributes' => array('userpassword')); $pass = $this->api->post('form_value.generate', null, $post); $data['userpassword'] = $pass->get('userpassword'); } diff --git a/lib/kolab_api_controller.php b/lib/kolab_api_controller.php index b216a0e..cc23849 100644 --- a/lib/kolab_api_controller.php +++ b/lib/kolab_api_controller.php @@ -84,8 +84,6 @@ class kolab_api_controller */ public function get_service($service) { - error_log($service); - // we are the system! if ($service == 'system') { return $this; diff --git a/public_html/js/kolab_admin.js b/public_html/js/kolab_admin.js index d0c7361..08b6f31 100644 --- a/public_html/js/kolab_admin.js +++ b/public_html/js/kolab_admin.js @@ -438,9 +438,10 @@ function kolab_admin() this.form_value_change = function(events) { - var i, j, data, e, elem, name, elem_name, + var i, j, e, elem, name, elem_name, form = $('#'+this.env.form_id), - type_id = $('[name="user_type_id"]', form).val(); + type_id = $('[name="user_type_id"]', form).val(), + data = {user_type_id: type_id, attributes: []}; this.set_busy(true, 'loading'); @@ -451,16 +452,15 @@ function kolab_admin() if (!e) continue; - data = {user_type_id: type_id, attribute: name}; + data.attributes.push(name); for (j=0; j