diff options
author | Tim Almdal <tnalmdal@shaw.ca> | 2009-12-31 12:32:54 -0800 |
---|---|---|
committer | Tim Almdal <tnalmdal@shaw.ca> | 2009-12-31 12:32:54 -0800 |
commit | 4611eb2142b155f1113e35b456fe38e33f0884fb (patch) | |
tree | a529cb5daf967b897a74d2d2ac8c618396de745c | |
parent | af10f0abcf452db92b5744a6c2d059d647d13d13 (diff) |
Move the set_active_user and normalize_request methods to rest.php helper
-rw-r--r-- | modules/rest/controllers/rest.php | 50 | ||||
-rw-r--r-- | modules/rest/helpers/rest.php | 54 |
2 files changed, 49 insertions, 55 deletions
diff --git a/modules/rest/controllers/rest.php b/modules/rest/controllers/rest.php index b71e60f5..05935e75 100644 --- a/modules/rest/controllers/rest.php +++ b/modules/rest/controllers/rest.php @@ -47,9 +47,9 @@ class Rest_Controller extends Controller { } public function __call($function, $args) { - $request = $this->_normalize_request($args); + $request = rest::normalize_request($args); try { - if ($this->_set_active_user($request->access_token)) { + if (rest::set_active_user($request->access_token)) { $handler_class = "{$function}_rest"; $handler_method = $request->method; @@ -66,50 +66,4 @@ class Rest_Controller extends Controller { header("HTTP/1.1 500 Internal Error"); } } - - private function _normalize_request($args=array()) { - $input = Input::instance(); - $method = strtolower($input->server("REQUEST_METHOD")); - $request = new stdClass(); - foreach (array_keys($input->get()) as $key) { - $request->$key = $input->get($key); - } - if ($method != "get") { - foreach (array_keys($input->post()) as $key) { - $request->$key = $input->post($key); - } - foreach (array_keys($_FILES) as $key) { - $request->$key = $_FILES[$key]; - } - } - - $request->method = strtolower($input->server("HTTP_X_GALLERY_REQUEST_METHOD", $method)); - $request->access_token = $input->server("HTTP_X_GALLERY_REQUEST_KEY"); - $request->arguments = $args; // Let the rest handler figure out what the arguments mean - - return $request; - } - - private function _set_active_user($access_token) { - if (empty($access_token)) { - $user = identity::guest(); - } else { - $key = ORM::factory("user_access_token") - ->where("access_key", "=", $access_token) - ->find(); - - 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}"); - } - } else { - Rest_Exception::trigger(403, "Forbidden", $log_message, - "Invalid user access token supplied: {$key->user_id}"); - } - } - identity::set_active_user($user); - return true; - } }
\ No newline at end of file diff --git a/modules/rest/helpers/rest.php b/modules/rest/helpers/rest.php index 7684567c..7e2445e4 100644 --- a/modules/rest/helpers/rest.php +++ b/modules/rest/helpers/rest.php @@ -18,13 +18,6 @@ */ class rest_Core { /** - * Not Implemented - */ - static function not_implemented($log_message=null) { - Rest_Exception::trigger(501, "Not implemented", $log_message); - } - - /** * Request failed */ static function fail($log_message=null) { @@ -62,4 +55,51 @@ class rest_Core { Session::abort_save(); return json_encode($response); } + + + static function normalize_request($args=array()) { + $input = Input::instance(); + $method = strtolower($input->server("REQUEST_METHOD")); + $request = new stdClass(); + foreach (array_keys($input->get()) as $key) { + $request->$key = $input->get($key); + } + if ($method != "get") { + foreach (array_keys($input->post()) as $key) { + $request->$key = $input->post($key); + } + foreach (array_keys($_FILES) as $key) { + $request->$key = $_FILES[$key]; + } + } + + $request->method = strtolower($input->server("HTTP_X_GALLERY_REQUEST_METHOD", $method)); + $request->access_token = $input->server("HTTP_X_GALLERY_REQUEST_KEY"); + $request->arguments = $args; // Let the rest handler figure out what the arguments mean + + return $request; + } + + static function set_active_user($access_token) { + if (empty($access_token)) { + $user = identity::guest(); + } else { + $key = ORM::factory("user_access_token") + ->where("access_key", "=", $access_token) + ->find(); + + 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}"); + } + } else { + Rest_Exception::trigger(403, "Forbidden", $log_message, + "Invalid user access token supplied: {$key->user_id}"); + } + } + identity::set_active_user($user); + return true; + } } |