Skip to content

Commit

Permalink
Move the set_active_user and normalize_request methods to rest.php he…
Browse files Browse the repository at this point in the history
…lper
  • Loading branch information
Tim Almdal committed Dec 31, 2009
1 parent af10f0a commit 4611eb2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 55 deletions.
50 changes: 2 additions & 48 deletions modules/rest/controllers/rest.php
Expand Up @@ -47,9 +47,9 @@ public function access_key() {
}

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;

Expand All @@ -66,50 +66,4 @@ public function __call($function, $args) {
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;
}
}
54 changes: 47 additions & 7 deletions modules/rest/helpers/rest.php
Expand Up @@ -17,13 +17,6 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class rest_Core {
/**
* Not Implemented
*/
static function not_implemented($log_message=null) {
Rest_Exception::trigger(501, "Not implemented", $log_message);
}

/**
* Request failed
*/
Expand Down Expand Up @@ -62,4 +55,51 @@ static function validation_error($error_data) {
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;
}
}

0 comments on commit 4611eb2

Please sign in to comment.