Skip to content

Commit

Permalink
1) Remove the rest::not_found method and replace it with "throw new K…
Browse files Browse the repository at this point in the history
…ohana_404_Exception

2) Don't use the input path to lookup the item via relative_path_cache.  Instead use url::get_item_from_uri method.
  • Loading branch information
Tim Almdal committed Dec 31, 2009
1 parent e6111e6 commit 11792a1
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 71 deletions.
72 changes: 25 additions & 47 deletions modules/gallery/helpers/gallery_rest.php
Expand Up @@ -21,14 +21,7 @@ class gallery_rest_Core {
static function get($request) {
$path = implode("/", $request->arguments);

$item = ORM::factory("item")
->where("relative_url_cache", "=", $path)
->viewable()
->find();

if (!$item->loaded()) {
return rest::not_found("Resource: {$path} missing.");
}
$item = gallery_rest::_get_item($path);

$parent = $item->parent();
$response_data = array("type" => $item->type,
Expand Down Expand Up @@ -60,25 +53,19 @@ static function put($request) {
return rest::invalid_request();
}
$path = implode("/", $request->arguments);

$item = ORM::factory("item")
->where("relative_url_cache", "=", $path)
->viewable()
->find();

if (!$item->loaded()) {
return rest::not_found("Resource: {$path} missing.");
}

if (!access::can("edit", $item)) {
return rest::not_found("Resource: {$path} permission denied.");
}
$item = gallery_rest::_get_item($path, "edit");

// Validate the request data
$new_values = gallery_rest::_validate($request, $item->parent_id, $item->id);
$errors = $new_values->errors();
if (empty($errors)) {
item::update($item, $new_values->as_array());
$item->title = $new_values->title;
$item->description = $new_values->description;
if ($item->id != 1) {
$item->rename($new_values->name);
}
$item->slug = $new_values->slug;
$item->save();

log::success("content", "Updated $item->type",
"<a href=\"{$item->type}s/$item->id\">view</a>");
Expand All @@ -93,23 +80,11 @@ static function post($request) {
if (empty($request->arguments)) {
return rest::invalid_request();
}
$path = implode("/", $request->arguments);

$components = $request->arguments;
$name = urldecode(array_pop($components));

$parent = ORM::factory("item")
->where("relative_url_cache", "=", implode("/", $components))
->viewable()
->find();

if (!$parent->loaded()) {
return rest::not_found("Resource: {$path} missing.");
}

if (!access::can("edit", $parent)) {
return rest::not_found("Resource: {$path} permission denied.");
}
$parent = gallery_rest::_get_item(implode("/", $components), "edit");

// Validate the request data
$new_values = gallery_rest::_validate($request, $parent->id);
Expand Down Expand Up @@ -153,18 +128,7 @@ static function delete($request) {
}
$path = implode("/", $request->arguments);

$item = ORM::factory("item")
->where("relative_url_cache", "=", $path)
->viewable()
->find();

if (!$item->loaded()) {
return rest::success();
}

if (!access::can("edit", $item)) {
return rest::not_found("Resource: {$path} permission denied.");
}
$item = gallery_rest::_get_item($path, "edit");

if ($item->id == 1) {
return rest::invalid_request("Attempt to delete the root album");
Expand All @@ -183,6 +147,20 @@ static function delete($request) {
return rest::success(array("resource" => array("parent_path" => $parent->relative_url())));
}

private static function _get_item($path, $permission="view") {
$item = url::get_item_from_uri($path);

if (!$item->loaded()) {
throw new Kohana_404_Exception();
}

if (!access::can($permission, $item)) {
throw new Kohana_404_Exception();
}

return $item;
}

private static function _get_children($item, $request) {
$children = array();
$limit = empty($request->limit) ? null : $request->limit;
Expand Down
16 changes: 12 additions & 4 deletions modules/gallery/tests/Gallery_Rest_Helper_Test.php
Expand Up @@ -136,8 +136,12 @@ public function gallery_rest_put_album_not_found_test() {
"title" => "Updated Title",
"name" => "new name");

$this->assert_equal(json_encode(array("status" => "ERROR", "message" => "Resource not found")),
gallery_rest::put($request));
try {
gallery_rest::put($request);
} catch (Kohana_404_Exception $k404) {
} catch (Exception $e) {
$this->assert_false(true, $e->__toString());
}
}

public function gallery_rest_put_album_no_edit_permission_test() {
Expand All @@ -147,8 +151,12 @@ public function gallery_rest_put_album_no_edit_permission_test() {
"title" => "Updated Title",
"name" => "new name");

$this->assert_equal(json_encode(array("status" => "ERROR", "message" => "Resource not found")),
gallery_rest::put($request));
try {
gallery_rest::put($request);
} catch (Kohana_404_Exception $k404) {
} catch (Exception $e) {
$this->assert_false(true, $e->__toString());
}
}

public function gallery_rest_put_album_rename_conflict_test() {
Expand Down
9 changes: 1 addition & 8 deletions modules/rest/helpers/rest.php
Expand Up @@ -46,14 +46,7 @@ static function internal_error($log_message=null) {
}

/**
* Resource Not Found
*/
static function not_found($log_message=null) {
return self::_format_failure_response(t("Resource not found"), $log_message);
}

/**
* Resource Not Found
* Request failed
*/
static function fail($log_message=null) {
return self::_format_failure_response($log_message, $log_message);
Expand Down
6 changes: 3 additions & 3 deletions modules/tag/helpers/tag_rest.php
Expand Up @@ -70,11 +70,11 @@ static function post($request) {
->viewable()
->find();
if (!$item->loaded()) {
return rest::not_found("Resource: {$path} missing.");
throw new Kohana_404_Exception();
}

if (!access::can("edit", $item)) {
return rest::not_found("Resource: {$path} permission denied.");
throw new Kohana_404_Exception();
}

foreach ($tags as $tag) {
Expand All @@ -94,7 +94,7 @@ static function put($request) {
->where("name", "=", $name)
->find();
if (!$tag->loaded()) {
return rest::not_found("Tag: {$name} not found.");
throw new Kohana_404_Exception();
}

$tag->name = $request->new_name;
Expand Down
27 changes: 18 additions & 9 deletions modules/tag/tests/Tag_Rest_Helper_Test.php
Expand Up @@ -119,19 +119,25 @@ public function tag_rest_add_tags_for_item_no_path_test() {
public function tag_rest_add_tags_for_item_not_found_test() {
$request = (object)array("path" => $this->_photo->relative_url() . "b",
"arguments" => array("new,one"));
$this->assert_equal(
json_encode(array("status" => "ERROR", "message" => "Resource not found")),
tag_rest::post($request));
try {
tag_rest::post($request);
} catch (Kohana_404_Exception $k404) {
} catch (Exception $e) {
$this->assert_false(true, $e->__toString());
}
}

public function tag_rest_add_tags_for_item_no_access_test() {
identity::set_active_user($this->_user);
$request = (object)array("path" => $this->_photo->relative_url(),
"arguments" => array("new,one"));

$this->assert_equal(
json_encode(array("status" => "ERROR", "message" => "Resource not found")),
tag_rest::post($request));
try {
tag_rest::post($request);
} catch (Kohana_404_Exception $k404) {
} catch (Exception $e) {
$this->assert_false(true, $e->__toString());
}
}

public function tag_rest_add_tags_for_item_test() {
Expand Down Expand Up @@ -175,9 +181,12 @@ public function tag_rest_update_tag_one_arguments_test() {
public function tag_rest_update_tags_not_found_test() {
$request = (object)array("arguments" => array("not"), "new_name" => "found");

$this->assert_equal(
json_encode(array("status" => "ERROR", "message" => "Resource not found")),
tag_rest::put($request));
try {
tag_rest::put($request);
} catch (Kohana_404_Exception $k404) {
} catch (Exception $e) {
$this->assert_false(true, $e->__toString());
}
}

public function tag_rest_update_tags_test() {
Expand Down

0 comments on commit 11792a1

Please sign in to comment.