diff options
author | Aleksander Machniak <alec@alec.pl> | 2012-03-14 10:00:10 +0100 |
---|---|---|
committer | Aleksander Machniak <alec@alec.pl> | 2012-03-14 10:00:10 +0100 |
commit | fa7994160bb410c1e8f35a0e91883b43b2ab9cfa (patch) | |
tree | 50ea974df3ef8fc7f0c3f227ebb8b850adaacd26 /lib/api | |
parent | 74a8a69bde41236851c099fd87cfd1e84e7acce6 (diff) | |
download | webadmin-fa7994160bb410c1e8f35a0e91883b43b2ab9cfa.tar.gz |
Unified form_value.generate API method (#625)
Diffstat (limited to 'lib/api')
-rw-r--r-- | lib/api/kolab_api_service_form_value.php | 82 | ||||
-rw-r--r-- | lib/api/kolab_api_service_group.php | 12 | ||||
-rw-r--r-- | lib/api/kolab_api_service_user.php | 6 |
3 files changed, 57 insertions, 43 deletions
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]; } |