2 commits - lib/client lib/kolab_client_api.php lib/kolab_client_task.php public_html/js

Aleksander Machniak machniak at kolabsys.com
Fri Oct 19 12:08:16 CEST 2012


 lib/client/kolab_client_task_domain.php   |    4 -
 lib/client/kolab_client_task_group.php    |    4 -
 lib/client/kolab_client_task_main.php     |    2 
 lib/client/kolab_client_task_resource.php |    4 -
 lib/client/kolab_client_task_role.php     |    4 -
 lib/client/kolab_client_task_settings.php |    2 
 lib/client/kolab_client_task_signup.php   |    2 
 lib/client/kolab_client_task_user.php     |    4 -
 lib/kolab_client_api.php                  |    9 +---
 lib/kolab_client_task.php                 |   67 +++++++++++++++++++++++++-----
 public_html/js/kolab_admin.js             |    5 --
 11 files changed, 77 insertions(+), 30 deletions(-)

New commits:
commit 87bb8b49ea269dc6a4e071000f636fde9f7ade27
Author: Aleksander Machniak <alec at alec.pl>
Date:   Fri Oct 19 12:07:35 2012 +0200

    Improved API errors handling added error logging and logout on invalid session

diff --git a/lib/client/kolab_client_task_domain.php b/lib/client/kolab_client_task_domain.php
index 71a138a..25dc711 100644
--- a/lib/client/kolab_client_task_domain.php
+++ b/lib/client/kolab_client_task_domain.php
@@ -94,7 +94,7 @@ class kolab_client_task_domain extends kolab_client_task
         }
 
         // get domains list
-        $result = $this->api->post('domains.list', null, $post);
+        $result = $this->api_post('domains.list', null, $post);
         $count  = (int) $result->get('count');
         $result = (array) $result->get('list');
 
@@ -189,7 +189,7 @@ class kolab_client_task_domain extends kolab_client_task
         $id     = $this->get_input('id', 'POST');
         //console("action_info() on", $id);
 
-        $result = $this->api->get('domain.info', array('domain' => $id));
+        $result = $this->api_get('domain.info', array('domain' => $id));
         //console("action_info() \$result", $result);
 
         $domain  = $result->get();
diff --git a/lib/client/kolab_client_task_group.php b/lib/client/kolab_client_task_group.php
index 3349129..81c1fe4 100644
--- a/lib/client/kolab_client_task_group.php
+++ b/lib/client/kolab_client_task_group.php
@@ -94,7 +94,7 @@ class kolab_client_task_group extends kolab_client_task
         }
 
         // get groups list
-        $result = $this->api->post('groups.list', null, $post);
+        $result = $this->api_post('groups.list', null, $post);
         $count  = (int) $result->get('count');
         $result = (array) $result->get('list');
 
@@ -179,7 +179,7 @@ class kolab_client_task_group extends kolab_client_task
     public function action_info()
     {
         $id     = $this->get_input('id', 'POST');
-        $result = $this->api->get('group.info', array('group' => $id));
+        $result = $this->api_get('group.info', array('group' => $id));
         $group  = $result->get();
         $output = $this->group_form(null, $group);
 
diff --git a/lib/client/kolab_client_task_main.php b/lib/client/kolab_client_task_main.php
index bab614f..b88fa40 100644
--- a/lib/client/kolab_client_task_main.php
+++ b/lib/client/kolab_client_task_main.php
@@ -39,7 +39,7 @@ class kolab_client_task_main extends kolab_client_task
     {
         // handle domain change
         if ($domain = $this->get_input('domain', 'GET')) {
-            $result = $this->api->get('system.select_domain', array('domain' => $domain));
+            $result = $this->api_get('system.select_domain', array('domain' => $domain));
 
             if (!$result->get_error_code()) {
                 $_SESSION['user']['domain'] = $domain;
diff --git a/lib/client/kolab_client_task_resource.php b/lib/client/kolab_client_task_resource.php
index a5669be..15caf6f 100644
--- a/lib/client/kolab_client_task_resource.php
+++ b/lib/client/kolab_client_task_resource.php
@@ -94,7 +94,7 @@ class kolab_client_task_resource extends kolab_client_task
         }
 
         // get resources list
-        $result = $this->api->post('resources.list', null, $post);
+        $result = $this->api_post('resources.list', null, $post);
         $count  = $result->get('count');
         $result = (array) $result->get('list');
 
@@ -192,7 +192,7 @@ class kolab_client_task_resource extends kolab_client_task
     public function action_info()
     {
         $id         = $this->get_input('id', 'POST');
-        $result     = $this->api->get('resource.info', array('resource' => $id));
+        $result     = $this->api_get('resource.info', array('resource' => $id));
         $resource   = $result->get();
 
         //console("action_info()", $resource);
diff --git a/lib/client/kolab_client_task_role.php b/lib/client/kolab_client_task_role.php
index 5539ceb..bae4585 100644
--- a/lib/client/kolab_client_task_role.php
+++ b/lib/client/kolab_client_task_role.php
@@ -94,7 +94,7 @@ class kolab_client_task_role extends kolab_client_task
         }
 
         // get roles list
-        $result = $this->api->post('roles.list', null, $post);
+        $result = $this->api_post('roles.list', null, $post);
         $count  = (int) $result->get('count');
         $result = (array) $result->get('list');
 
@@ -179,7 +179,7 @@ class kolab_client_task_role extends kolab_client_task
     public function action_info()
     {
         $id     = $this->get_input('id', 'POST');
-        $result = $this->api->get('role.info', array('role' => $id));
+        $result = $this->api_get('role.info', array('role' => $id));
         $role  = $result->get();
         $output = $this->role_form(null, $role);
 
diff --git a/lib/client/kolab_client_task_settings.php b/lib/client/kolab_client_task_settings.php
index 376097e..a6254f0 100644
--- a/lib/client/kolab_client_task_settings.php
+++ b/lib/client/kolab_client_task_settings.php
@@ -829,7 +829,7 @@ class kolab_client_task_settings extends kolab_client_task
         );
 
         // get all available attributes
-        $response   = $this->api->post('form_value.select_options', null, $post_data);
+        $response   = $this->api_post('form_value.select_options', null, $post_data);
         $response   = $response->get('attribute');
         $attributes = array();
 
diff --git a/lib/client/kolab_client_task_signup.php b/lib/client/kolab_client_task_signup.php
index 8863c2b..7c40b36 100644
--- a/lib/client/kolab_client_task_signup.php
+++ b/lib/client/kolab_client_task_signup.php
@@ -69,7 +69,7 @@ class kolab_client_task_signup extends kolab_client_task
         }
 
         // Login ($result is a kolab_client_api_result instance)
-        $result = $this->api->login($this->config_get('bind_dn'), $this->config_get('bind_pw'), $this->domain);
+        $result = $this->api_login($this->config_get('bind_dn'), $this->config_get('bind_pw'), $this->domain);
 
         // Set the session token we got in the API client instance, so subsequent
         // API calls are made in the same session.
diff --git a/lib/client/kolab_client_task_user.php b/lib/client/kolab_client_task_user.php
index 3edf551..2ab1647 100644
--- a/lib/client/kolab_client_task_user.php
+++ b/lib/client/kolab_client_task_user.php
@@ -94,7 +94,7 @@ class kolab_client_task_user extends kolab_client_task
         }
 
         // get users list
-        $result = $this->api->post('users.list', null, $post);
+        $result = $this->api_post('users.list', null, $post);
         $count  = $result->get('count');
         $result = (array) $result->get('list');
 
@@ -179,7 +179,7 @@ class kolab_client_task_user extends kolab_client_task
     public function action_info()
     {
         $id     = $this->get_input('id', 'POST');
-        $result = $this->api->get('user.info', array('user' => $id));
+        $result = $this->api_get('user.info', array('user' => $id));
         $user   = $result->get();
         $output = $this->user_form(null, $user);
 
diff --git a/lib/kolab_client_task.php b/lib/kolab_client_task.php
index 59ed1fb..95d45a7 100644
--- a/lib/kolab_client_task.php
+++ b/lib/kolab_client_task.php
@@ -316,9 +316,9 @@ class kolab_client_task
     /**
      * Logout action.
      */
-    private function action_logout($sess_expired = false)
+    private function action_logout($sess_expired = false, $stop_sess = true)
     {
-        if (!empty($_SESSION['user']) && !empty($_SESSION['user']['token'])) {
+        if (!empty($_SESSION['user']) && !empty($_SESSION['user']['token']) && $stop_sess) {
             $this->api->logout();
         }
         $_SESSION = array();
@@ -328,6 +328,11 @@ class kolab_client_task
                 $args = array('error' => 'session.expired');
             }
             $this->output->command('main_logout', $args);
+
+            if ($sess_expired) {
+                $this->output->send();
+                exit;
+            }
         }
         else {
             $this->output->add_translation('loginerror', 'internalerror', 'session.expired');
@@ -539,6 +544,50 @@ class kolab_client_task
     }
 
     /**
+     * API GET request wrapper
+     */
+    protected function api_get($action, $get = array())
+    {
+        return $this->api_call('get', $action, $get);
+    }
+
+    /**
+     * API POST request wrapper
+     */
+    protected function api_post($action, $get = array(), $post = array())
+    {
+        return $this->api_call('post', $action, $get, $post);
+    }
+
+    /**
+     * API request wrapper with error handling
+     */
+    protected function api_call($type, $action, $get = array(), $post = array())
+    {
+        if ($type == 'post') {
+            $result = $this->api->get($action, $get);
+        }
+        else {
+            $result = $this->api->post($action, $get, $post);
+        }
+
+        // error handling
+        if ($code = $result->get_error_code()) {
+            // Invalid session, do logout
+            if ($code == 403) {
+                $this->action_logout(true, false);
+            }
+
+            // Log communication errors, other should be logged on API side
+            if ($code < 400) {
+                $this->raise_error($code, 'API Error: ' . $result->get_error_str());
+            }
+        }
+
+        return $result;
+    }
+
+    /**
      * Returns list of object types.
      *
      * @para string  $type     Object type name
@@ -555,7 +604,7 @@ class kolab_client_task
         $cache_idx = $type . '_types' . ($used_for ? ":$used_for" : '');
 
         if (!array_key_exists($cache_idx, $this->cache)) {
-            $result = $this->api->post($type . '_types.list');
+            $result = $this->api_post($type . '_types.list');
             $list   = $result->get('list');
 
             if (!empty($used_for) && is_array($list)) {
@@ -589,7 +638,7 @@ class kolab_client_task
             }
         }
 
-        $result   = $this->api->get('user.info', array('user' => $dn));
+        $result   = $this->api_get('user.info', array('user' => $dn));
         $username = $result->get('displayname');
 
         if (empty($username)) {
@@ -622,7 +671,7 @@ class kolab_client_task
             $list = $_SESSION['capabilities'];
         }
         else {
-            $result = $this->api->post('system.capabilities');
+            $result = $this->api_post('system.capabilities');
             $list   = $result->get('list');
 
             if (is_array($list) && !$this->devel_mode) {
@@ -681,7 +730,7 @@ class kolab_client_task
         }
 
         // Get the rights on the entry and attribute level
-        $result = $this->api->get($type . '.effective_rights', array($type => $id));
+        $result = $this->api_get($type . '.effective_rights', array($type => $id));
 
         $result = array(
             'attribute' => $result->get('attributeLevelRights'),
@@ -838,7 +887,7 @@ class kolab_client_task
 
         if (!isset($field['values'])) {
             $data['attributes'] = array($field['name']);
-            $resp = $this->api->post('form_value.select_options', null, $data);
+            $resp = $this->api_post('form_value.select_options', null, $data);
             $resp = $resp->get($field['name']);
             unset($data['attributes']);
 
@@ -1012,7 +1061,7 @@ class kolab_client_task
         if ($add_mode) {
             if (!empty($auto_attribs)) {
                 $data['attributes'] = $auto_attribs;
-                $resp = $this->api->post('form_value.generate', null, $data);
+                $resp = $this->api_post('form_value.generate', null, $data);
                 $data = array_merge((array)$data, (array)$resp->get());
                 unset($data['attributes']);
             }
@@ -1078,7 +1127,7 @@ class kolab_client_task
                 );
 
                 // get options list
-                $result = $this->api->post('form_value.list_options', null, $post);
+                $result = $this->api_post('form_value.list_options', null, $post);
                 $result = $result->get('list');
 
                 $data[$fname] = $result;
diff --git a/public_html/js/kolab_admin.js b/public_html/js/kolab_admin.js
index 4f79b5f..99fc6b9 100644
--- a/public_html/js/kolab_admin.js
+++ b/public_html/js/kolab_admin.js
@@ -355,12 +355,11 @@ function kolab_admin()
     this.set_busy(false);
 
     if (!response || response.status != 'OK') {
-      var msg = response && response.reason ? response.reason : this.t('servererror');
-      this.display_message(msg, 'error');
-
       // Logout on invalid-session error
       if (response && response.code == 403)
         this.main_logout();
+      else
+        this.display_message(response && response.reason ? response.reason : this.t('servererror'), 'error');
 
       return false;
     }


commit 704ba022c02a40ca27ad685836c831dbfeca8f55
Author: Aleksander Machniak <alec at alec.pl>
Date:   Fri Oct 19 12:07:23 2012 +0200

    Added API calls tracking

diff --git a/lib/kolab_client_api.php b/lib/kolab_client_api.php
index 7405489..af9eeca 100644
--- a/lib/kolab_client_api.php
+++ b/lib/kolab_client_api.php
@@ -37,9 +37,6 @@ class kolab_client_api
      */
     private $base_url;
 
-    const STATUS_OK    = 0;
-    const STATUS_ERROR = 1;
-
     const ERROR_INTERNAL   = 100;
     const ERROR_CONNECTION = 200;
 
@@ -158,9 +155,9 @@ class kolab_client_api
     {
         $url = $this->build_url($action, $args);
 
-        $this->request->setMethod(HTTP_Request2::METHOD_GET);
+        Log::trace("Calling API GET: $url");
 
-        //console("GET", $url);
+        $this->request->setMethod(HTTP_Request2::METHOD_GET);
 
         return $this->get_response($url);
     }
@@ -178,6 +175,8 @@ class kolab_client_api
     {
         $url = $this->build_url($action, $url_args);
 
+        Log::trace("Calling API POST: $url");
+
         $this->request->setMethod(HTTP_Request2::METHOD_POST);
         $this->request->setBody(@json_encode($post));
 





More information about the commits mailing list