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 /modules/rest/helpers/rest.php | |
parent | af10f0abcf452db92b5744a6c2d059d647d13d13 (diff) |
Move the set_active_user and normalize_request methods to rest.php helper
Diffstat (limited to 'modules/rest/helpers/rest.php')
-rw-r--r-- | modules/rest/helpers/rest.php | 54 |
1 files changed, 47 insertions, 7 deletions
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; + } } |