summaryrefslogtreecommitdiff
path: root/modules/rest
diff options
context:
space:
mode:
Diffstat (limited to 'modules/rest')
-rw-r--r--modules/rest/controllers/rest.php44
-rw-r--r--modules/rest/helpers/rest.php9
-rw-r--r--modules/rest/libraries/Rest_Exception.php11
-rw-r--r--modules/rest/tests/Rest_Controller_Test.php15
-rw-r--r--modules/rest/views/error_rest.json.php2
5 files changed, 28 insertions, 53 deletions
diff --git a/modules/rest/controllers/rest.php b/modules/rest/controllers/rest.php
index 3e364bff..f8a46515 100644
--- a/modules/rest/controllers/rest.php
+++ b/modules/rest/controllers/rest.php
@@ -34,8 +34,7 @@ class Rest_Controller extends Controller {
auth::login($user);
- $key = rest::get_access_key($user->id);
- rest::reply($key->access_key);
+ rest::reply(rest::access_key());
}
public function __call($function, $args) {
@@ -82,41 +81,12 @@ class Rest_Controller extends Controller {
}
$response = call_user_func(array($handler_class, $handler_method), $request);
- } catch (Exception $e) {
- $response = $this->_format_exception_response($e);
+ rest::reply($response);
+ } catch (ORM_Validation_Exception $e) {
+ // Note: this is totally insufficient because it doesn't take into account localization. We
+ // either need to map the result values to localized strings in the application code, or every
+ // client needs its own l10n string set.
+ throw new Rest_Exception("Bad Request", 400, $e->validation->errors());
}
-
- rest::reply($response);
- }
-
- private function _format_exception_response($e) {
- // Add this exception to the log
- Kohana_Log::add('error', Kohana_Exception::text($e));
-
- $rest_exception = array();
- if ($e instanceof ORM_Validation_Exception) {
- $detail_response = true;
- $rest_exception["code"] = 400;
- $rest_exception["message"] = "Validation errors";
- $rest_exception["fields"] = $e->validation->errors();
- } else if ($e instanceof Rest_Exception) {
- $rest_exception["code"] = $e->getCode();
- if ($e->getMessage() != "Bad Request") {
- $rest_exception["message"] = "Bad Request";
- $rest_exception["fields"] = array("type", $e->getMessage());
- } else {
- $rest_exception["message"] = $e->getMessage();
- }
- } else {
- $rest_exception["code"] = 500;
- $rest_exception["message"] = t("Remote server call failed. Please contact the Adminstrator.");
- }
-
- if (!headers_sent()) {
- header($rest_exception["code"] == 500 ? "HTTP/1.1 500 Internal Server Error" :
- "HTTP/1.1 400 Bad Request");
- }
-
- return $rest_exception;
}
} \ No newline at end of file
diff --git a/modules/rest/helpers/rest.php b/modules/rest/helpers/rest.php
index b382cb29..bcb12d58 100644
--- a/modules/rest/helpers/rest.php
+++ b/modules/rest/helpers/rest.php
@@ -66,17 +66,18 @@ class rest_Core {
identity::set_active_user($user);
}
- static function get_access_key($user_id) {
+ static function access_key() {
$key = ORM::factory("user_access_key")
- ->where("user_id", "=", $user_id)
+ ->where("user_id", "=", identity::active_user()->id)
->find();
if (!$key->loaded()) {
- $key->user_id = $user_id;
+ $key->user_id = identity::active_user()->id;
$key->access_key = md5(md5(uniqid(mt_rand(), true) . access::private_key()));
$key->save();
}
- return $key;
+
+ return $key->access_key;
}
/**
diff --git a/modules/rest/libraries/Rest_Exception.php b/modules/rest/libraries/Rest_Exception.php
index aa5b3281..087da939 100644
--- a/modules/rest/libraries/Rest_Exception.php
+++ b/modules/rest/libraries/Rest_Exception.php
@@ -18,13 +18,20 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class Rest_Exception_Core extends Kohana_Exception {
- public function __construct($message, $code) {
+ var $response = array();
+
+ public function __construct($message, $code, $response=array()) {
parent::__construct($message, null, $code);
+ $this->response = $response;
}
public function sendHeaders() {
if (!headers_sent()) {
- header("HTTP/1.1 " . $this->getCode() . "Bad Request");
+ header("HTTP/1.1 " . $this->getCode() . " " . $this->getMessage());
}
}
+
+ public function getTemplate() {
+ return "error_rest.json";
+ }
} \ No newline at end of file
diff --git a/modules/rest/tests/Rest_Controller_Test.php b/modules/rest/tests/Rest_Controller_Test.php
index fe83283d..0c8a4a98 100644
--- a/modules/rest/tests/Rest_Controller_Test.php
+++ b/modules/rest/tests/Rest_Controller_Test.php
@@ -21,8 +21,7 @@ class Rest_Controller_Test extends Gallery_Unit_Test_Case {
public function setup() {
$this->_save = array($_GET, $_POST, $_SERVER);
- $key = rest::get_access_key(1); // admin user
- $_SERVER["HTTP_X_GALLERY_REQUEST_KEY"] = $key->access_key;
+ $_SERVER["HTTP_X_GALLERY_REQUEST_KEY"] = rest::access_key();
}
public function teardown() {
@@ -83,11 +82,10 @@ class Rest_Controller_Test extends Gallery_Unit_Test_Case {
$_SERVER["REQUEST_METHOD"] = "GET";
$_GET["key"] = "value";
- $key = rest::get_access_key(1); // admin user
$this->assert_array_equal_to_json(
array("params" => array("key" => "value"),
"method" => "get",
- "access_key" => $key->access_key,
+ "access_key" => rest::access_key(),
"url" => "http://./index.php/gallery_unit_test"),
test::call_and_capture(array(new Rest_Controller(), "mock")));
}
@@ -96,11 +94,10 @@ class Rest_Controller_Test extends Gallery_Unit_Test_Case {
$_SERVER["REQUEST_METHOD"] = "POST";
$_POST["key"] = "value";
- $key = rest::get_access_key(1); // admin user
$this->assert_array_equal_to_json(
array("params" => array("key" => "value"),
"method" => "post",
- "access_key" => $key->access_key,
+ "access_key" => rest::access_key(),
"url" => "http://./index.php/gallery_unit_test"),
test::call_and_capture(array(new Rest_Controller(), "mock")));
}
@@ -110,11 +107,10 @@ class Rest_Controller_Test extends Gallery_Unit_Test_Case {
$_SERVER["HTTP_X_GALLERY_REQUEST_METHOD"] = "put";
$_POST["key"] = "value";
- $key = rest::get_access_key(1); // admin user
$this->assert_array_equal_to_json(
array("params" => array("key" => "value"),
"method" => "put",
- "access_key" => $key->access_key,
+ "access_key" => rest::access_key(),
"url" => "http://./index.php/gallery_unit_test"),
test::call_and_capture(array(new Rest_Controller(), "mock")));
}
@@ -124,11 +120,10 @@ class Rest_Controller_Test extends Gallery_Unit_Test_Case {
$_SERVER["HTTP_X_GALLERY_REQUEST_METHOD"] = "delete";
$_POST["key"] = "value";
- $key = rest::get_access_key(1); // admin user
$this->assert_array_equal_to_json(
array("params" => array("key" => "value"),
"method" => "delete",
- "access_key" => $key->access_key,
+ "access_key" => rest::access_key(),
"url" => "http://./index.php/gallery_unit_test"),
test::call_and_capture(array(new Rest_Controller(), "mock")));
}
diff --git a/modules/rest/views/error_rest.json.php b/modules/rest/views/error_rest.json.php
new file mode 100644
index 00000000..179ce7f9
--- /dev/null
+++ b/modules/rest/views/error_rest.json.php
@@ -0,0 +1,2 @@
+<?php defined("SYSPATH") or die("No direct script access.") ?>
+<?= json_encode($e->response); \ No newline at end of file