lib/api lib/Auth lib/Auth.php

Aleksander Machniak machniak at kolabsys.com
Thu Mar 22 10:09:34 CET 2012


 lib/Auth.php                         |    4 -
 lib/Auth/LDAP.php                    |  116 ++++++++++++++++++++++-------------
 lib/api/kolab_api_service_groups.php |   41 +++++++++++-
 lib/api/kolab_api_service_users.php  |    1 
 4 files changed, 115 insertions(+), 47 deletions(-)

New commits:
commit 7e4ebf0372681d8ed18749d52f920b4cbfd95644
Author: Aleksander Machniak <machniak at kolabsys.com>
Date:   Thu Mar 22 10:09:06 2012 +0100

    Implement groups searching

diff --git a/lib/Auth.php b/lib/Auth.php
index b273e39..17cbd0d 100644
--- a/lib/Auth.php
+++ b/lib/Auth.php
@@ -207,14 +207,14 @@ class Auth {
         return $users;
     }
 
-    public function list_groups($domain = NULL, $attributes = array())
+    public function list_groups($domain = NULL, $attributes = array(), $search = array(), $params = array())
     {
         $this->connect($domain);
         if ($domain === NULL) {
             $domain = $this->conf->get('primary_domain');
         }
 
-        $groups = $this->_auth[$domain]->list_groups($attributes);
+        $groups = $this->_auth[$domain]->list_groups($attributes, $search, $params);
 
         return $groups;
     }
diff --git a/lib/Auth/LDAP.php b/lib/Auth/LDAP.php
index e9eecf4..685081c 100644
--- a/lib/Auth/LDAP.php
+++ b/lib/Auth/LDAP.php
@@ -373,12 +373,8 @@ class LDAP
         return $this->_list_group_members($group_dn);
     }
 
-    public function groups_list($attributes = array())
+    public function groups_list($attributes = array(), $search = array())
     {
-        if (empty($attributes)) {
-            $attributes = array('*');
-        }
-
         # TODO: From config
         $base_dn = "ou=Groups,dc=klab,dc=cc";
         # TODO: From config
@@ -388,6 +384,15 @@ class LDAP
             ."(objectclass=kolabgroupofurls)"
             .")";
 
+        if (empty($attributes) || !is_array($attributes)) {
+            $attributes = array('*');
+        }
+
+        if ($s_filter = $this->_search_filter($search)) {
+            // join search filter with objectClass filter
+            $filter = '(&' . $filter . $s_filter . ')';
+        }
+
         return $this->search($base_dn, $filter, $attributes);
     }
 
@@ -404,11 +409,26 @@ class LDAP
         return $domains;
     }
 
-    public function list_groups($attributes = array())
+    public function list_groups($attributes = array(), $search = array(), $params = array())
     {
-        $groups = $this->groups_list($attributes);
+        if (!empty($params['sort_by'])) {
+            if (!in_array($params['sort_by'], $attributes)) {
+                $attributes[] = $params['sort_by'];
+            }
+        }
+
+        $groups = $this->groups_list($attributes, $search);
         $groups = $this->normalize_result($groups);
 
+        if (!empty($params['sort_by'])) {
+            $this->sort_result_key = $params['sort_by'];
+            uasort($groups, array($this, 'sort_result'));
+
+            if ($params['sort_order'] == 'DESC') {
+                $groups = array_reverse($groups, true);
+            }
+        }
+
         return $groups;
     }
 
@@ -624,7 +644,7 @@ class LDAP
         return $this->search($user_dn);
     }
 
-    public function users_list($attributes = array(), $search = array(), $params = array())
+    public function users_list($attributes = array(), $search = array())
     {
         $conf = Conf::get_instance();
 
@@ -635,39 +655,7 @@ class LDAP
             $attributes = array('*');
         }
 
-        if (!empty($search) && is_array($search) && !empty($search['params'])) {
-            $s_filter = '';
-            foreach ((array) $search['params'] as $field => $param) {
-                $value = self::_quote_string($param['value']);
-
-                switch ((string)$param['type']) {
-                case 'prefix':
-                    $prefix = '';
-                    $suffix = '*';
-                    break;
-                case 'suffix':
-                    $prefix = '*';
-                    $suffix = '';
-                    break;
-                case 'exact':
-                    $prefix = '';
-                    $suffix = '';
-                    break;
-                case 'both':
-                default:
-                    $prefix = '*';
-                    $suffix = '*';
-                    break;
-                }
-
-                $s_filter .= "($field=$prefix" . $value . "$suffix)";
-            }
-
-            // join search parameters with specified operator ('OR' or 'AND')
-            if (count($search['params']) > 1) {
-                $s_filter = '(' . ($search['operator'] == 'AND' ? '&' : '|') . $s_filter . ')';
-            }
-
+        if ($s_filter = $this->_search_filter($search)) {
             // join search filter with objectClass filter
             $filter = '(&' . $filter . $s_filter . ')';
         }
@@ -726,8 +714,6 @@ class LDAP
             $attributes_filter[] = is_array($value) ? $key : $value;
         }
 
-//         console($attributes_filter);
-
         return $attributes_filter;
     }
 
@@ -946,6 +932,50 @@ class LDAP
     }
 
     /**
+     * Create LDAP search filter string according to defined parameters.
+     */
+    private function _search_filter($search)
+    {
+        if (empty($search) || !is_array($search) || empty($search['params'])) {
+            return null;
+        }
+    
+        $filter = '';
+        foreach ((array) $search['params'] as $field => $param) {
+            $value = self::_quote_string($param['value']);
+
+            switch ((string)$param['type']) {
+            case 'prefix':
+                $prefix = '';
+                $suffix = '*';
+                break;
+            case 'suffix':
+                $prefix = '*';
+                $suffix = '';
+                break;
+            case 'exact':
+                $prefix = '';
+                $suffix = '';
+                break;
+            case 'both':
+            default:
+                $prefix = '*';
+                $suffix = '*';
+                break;
+            }
+
+            $filter .= "($field=$prefix" . $value . "$suffix)";
+        }
+
+        // join search parameters with specified operator ('OR' or 'AND')
+        if (count($search['params']) > 1) {
+            $filter = '(' . ($search['operator'] == 'AND' ? '&' : '|') . $filter . ')';
+        }
+
+        return $filter;
+    }
+
+    /**
      * Shortcut to ldap_unbind()
      */
     private function _unbind($yes = false, $really = false)
diff --git a/lib/api/kolab_api_service_groups.php b/lib/api/kolab_api_service_groups.php
index 4bdf3b1..d85de12 100644
--- a/lib/api/kolab_api_service_groups.php
+++ b/lib/api/kolab_api_service_groups.php
@@ -39,7 +39,7 @@ class kolab_api_service_groups extends kolab_api_service
     {
         return array(
             'list' => 'r',
-            );
+        );
     }
 
     public function groups_list($get, $post)
@@ -57,7 +57,44 @@ class kolab_api_service_groups extends kolab_api_service
             $attributes = (array)$this->list_attribs[0];
         }
 
-        $groups = $auth->list_groups();
+        $search = array();
+        $params = array();
+
+        // searching
+        if (!empty($post['search']) && is_array($post['search'])) {
+            $params = $post['search'];
+            foreach ($params as $idx => $param) {
+                // get only supported attributes
+                if (!in_array($idx, $this->list_attribs)) {
+                    unset($params[$idx]);
+                    continue;
+                }
+
+                // search string
+                if (empty($param['value'])) {
+                    unset($params[$idx]);
+                    continue;
+                }
+            }
+
+            $search['params'] = $params;
+            if (!empty($post['search_operator'])) {
+                $search['operator'] = $post['search_operator'];
+            }
+        }
+
+        if (!empty($post['sort_by'])) {
+            // check if sort attribute is supported
+            if (in_array($post['sort_by'], $this->list_attribs)) {
+                $params['sort_by'] = $post['sort_by'];
+            }
+        }
+
+        if (!empty($post['sort_order'])) {
+            $params['sort_order'] = $post['sort_order'] == 'DESC' ? 'DESC' : 'ASC';
+        }
+
+        $groups = $auth->list_groups(null, $attributes, $search, $params);
         $count  = count($groups);
 
         // pagination
diff --git a/lib/api/kolab_api_service_users.php b/lib/api/kolab_api_service_users.php
index f06232d..a950a09 100644
--- a/lib/api/kolab_api_service_users.php
+++ b/lib/api/kolab_api_service_users.php
@@ -39,6 +39,7 @@ class kolab_api_service_users extends kolab_api_service
         'uidnumber',
         'gidnumber',
         'mailhost',
+        'dn',
     );
 
 





More information about the commits mailing list