summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/api/kolab_api_service_form_value.php36
-rw-r--r--lib/api/kolab_api_service_user.php9
-rw-r--r--lib/kolab_api_service.php35
-rw-r--r--lib/locale/en_US.api.php1
4 files changed, 41 insertions, 40 deletions
diff --git a/lib/api/kolab_api_service_form_value.php b/lib/api/kolab_api_service_form_value.php
index 0d16bdb..3b9270a 100644
--- a/lib/api/kolab_api_service_form_value.php
+++ b/lib/api/kolab_api_service_form_value.php
@@ -21,14 +21,7 @@ class kolab_api_service_form_value extends kolab_api_service
public function generate_cn($getdata, $postdata)
{
- if (!isset($postdata['user_type_id'])) {
- throw new Exception("No user type ID specified", 34);
- }
-
- $sql_result = $this->db->query("SELECT attributes FROM user_types WHERE id = ?", $postdata['user_type_id']);
- $user_type = $this->db->fetch_assoc($sql_result);
-
- $uta = json_decode(unserialize($user_type['attributes']), true);
+ $uta = $this->user_type_attributes($postdata['user_type_id']);
if (isset($uta['auto_form_fields']) && isset($uta['auto_form_fields']['cn'])) {
// Use Data Please
@@ -44,14 +37,7 @@ class kolab_api_service_form_value extends kolab_api_service
public function generate_displayname($getdata, $postdata)
{
- if (!isset($postdata['user_type_id'])) {
- throw new Exception("No user type ID specified", 34);
- }
-
- $sql_result = $this->db->query("SELECT attributes FROM user_types WHERE id = ?", $postdata['user_type_id']);
- $user_type = $this->db->fetch_assoc($sql_result);
-
- $uta = json_decode(unserialize($user_type['attributes']), true);
+ $uta = $this->user_type_attributes($postdata['user_type_id']);
if (isset($uta['auto_form_fields']) && isset($uta['auto_form_fields']['displayname'])) {
// Use Data Please
@@ -68,14 +54,7 @@ class kolab_api_service_form_value extends kolab_api_service
public function generate_mail($getdata, $postdata)
{
- if (!isset($postdata['user_type_id'])) {
- throw new Exception("No user type ID specified", 34);
- }
-
- $sql_result = $this->db->query("SELECT attributes FROM user_types WHERE id = ?", $postdata['user_type_id']);
- $user_type = $this->db->fetch_assoc($sql_result);
-
- $uta = json_decode(unserialize($user_type['attributes']), true);
+ $uta = $this->user_type_attributes($postdata['user_type_id']);
if (isset($uta['auto_form_fields']) && isset($uta['auto_form_fields']['mail'])) {
// Use Data Please
@@ -118,14 +97,7 @@ class kolab_api_service_form_value extends kolab_api_service
public function generate_uid($getdata, $postdata)
{
- if (!isset($postdata['user_type_id'])) {
- throw new Exception("No user type ID specified", 34);
- }
-
- $sql_result = $this->db->query("SELECT attributes FROM user_types WHERE id = ?", $postdata['user_type_id']);
- $user_type = $this->db->fetch_assoc($sql_result);
-
- $uta = json_decode(unserialize($user_type['attributes']), true);
+ $uta = $this->user_type_attributes($postdata['user_type_id']);
if (isset($uta['auto_form_fields']) && isset($uta['auto_form_fields']['uid'])) {
// Use Data Please
diff --git a/lib/api/kolab_api_service_user.php b/lib/api/kolab_api_service_user.php
index b8d46a2..e6c4049 100644
--- a/lib/api/kolab_api_service_user.php
+++ b/lib/api/kolab_api_service_user.php
@@ -21,14 +21,7 @@ class kolab_api_service_user extends kolab_api_service
public function user_add($getdata, $postdata)
{
- if (!isset($postdata['user_type_id'])) {
- throw new Exception($this->controller->translate('user.notypeid'), 346781);
- }
-
- $sql_result = $this->db->query("SELECT attributes FROM user_types WHERE id = ?", $postdata['user_type_id']);
- $user_type = $this->db->fetch_assoc($sql_result);
-
- $uta = json_decode(unserialize($user_type['attributes']), true);
+ $uta = $this->user_type_attributes($postdata['user_type_id']);
$user_attributes = array();
diff --git a/lib/kolab_api_service.php b/lib/kolab_api_service.php
index 46423a8..06ebbb9 100644
--- a/lib/kolab_api_service.php
+++ b/lib/kolab_api_service.php
@@ -10,6 +10,11 @@ abstract class kolab_api_service
protected $controller;
protected $db;
+ /**
+ * Class constructor.
+ *
+ * @param kolab_api_controller Controller
+ */
public function __construct($ctrl)
{
$this->db = SQL::get_instance();
@@ -21,4 +26,34 @@ abstract class kolab_api_service
*/
abstract public function capabilities($domain);
+ /**
+ * Returns attributes of specified user type.
+ *
+ * @param int $type_id User type identifier
+ * @param bool $required Throws exception on empty ID
+ *
+ * @return array User type attributes
+ */
+ protected function user_type_attributes($type_id, $required = true)
+ {
+ if (empty($user_id)) {
+ if ($required) {
+ throw new Exception($this->controller->translate('user.notypeid'), 34);
+ }
+
+ return array();
+ }
+
+ $sql_result = $this->db->query("SELECT attributes FROM user_types WHERE id = ?", $type_id);
+ $user_type = $this->db->fetch_assoc($sql_result);
+
+ if (empty($user_type)) {
+ throw new Exception($this->controller->translate('user.invalidtypeid'), 35);
+ }
+
+ $uta = json_decode(unserialize($user_type['attributes']), true);
+
+ return $uta;
+ }
+
}
diff --git a/lib/locale/en_US.api.php b/lib/locale/en_US.api.php
index 4e43a72..fba4687 100644
--- a/lib/locale/en_US.api.php
+++ b/lib/locale/en_US.api.php
@@ -1,3 +1,4 @@
<?php
$LANG['user.notypeid'] = 'No user type ID specified!';
+$LANG['user.invalidtypeid'] = 'Invalid user type ID!';