summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAleksander Machniak <machniak@kolabsys.com>2012-03-14 14:55:09 +0100
committerAleksander Machniak <machniak@kolabsys.com>2012-03-14 14:55:09 +0100
commit4872803486f298b98fb282c83c5c280201a0424c (patch)
tree8a7b605082547fce4f1491bd619d6584d5d78b0c /lib
parent5b95b56165eddd823b5e3d8e5ee1489d4aa43c5c (diff)
downloadwebadmin-4872803486f298b98fb282c83c5c280201a0424c.tar.gz
Move user type detection to service class, return type id in user.info result
Diffstat (limited to 'lib')
-rw-r--r--lib/api/kolab_api_service_user.php7
-rw-r--r--lib/api/kolab_api_service_user_types.php18
-rw-r--r--lib/client/kolab_client_task_user.php48
-rw-r--r--lib/kolab_api_service.php73
4 files changed, 80 insertions, 66 deletions
diff --git a/lib/api/kolab_api_service_user.php b/lib/api/kolab_api_service_user.php
index 903771e..32f6b22 100644
--- a/lib/api/kolab_api_service_user.php
+++ b/lib/api/kolab_api_service_user.php
@@ -114,7 +114,12 @@ class kolab_api_service_user extends kolab_api_service
}
$auth = Auth::get_instance();
- $result = $auth->user_info($getdata['user']);
+ $user = $getdata['user'];
+ $result = $auth->user_info($user);
+
+ // add user type id to the result
+ $objectclass = $result[$user]['objectclass'];
+ $result[$user]['user_type_id'] = $this->user_type_id($objectclass);
if ($result) {
return $result;
diff --git a/lib/api/kolab_api_service_user_types.php b/lib/api/kolab_api_service_user_types.php
index 1c5a6c4..164e872 100644
--- a/lib/api/kolab_api_service_user_types.php
+++ b/lib/api/kolab_api_service_user_types.php
@@ -37,23 +37,7 @@ class kolab_api_service_user_types extends kolab_api_service
public function user_types_list($get, $post)
{
- $sql_result = $this->db->query("SELECT * FROM user_types");
- $user_types = array();
-
- while ($row = $this->db->fetch_assoc($sql_result)) {
- $user_types[$row['id']] = array();
-
- foreach ($row as $key => $value) {
- if ($key != "id") {
- if ($key == "attributes") {
- $user_types[$row['id']][$key] = json_decode(unserialize($value), true);
- }
- else {
- $user_types[$row['id']][$key] = $value;
- }
- }
- }
- }
+ $user_types = $this->user_types();
return array(
'list' => $user_types,
diff --git a/lib/client/kolab_client_task_user.php b/lib/client/kolab_client_task_user.php
index 9fbffa4..c8a6c2a 100644
--- a/lib/client/kolab_client_task_user.php
+++ b/lib/client/kolab_client_task_user.php
@@ -198,57 +198,9 @@ class kolab_client_task_user extends kolab_client_task
$add_mode = empty($data['user']);
$accttypes = array();
- $result = Array();
- $current_user_type_score = -1;
-
- for ($i=0; $i < count($data['objectclass']); $i++) {
- $data['objectclass'][$i] = strtolower($data['objectclass'][$i]);
- }
-
- $data_ocs = $data['objectclass'];
-
- console("Data objectclasses (i.e. \$data_ocs): " . implode(", ", $data_ocs));
-
foreach ($utypes as $idx => $elem) {
-
$accttypes[$idx] = array('value' => $idx, 'content' => $elem['name']);
-
- // Unless we're in add mode, detect the user type.
- if (!$add_mode) {
-
- $ref_ocs = $elem['attributes']['fields']['objectclass'];
-
- console("Reference objectclasses (\$ref_ocs for " . $elem['key'] . "): " . implode(", ", $ref_ocs));
-
- // Eliminate the duplicates between the $data_ocs and $ref_ocs
- $_data_ocs = array_diff($data_ocs, $ref_ocs);
- $_ref_ocs = array_diff($ref_ocs, $data_ocs);
-
- console("\$data_ocs not in \$ref_ocs (" . $elem['key'] . "): " . implode(", ", $_data_ocs));
- console("\$ref_ocs not in \$data_ocs (" . $elem['key'] . "): " . implode(", ", $_ref_ocs));
-
- $differences = count($_data_ocs) + count($_ref_ocs);
- $commonalities = count($data_ocs) - $differences;
-
- console("Commonalities/differences (" . $elem['key'] . "): " . $commonalities . " / " . $differences);
-
- if ($differences > 0) {
- $user_type_score = ($commonalities / $differences);
- } else {
- $user_type_score = $commonalities;
- }
-
- console("Score for user type " . $elem['name'] . ": " . $user_type_score);
-
- if ($user_type_score > $current_user_type_score) {
- console("Score for user type " . $elem['name'] . " is greater than score for user type " . $current_user_type['name']);
- $current_user_type = $elem;
- $current_user_type_score = $user_type_score;
- }
- }
}
-
- console("A little bird tells me we need to use user_type_name " . $current_user_type['name']);
/*
$fields = array(
'personal' => array(
diff --git a/lib/kolab_api_service.php b/lib/kolab_api_service.php
index 5d144ff..8630cbe 100644
--- a/lib/kolab_api_service.php
+++ b/lib/kolab_api_service.php
@@ -107,4 +107,77 @@ abstract class kolab_api_service
return $uta;
}
+ /**
+ * Detects user type ID for specified objectClass attribute value
+ *
+ * @param array $object_class Value of objectClass attribute
+ *
+ * @return int User type identifier
+ */
+ protected function user_type_id($object_class)
+ {
+ if (empty($object_class)) {
+ return null;
+ }
+
+ $object_class = array_map('strtolower', $object_class);
+ $user_types = $this->user_types();
+ $type_score = -1;
+ $type_id = null;
+
+ console("Data objectClasses: " . implode(", ", $object_class));
+
+ foreach ($user_types as $idx => $elem) {
+ $ref_class = $elem['attributes']['fields']['objectclass'];
+
+ console("Reference objectclasses for " . $elem['key'] . ": " . implode(", ", $ref_class));
+
+ // Eliminate the duplicates between the $data_ocs and $ref_ocs
+ $_object_class = array_diff($object_class, $ref_class);
+ $_ref_class = array_diff($ref_class, $object_class);
+
+ $differences = count($_object_class) + count($_ref_class);
+ $commonalities = count($object_class) - $differences;
+ $elem_score = $differences > 0 ? ($commonalities / $differences) : $commonalities;
+
+// console("\$object_class not in \$ref_class (" . $elem['key'] . "): " . implode(", ", $_object_class));
+// console("\$ref_class not in \$object_class (" . $elem['key'] . "): " . implode(", ", $_ref_class));
+ console("Score for user type " . $elem['name'] . ": " . $elem_score . "(" . $commonalities . "/" . $differences . ")");
+
+ if ($elem_score > $type_score) {
+ $type_id = $idx;
+ $type_score = $elem_score;
+ }
+ }
+
+ return $type_id;
+ }
+
+ /**
+ * Returns user types definitions.
+ *
+ * @return array User types.
+ */
+ protected function user_types()
+ {
+ $sql_result = $this->db->query("SELECT * FROM user_types");
+ $user_types = array();
+
+ while ($row = $this->db->fetch_assoc($sql_result)) {
+ $user_types[$row['id']] = array();
+
+ foreach ($row as $key => $value) {
+ if ($key != "id") {
+ if ($key == "attributes") {
+ $user_types[$row['id']][$key] = json_decode(unserialize($value), true);
+ }
+ else {
+ $user_types[$row['id']][$key] = $value;
+ }
+ }
+ }
+ }
+
+ return $user_types;
+ }
}