From 71be6cf239fba5718cd6336403df602b05c21c7d Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Mon, 7 Dec 2009 18:11:26 -0800 Subject: The rest framework that the new gallery3 remote interface will be built on. At the moment, there are no handlers to perform any functionality. --- modules/rest/libraries/Form_Label.php | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 modules/rest/libraries/Form_Label.php (limited to 'modules/rest/libraries') diff --git a/modules/rest/libraries/Form_Label.php b/modules/rest/libraries/Form_Label.php new file mode 100644 index 00000000..315ff510 --- /dev/null +++ b/modules/rest/libraries/Form_Label.php @@ -0,0 +1,45 @@ + "label", + "text" => ""); + + public function __construct($label) { + $this->data["text"] = $label; + } + + public function __get($key) { + return isset($this->data[$key]) ? $this->data[$key] : null; + } + + // In this element we never want print any html so make sure + // render and ultimately html_element only return the empty string + public function render() { + return $this->html_element(); + } + + public function html_element() { + $data = $this->data; + unset($data["text"]); + return "

{$this->data['text']}

"; + } + +} // End Form Script \ No newline at end of file -- cgit v1.2.3 From b0de1fe1d975e43a060c66da16f3cc2474d70bae Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Mon, 7 Dec 2009 20:46:18 -0800 Subject: Delete Form_Label.php which was accidently committed. --- modules/rest/libraries/Form_Label.php | 45 ----------------------------------- 1 file changed, 45 deletions(-) delete mode 100644 modules/rest/libraries/Form_Label.php (limited to 'modules/rest/libraries') diff --git a/modules/rest/libraries/Form_Label.php b/modules/rest/libraries/Form_Label.php deleted file mode 100644 index 315ff510..00000000 --- a/modules/rest/libraries/Form_Label.php +++ /dev/null @@ -1,45 +0,0 @@ - "label", - "text" => ""); - - public function __construct($label) { - $this->data["text"] = $label; - } - - public function __get($key) { - return isset($this->data[$key]) ? $this->data[$key] : null; - } - - // In this element we never want print any html so make sure - // render and ultimately html_element only return the empty string - public function render() { - return $this->html_element(); - } - - public function html_element() { - $data = $this->data; - unset($data["text"]); - return "

{$this->data['text']}

"; - } - -} // End Form Script \ No newline at end of file -- cgit v1.2.3 From 1a12a5e3c89c41ebd087591c16611fbab4293f5b Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Thu, 31 Dec 2009 11:51:51 -0800 Subject: Create a Rest_Exception class and use it to convey status to the client instead of calling rest::forbidden and other rest helper error messages. --- modules/gallery/helpers/gallery_rest.php | 7 ++-- modules/rest/controllers/rest.php | 23 +++++------ modules/rest/helpers/rest.php | 39 ++++--------------- modules/rest/libraries/Rest_Exception.php | 41 ++++++++++++++++++++ modules/rest/tests/Rest_Controller_Test.php | 60 ++++++++++++++++++++--------- modules/tag/helpers/tag_rest.php | 6 +-- 6 files changed, 109 insertions(+), 67 deletions(-) create mode 100644 modules/rest/libraries/Rest_Exception.php (limited to 'modules/rest/libraries') diff --git a/modules/gallery/helpers/gallery_rest.php b/modules/gallery/helpers/gallery_rest.php index 21e2b939..563a2c7c 100644 --- a/modules/gallery/helpers/gallery_rest.php +++ b/modules/gallery/helpers/gallery_rest.php @@ -50,7 +50,7 @@ class gallery_rest_Core { static function put($request) { if (empty($request->arguments)) { - return rest::invalid_request(); + Rest_Exception::trigger(400, "Bad request"); } $path = implode("/", $request->arguments); $item = gallery_rest::_get_item($path, "edit"); @@ -78,7 +78,7 @@ class gallery_rest_Core { static function post($request) { if (empty($request->arguments)) { - return rest::invalid_request(); + Rest_Exception::trigger(400, "Bad request"); } $components = $request->arguments; @@ -125,6 +125,7 @@ class gallery_rest_Core { static function delete($request) { if (empty($request->arguments)) { + Rest_Exception::trigger(400, "Bad request", $log_message); return rest::invalid_request(); } $path = implode("/", $request->arguments); @@ -132,7 +133,7 @@ class gallery_rest_Core { $item = gallery_rest::_get_item($path, "edit"); if ($item->id == 1) { - return rest::invalid_request("Attempt to delete the root album"); + Rest_Exception::trigger(400, "Bad request", "Attempt to delete the root album"); } $parent = $item->parent(); diff --git a/modules/rest/controllers/rest.php b/modules/rest/controllers/rest.php index 6715bc15..b71e60f5 100644 --- a/modules/rest/controllers/rest.php +++ b/modules/rest/controllers/rest.php @@ -20,18 +20,17 @@ class Rest_Controller extends Controller { public function access_key() { $request = (object)Input::instance()->get(); if (empty($request->user) || empty($request->password)) { - print rest::forbidden("No user or password supplied"); - return; + Rest_Exception::trigger(403, "Forbidden", "No user or password supplied"); } $user = identity::lookup_user_by_name($request->user); if (empty($user)) { - print rest::forbidden("User '{$request->user}' not found"); + Rest_Exception::trigger(403, "Forbidden", "User '{$request->user}' not found"); return; } if (!identity::is_correct_password($user, $request->password)) { - print rest::forbidden("Invalid password for '{$request->user}'."); + Rest_Exception::trigger(403, "Forbidden", "Invalid password for '{$request->user}'."); return; } @@ -55,14 +54,16 @@ class Rest_Controller extends Controller { $handler_method = $request->method; if (!method_exists($handler_class, $handler_method)) { - print rest::not_implemented("$handler_class::$handler_method is not implemented"); - return; + Rest_Exception::trigger(501, "Not implemented", "$handler_class::$handler_method"); } print call_user_func(array($handler_class, $handler_method), $request); } + } catch (Rest_Exception $e) { + $e->sendHeaders(); } catch (Exception $e) { - print rest::internal_error($e->__toString()); + Kohana_Log::add("error", $e->__toString()); + header("HTTP/1.1 500 Internal Error"); } } @@ -100,12 +101,12 @@ class Rest_Controller extends Controller { if ($key->loaded()) { $user = identity::lookup_user($key->user_id); if (empty($user)) { - print rest::forbidden("User not found: {$key->user_id}"); - return false;; + Rest_Exception::trigger(403, "Forbidden", $log_message, + "User not found: {$key->user_id}"); } } else { - print rest::forbidden("Invalid user access token supplied: {$key->user_id}"); - return false; + Rest_Exception::trigger(403, "Forbidden", $log_message, + "Invalid user access token supplied: {$key->user_id}"); } } identity::set_active_user($user); diff --git a/modules/rest/helpers/rest.php b/modules/rest/helpers/rest.php index 4b3166c0..7684567c 100644 --- a/modules/rest/helpers/rest.php +++ b/modules/rest/helpers/rest.php @@ -17,39 +17,23 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class rest_Core { - /** - * Authorization Failure - */ - static function forbidden($log_message=null) { - return self::_format_failure_response(t("Authorization failed"), $log_message); - } - - /** - * Invalid Failure - */ - static function invalid_request($log_message=null) { - return self::_format_failure_response(t("Invalid request"), $log_message); - } - /** * Not Implemented */ static function not_implemented($log_message=null) { - return self::_format_failure_response(t("Service not implemented"), $log_message); - } - - /** - * Internal Error - */ - static function internal_error($log_message=null) { - return self::_format_failure_response(t("Internal error"), $log_message); + Rest_Exception::trigger(501, "Not implemented", $log_message); } /** * Request failed */ static function fail($log_message=null) { - return self::_format_failure_response($log_message, $log_message); + if (!empty($log_message)) { + Kohana_Log::add("info", $log_message); + } + // We don't need to save the session for this request + Session::abort_save(); + return json_encode(array("status" => "ERROR", "message" => (string)$message)); } /** @@ -78,13 +62,4 @@ class rest_Core { Session::abort_save(); return json_encode($response); } - - private static function _format_failure_response($message, $log_message) { - if (!empty($log_message)) { - Kohana_Log::add("info", $log_message); - } - // We don't need to save the session for this request - Session::abort_save(); - return json_encode(array("status" => "ERROR", "message" => (string)$message)); - } } diff --git a/modules/rest/libraries/Rest_Exception.php b/modules/rest/libraries/Rest_Exception.php new file mode 100644 index 00000000..acdcb568 --- /dev/null +++ b/modules/rest/libraries/Rest_Exception.php @@ -0,0 +1,41 @@ +getMessage()}'); + } +} // End Rest Exception \ 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 6bebc47d..21b83fe6 100644 --- a/modules/rest/tests/Rest_Controller_Test.php +++ b/modules/rest/tests/Rest_Controller_Test.php @@ -75,26 +75,38 @@ class Rest_Controller_Test extends Unit_Test_Case { public function rest_access_key_no_parameters_test() { $_SERVER["REQUEST_METHOD"] = "GET"; - $this->assert_equal( - json_encode(array("status" => "ERROR", "message" => (string)t("Authorization failed"))), - $this->_call_controller()); + try { + $this->_call_controller(); + } catch (Rest_Exception $e) { + $this->assert_equal("403 Forbidden", $e->getMessage()); + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } } public function rest_access_key_user_not_found_test() { $_SERVER["REQUEST_METHOD"] = "POST"; $_POST["request"] = json_encode(array("user" => "access_test2", "password" => "password")); - $this->assert_equal( - json_encode(array("status" => "ERROR", "message" => (string)t("Authorization failed"))), - $this->_call_controller()); + try { + $this->_call_controller(); + } catch (Rest_Exception $e) { + $this->assert_equal("403 Forbidden", $e->getMessage()); + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } } public function rest_access_key_invalid_password_test() { $_SERVER["REQUEST_METHOD"] = "POST"; - $this->assert_equal( - json_encode(array("status" => "ERROR", "message" => (string)t("Authorization failed"))), - $this->_call_controller()); + try { + $this->_call_controller(); + } catch (Rest_Exception $e) { + $this->assert_equal("403 Forbidden", $e->getMessage()); + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } } public function rest_get_resource_no_request_key_test() { @@ -114,9 +126,13 @@ class Rest_Controller_Test extends Unit_Test_Case { $_SERVER["HTTP_X_GALLERY_REQUEST_KEY"] = md5($this->_access_key); // screw up the access key; $_SERVER["REQUEST_METHOD"] = "GET"; - $this->assert_equal( - json_encode(array("status" => "ERROR", "message" => (string)t("Authorization failed"))), - $this->_call_controller()); + try { + $this->_call_controller(); + } catch (Rest_Exception $e) { + $this->assert_equal("403 Forbidden", $e->getMessage()); + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } } public function rest_get_resource_no_user_for_key_test() { @@ -126,9 +142,13 @@ class Rest_Controller_Test extends Unit_Test_Case { $this->_user->delete(); unset($this->_user); - $this->assert_equal( - json_encode(array("status" => "ERROR", "message" => (string)t("Authorization failed"))), - $this->_call_controller("rest", explode("/", $this->_photo->relative_url()))); + try { + $this->_call_controller("rest", explode("/", $this->_photo->relative_url())); + } catch (Rest_Exception $e) { + $this->assert_equal("403 Forbidden", $e->getMessage()); + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } } public function rest_get_resource_no_handler_test() { @@ -136,9 +156,13 @@ class Rest_Controller_Test extends Unit_Test_Case { $_SERVER["HTTP_X_GALLERY_REQUEST_KEY"] = $this->_access_key; $_SERVER["HTTP_X_GALLERY_REQUEST_METHOD"] = "PUT"; - $this->assert_equal( - json_encode(array("status" => "ERROR", "message" => (string)t("Service not implemented"))), - $this->_call_controller("rest", explode("/", $this->_photo->relative_url()))); + try { + $this->_call_controller("rest", explode("/", $this->_photo->relative_url())); + } catch (Rest_Exception $e) { + $this->assert_equal("501 Not Implemented", $e->getMessage()); + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } } public function rest_get_resource_test() { diff --git a/modules/tag/helpers/tag_rest.php b/modules/tag/helpers/tag_rest.php index cfcf93b2..29b74510 100644 --- a/modules/tag/helpers/tag_rest.php +++ b/modules/tag/helpers/tag_rest.php @@ -60,7 +60,7 @@ class tag_rest_Core { static function post($request) { if (empty($request->arguments) || count($request->arguments) != 1 || empty($request->path)) { - return rest::invalid_request(); + Rest_Exception::trigger(400, "Bad request"); } $path = $request->path; $tags = explode(",", $request->arguments[0]); @@ -85,7 +85,7 @@ class tag_rest_Core { static function put($request) { if (empty($request->arguments[0]) || empty($request->new_name)) { - return rest::invalid_request(); + Rest_Exception::trigger(400, "Bad request"); } $name = $request->arguments[0]; @@ -105,7 +105,7 @@ class tag_rest_Core { static function delete($request) { if (empty($request->arguments[0])) { - return rest::invalid_request(); + Rest_Exception::trigger(400, "Bad request"); } $tags = explode(",", $request->arguments[0]); if (!empty($request->path)) { -- cgit v1.2.3 From 5b9801092b3c347161f9e3b8069e05945a5010d2 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Sat, 2 Jan 2010 16:55:06 -0800 Subject: Remove the Rest_Exception::trigger method. --- modules/gallery/helpers/gallery_rest.php | 9 +++--- modules/rest/controllers/rest.php | 51 +++++++++++++++---------------- modules/rest/helpers/rest.php | 6 ++-- modules/rest/libraries/Rest_Exception.php | 35 +++++++++------------ modules/tag/helpers/tag_rest.php | 6 ++-- 5 files changed, 49 insertions(+), 58 deletions(-) (limited to 'modules/rest/libraries') diff --git a/modules/gallery/helpers/gallery_rest.php b/modules/gallery/helpers/gallery_rest.php index 563a2c7c..a87ebb4e 100644 --- a/modules/gallery/helpers/gallery_rest.php +++ b/modules/gallery/helpers/gallery_rest.php @@ -50,7 +50,7 @@ class gallery_rest_Core { static function put($request) { if (empty($request->arguments)) { - Rest_Exception::trigger(400, "Bad request"); + throw new Rest_Exception(400, "Bad request"); } $path = implode("/", $request->arguments); $item = gallery_rest::_get_item($path, "edit"); @@ -78,7 +78,7 @@ class gallery_rest_Core { static function post($request) { if (empty($request->arguments)) { - Rest_Exception::trigger(400, "Bad request"); + throw new Rest_Exception(400, "Bad request"); } $components = $request->arguments; @@ -125,15 +125,14 @@ class gallery_rest_Core { static function delete($request) { if (empty($request->arguments)) { - Rest_Exception::trigger(400, "Bad request", $log_message); - return rest::invalid_request(); + throw new Rest_Exception(400, "Bad request"); } $path = implode("/", $request->arguments); $item = gallery_rest::_get_item($path, "edit"); if ($item->id == 1) { - Rest_Exception::trigger(400, "Bad request", "Attempt to delete the root album"); + throw new Rest_Exception(400, "Bad request"); } $parent = $item->parent(); diff --git a/modules/rest/controllers/rest.php b/modules/rest/controllers/rest.php index 39ca4797..26e5b31a 100644 --- a/modules/rest/controllers/rest.php +++ b/modules/rest/controllers/rest.php @@ -19,32 +19,34 @@ */ class Rest_Controller extends Controller { public function access_key() { - $request = (object)Input::instance()->get(); - if (empty($request->user) || empty($request->password)) { - Rest_Exception::trigger(403, "Forbidden", "No user or password supplied"); - } + try { + $request = (object)Input::instance()->get(); + if (empty($request->user) || empty($request->password)) { + throw new Rest_Exception(403, "Forbidden"); + } - $user = identity::lookup_user_by_name($request->user); - if (empty($user)) { - Rest_Exception::trigger(403, "Forbidden", "User '{$request->user}' not found"); - return; - } + $user = identity::lookup_user_by_name($request->user); + if (empty($user)) { + throw new Rest_Exception(403, "Forbidden"); + } - if (!identity::is_correct_password($user, $request->password)) { - Rest_Exception::trigger(403, "Forbidden", "Invalid password for '{$request->user}'."); - return; - } + if (!identity::is_correct_password($user, $request->password)) { + throw new Rest_Exception(403, "Forbidden"); + } - $key = ORM::factory("user_access_token") - ->where("user_id", "=", $user->id) - ->find(); - if (!$key->loaded()) { - $key->user_id = $user->id; - $key->access_key = md5($user->name . rand()); - $key->save(); + $key = ORM::factory("user_access_token") + ->where("user_id", "=", $user->id) + ->find(); + if (!$key->loaded()) { + $key->user_id = $user->id; + $key->access_key = md5($user->name . rand()); + $key->save(); + } + print rest::success(array("token" => $key->access_key)); + } catch (Rest_Exception $e) { + $e->sendHeaders(); } - print rest::success(array("token" => $key->access_key)); - } + } public function __call($function, $args) { $request = rest::normalize_request($args); @@ -54,16 +56,13 @@ class Rest_Controller extends Controller { $handler_method = $request->method; if (!method_exists($handler_class, $handler_method)) { - Rest_Exception::trigger(501, "Not implemented", "$handler_class::$handler_method"); + throw new Rest_Exception(403, "Forbidden"); } print call_user_func(array($handler_class, $handler_method), $request); } } catch (Rest_Exception $e) { $e->sendHeaders(); - } catch (Exception $e) { - Kohana_Log::add("error", $e->__toString()); - header("HTTP/1.1 500 Internal Error"); } } } \ No newline at end of file diff --git a/modules/rest/helpers/rest.php b/modules/rest/helpers/rest.php index 00790e6b..be0644f2 100644 --- a/modules/rest/helpers/rest.php +++ b/modules/rest/helpers/rest.php @@ -92,12 +92,10 @@ class rest_Core { if ($key->loaded()) { $user = identity::lookup_user($key->user_id); if (empty($user)) { - Rest_Exception::trigger(403, "Forbidden", $log_message, - "User not found: {$key->user_id}"); + throw new Rest_Exception(403, "Forbidden"); } } else { - Rest_Exception::trigger(403, "Forbidden", $log_message, - "Invalid user access token supplied: {$key->user_id}"); + throw new Rest_Exception(403, "Forbidden"); } } identity::set_active_user($user); diff --git a/modules/rest/libraries/Rest_Exception.php b/modules/rest/libraries/Rest_Exception.php index acdcb568..905b94a0 100644 --- a/modules/rest/libraries/Rest_Exception.php +++ b/modules/rest/libraries/Rest_Exception.php @@ -1,15 +1,22 @@ -arguments) || count($request->arguments) != 1 || empty($request->path)) { - Rest_Exception::trigger(400, "Bad request"); + throw new Rest_Exception(400, "Bad request"); } $path = $request->path; $tags = explode(",", $request->arguments[0]); @@ -85,7 +85,7 @@ class tag_rest_Core { static function put($request) { if (empty($request->arguments[0]) || empty($request->new_name)) { - Rest_Exception::trigger(400, "Bad request"); + throw new Rest_Exception(400, "Bad request"); } $name = $request->arguments[0]; @@ -105,7 +105,7 @@ class tag_rest_Core { static function delete($request) { if (empty($request->arguments[0])) { - Rest_Exception::trigger(400, "Bad request"); + throw new Rest_Exception(400, "Bad request"); } $tags = explode(",", $request->arguments[0]); if (!empty($request->path)) { -- cgit v1.2.3