2 commits - lib/api lib/kolab_api_service.php

Aleksander Machniak machniak at kolabsys.com
Wed Oct 24 13:14:57 CEST 2012


 lib/api/kolab_api_service_domain.php     |    9 +--
 lib/api/kolab_api_service_form_value.php |   90 ++++++++++++-------------------
 lib/api/kolab_api_service_role.php       |    9 ---
 lib/kolab_api_service.php                |   28 +++++++--
 4 files changed, 63 insertions(+), 73 deletions(-)

New commits:
commit 1f4ffa342d4d613fc5801b0f04d00a40fe422518
Author: Aleksander Machniak <alec at alec.pl>
Date:   Wed Oct 24 13:13:59 2012 +0200

    Limit OUs list by base_dn according to specified object type (#699)

diff --git a/lib/api/kolab_api_service_form_value.php b/lib/api/kolab_api_service_form_value.php
index 70aa8b9..fbd55fc 100644
--- a/lib/api/kolab_api_service_form_value.php
+++ b/lib/api/kolab_api_service_form_value.php
@@ -105,7 +105,7 @@ class kolab_api_service_form_value extends kolab_api_service
     {
         //console($postdata);
 
-        $attribs   = $this->object_type_attributes($postdata['object_type'], $postdata['type_id']);
+        $attribs   = $this->object_type_attributes($postdata['object_type'], $postdata['type_id'], true, $key_name);
         $attr_name = $postdata['attribute'];
         $result    = array(
             // return search value, so client can match response to request
@@ -117,6 +117,9 @@ class kolab_api_service_form_value extends kolab_api_service
             return $result;
         }
 
+        if ($key_name) {
+            $postdata['type_key'] = $key_name;
+        }
 
         $method_name = 'list_options_' . strtolower($attr_name) . '_' . strtolower($postdata['object_type']);
 
@@ -151,10 +154,14 @@ class kolab_api_service_form_value extends kolab_api_service
     public function select_options($getdata, $postdata)
     {
         //console("form_value.select_options postdata", $postdata);
-        $attribs    = $this->object_type_attributes($postdata['object_type'], $postdata['type_id']);
+        $attribs    = $this->object_type_attributes($postdata['object_type'], $postdata['type_id'], true, $key_name);
         $attributes = (array) $postdata['attributes'];
         $result     = array();
 
+        if ($key_name) {
+            $postdata['type_key'] = $key_name;
+        }
+
         foreach ($attributes as $attr_name) {
             if (empty($attr_name)) {
                 continue;
@@ -407,11 +414,6 @@ class kolab_api_service_form_value extends kolab_api_service
 
     private function generate_mail_resource($postdata, $attribs = array())
     {
-        $db = SQL::get_instance();
-        $result = $db->fetch_assoc($db->query("SELECT `key` FROM `resource_types` WHERE id = ?", $postdata['type_id']));
-
-        $object_type_key = $result['key'];
-
         if (isset($attribs['auto_form_fields']) && isset($attribs['auto_form_fields']['mail'])) {
             // Use Data Please
             foreach ($attribs['auto_form_fields']['mail']['data'] as $key) {
@@ -424,7 +426,7 @@ class kolab_api_service_form_value extends kolab_api_service
             //console("normalized resource data", $resourcedata);
 
             // TODO: Normalize $postdata
-            $mail_local  = 'resource-' . $object_type_key . '-' . strtolower($resourcedata['cn']);
+            $mail_local  = 'resource-' . $postdata['type_key'] . '-' . strtolower($resourcedata['cn']);
             $mail_domain = $_SESSION['user']->get_domain();
             $mail        = $mail_local . '@' . $mail_domain;
             $auth        = Auth::get_instance($_SESSION['user']->get_domain());
@@ -782,8 +784,15 @@ class kolab_api_service_form_value extends kolab_api_service
         $conf = Conf::get_instance();
 
         $unique_attr = $this->unique_attribute();
+        $object_type = $postdata['object_type'];
+        $object_key  = $postdata['type_key'];
 
-        $base_dn = $conf->get('user_base_dn');
+        if ($object_key && $object_type) {
+            $base_dn = $conf->get($object_key . '_' . $object_type . '_base_dn');
+        }
+        if (!$base_dn && $object_type) {
+            $base_dn = $conf->get($object_type . '_base_dn');
+        }
         if (!$base_dn) {
             $base_dn = $conf->get('base_dn');
         }
diff --git a/lib/kolab_api_service.php b/lib/kolab_api_service.php
index 3293bb5..7d9da8b 100644
--- a/lib/kolab_api_service.php
+++ b/lib/kolab_api_service.php
@@ -58,10 +58,11 @@ abstract class kolab_api_service
      * @param string $object_name  Name of the object (user, group, etc.)
      * @param int    $type_id      User type identifier
      * @param bool   $required     Throws exception on empty ID
+     * @param string $key_name     Reference to a variable which will be set to type key
      *
      * @return array User type attributes
      */
-    protected function object_type_attributes($object_name, $type_id, $required = true)
+    protected function object_type_attributes($object_name, $type_id, $required = true, &$key_name = null)
     {
         if (!$object_name || !in_array($object_name, $this->supported_types)) {
             return array();
@@ -101,6 +102,8 @@ abstract class kolab_api_service
             }
         }
 
+        $key_name = $object_types[$type_id]['key'];
+
         return $object_types[$type_id]['attributes'];
     }
 


commit 580d78c48af97999d7a2417bdae45658f94c5aca
Author: Aleksander Machniak <alec at alec.pl>
Date:   Wed Oct 24 12:53:17 2012 +0200

    Code improvements

diff --git a/lib/api/kolab_api_service_domain.php b/lib/api/kolab_api_service_domain.php
index 636c230..2d91358 100644
--- a/lib/api/kolab_api_service_domain.php
+++ b/lib/api/kolab_api_service_domain.php
@@ -145,14 +145,11 @@ class kolab_api_service_domain extends kolab_api_service
     public function domain_effective_rights($getdata, $postdata)
     {
         $auth = Auth::get_instance();
-        $conf = Conf::get_instance();
 
         if (!empty($getdata['domain'])) {
-            $entry_dn = $getdata['domain'];
-
-            $unique_attr = $conf->get('ldap', 'unique_attribute');
-
-            $domain = $auth->domain_find_by_attribute(array($unique_attr => $entry_dn));
+            $entry_dn    = $getdata['domain'];
+            $unique_attr = $this->unique_attribute();
+            $domain      = $auth->domain_find_by_attribute(array($unique_attr => $entry_dn));
 
             if (!empty($domain)) {
                 $entry_dn = key($domain);
diff --git a/lib/api/kolab_api_service_form_value.php b/lib/api/kolab_api_service_form_value.php
index 1443a1b..70aa8b9 100644
--- a/lib/api/kolab_api_service_form_value.php
+++ b/lib/api/kolab_api_service_form_value.php
@@ -260,15 +260,9 @@ class kolab_api_service_form_value extends kolab_api_service
                 }
             }
 
-            $auth = Auth::get_instance($_SESSION['user']->get_domain());
-            $conf = Conf::get_instance();
-
-            $unique_attr = $conf->get('unique_attribute');
-            if (!$unique_attr) {
-                $unique_attr = 'nsuniqueid';
-            }
-
-            $cn = $postdata['cn'];
+            $auth        = Auth::get_instance($_SESSION['user']->get_domain());
+            $unique_attr = $this->unique_attribute();
+            $cn          = $postdata['cn'];
 
             $x = 2;
             while (($resource_found = $auth->resource_find_by_attribute(array('cn' => $cn)))) {
@@ -430,19 +424,11 @@ class kolab_api_service_form_value extends kolab_api_service
             //console("normalized resource data", $resourcedata);
 
             // TODO: Normalize $postdata
-            $mail_local = 'resource-' . $object_type_key . '-' . strtolower($resourcedata['cn']);
+            $mail_local  = 'resource-' . $object_type_key . '-' . strtolower($resourcedata['cn']);
             $mail_domain = $_SESSION['user']->get_domain();
-            $mail = $mail_local . '@' . $mail_domain;
-
-            $orig_mail = $mail;
-
-            $auth = Auth::get_instance($_SESSION['user']->get_domain());
-            $conf = Conf::get_instance();
-
-            $unique_attr = $conf->get('unique_attribute');
-            if (!$unique_attr) {
-                $unique_attr = 'nsuniqueid';
-            }
+            $mail        = $mail_local . '@' . $mail_domain;
+            $auth        = Auth::get_instance($_SESSION['user']->get_domain());
+            $unique_attr = $this->unique_attribute();
 
             $x = 2;
             while (($resource_found = $auth->resource_find_by_attribute(array('mail' => $mail)))) {
@@ -461,8 +447,6 @@ class kolab_api_service_form_value extends kolab_api_service
             }
 
             return $mail;
-
-
         }
     }
 
@@ -594,13 +578,8 @@ class kolab_api_service_form_value extends kolab_api_service
 
             $orig_uid = $uid;
 
-            $auth = Auth::get_instance($_SESSION['user']->get_domain());
-            $conf = Conf::get_instance();
-
-            $unique_attr = $conf->get('unique_attribute');
-            if (!$unique_attr) {
-                $unique_attr = 'nsuniqueid';
-            }
+            $auth        = Auth::get_instance($_SESSION['user']->get_domain());
+            $unique_attr = $this->unique_attribute();
 
             $x = 2;
             while (($user_found = $auth->user_find_by_attribute(array('uid' => $uid)))) {
@@ -625,18 +604,17 @@ class kolab_api_service_form_value extends kolab_api_service
     private function generate_uidnumber($postdata, $attribs = array())
     {
         if (isset($attribs['auto_form_fields']) && isset($attribs['auto_form_fields']['uidnumber'])) {
-            $auth = Auth::get_instance($_SESSION['user']->get_domain());
-            $conf = Conf::get_instance();
-
-            $search = Array(
-                    'params' => Array(
-                            'objectclass' => Array(
-                                    'type' => 'exact',
-                                    'value' => 'posixaccount',
-                                ),
-                        ),
-                );
+            $search = array(
+                'params' => array(
+                    'objectclass' => array(
+                        'type'  => 'exact',
+                        'value' => 'posixaccount',
+                    ),
+                ),
+            );
 
+            $auth  = Auth::get_instance($_SESSION['user']->get_domain());
+            $conf  = Conf::get_instance();
             $users = $auth->list_users(NULL, Array('uidnumber'), $search);
 
             $highest_uidnumber = $conf->get('uidnumber_lower_barrier');
@@ -803,10 +781,7 @@ class kolab_api_service_form_value extends kolab_api_service
         $auth = Auth::get_instance();
         $conf = Conf::get_instance();
 
-        $unique_attr = $conf->get('unique_attribute');
-        if (!$unique_attr) {
-            $unique_attr = 'nsuniqueid';
-        }
+        $unique_attr = $this->unique_attribute();
 
         $base_dn = $conf->get('user_base_dn');
         if (!$base_dn) {
diff --git a/lib/api/kolab_api_service_role.php b/lib/api/kolab_api_service_role.php
index 18ebf19..cf1e339 100644
--- a/lib/api/kolab_api_service_role.php
+++ b/lib/api/kolab_api_service_role.php
@@ -137,16 +137,11 @@ class kolab_api_service_role extends kolab_api_service
     public function role_effective_rights($getdata, $postdata)
     {
         $auth = Auth::get_instance();
-        $conf = Conf::get_instance();
 
         // Roles are special in that they are ldapsubentries.
         if (!empty($getdata['role'])) {
-            $unique_attr = $conf->get('unique_attribute');
-            if (empty($unique_attr)) {
-                $unique_attr = 'nsuniqueid';
-            }
-
-            $role = $auth->role_find_by_attribute(Array($unique_attr => $getdata['role']));
+            $unique_attr = $this->unique_attribute();
+            $role        = $auth->role_find_by_attribute(Array($unique_attr => $getdata['role']));
 
             if (is_array($role) && count($role) == 1) {
                 $role_dn = key($role);
diff --git a/lib/kolab_api_service.php b/lib/kolab_api_service.php
index e90aa87..3293bb5 100644
--- a/lib/kolab_api_service.php
+++ b/lib/kolab_api_service.php
@@ -403,7 +403,6 @@ abstract class kolab_api_service
             return $attrs;
         }
 
-        $conf        = Conf::get_instance();
         $auth        = Auth::get_instance();
         $dn          = key($attrs);
         $attrs       = $attrs[$dn];
@@ -434,11 +433,7 @@ abstract class kolab_api_service
         }
 
         // Insert the persistent, unique attribute
-        $unique_attr = $conf->get('unique_attribute');
-        if (!$unique_attr) {
-            $unique_attr = 'nsuniqueid';
-        }
-
+        $unique_attr = $this->unique_attribute();
         if (!array_key_exists($unique_attr, $attrs)) {
             $extra_attrs[] = $unique_attr;
         }
@@ -458,4 +453,20 @@ abstract class kolab_api_service
         return $attrs;
     }
 
+    /**
+     * Returns name of unique attribute
+     *
+     * @return string Unique attribute name
+     */
+    protected function unique_attribute()
+    {
+        $conf        = Conf::get_instance();
+        $unique_attr = $conf->get('unique_attribute');
+
+        if (!$unique_attr) {
+            $unique_attr = 'nsuniqueid';
+        }
+
+        return $unique_attr;
+    }
 }





More information about the commits mailing list