Skip to content

Commit

Permalink
Simplify rest::get_access_key($user) to rest::access_key() that
Browse files Browse the repository at this point in the history
returns just the access key string for the active user.  That's how we
use the API, so keep it simple.
  • Loading branch information
bharat committed Jun 19, 2010
1 parent 295a42e commit 9b78867
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 16 deletions.
2 changes: 1 addition & 1 deletion modules/organize/controllers/organize.php
Expand Up @@ -39,7 +39,7 @@ function dialog($album_id) {
$v = new View("organize_dialog.html");
$v->album = $album;
$v->domain = $input->server("SERVER_NAME");
$v->access_key = rest::get_access_key($user->id)->access_key;
$v->access_key = rest::access_key();
$v->file_filter = addslashes($file_filter);
$v->sort_order = addslashes(json_encode($sort_order));
$v->sort_fields = addslashes(json_encode($sort_fields));
Expand Down
3 changes: 1 addition & 2 deletions modules/rest/controllers/rest.php
Expand Up @@ -34,8 +34,7 @@ public function index() {

auth::login($user);

$key = rest::get_access_key($user->id);
rest::reply($key->access_key);
rest::reply(rest::access_key());
}

public function __call($function, $args) {
Expand Down
7 changes: 4 additions & 3 deletions modules/rest/helpers/rest.php
Expand Up @@ -66,17 +66,18 @@ static function set_active_user($access_key) {
identity::set_active_user($user);
}

static function get_access_key($user_id) {
static function access_key() {
$key = ORM::factory("user_access_key")
->where("user_id", "=", $user_id)
->where("user_id", "=", identity::active_user()->id)
->find();

if (!$key->loaded()) {
$key->user_id = $user_id;
$key->access_key = md5(md5(uniqid(mt_rand(), true) . access::private_key()));
$key->save();
}
return $key;

return $key->access_key;
}

/**
Expand Down
15 changes: 5 additions & 10 deletions modules/rest/tests/Rest_Controller_Test.php
Expand Up @@ -21,8 +21,7 @@ class Rest_Controller_Test extends Gallery_Unit_Test_Case {
public function setup() {
$this->_save = array($_GET, $_POST, $_SERVER);

$key = rest::get_access_key(1); // admin user
$_SERVER["HTTP_X_GALLERY_REQUEST_KEY"] = $key->access_key;
$_SERVER["HTTP_X_GALLERY_REQUEST_KEY"] = rest::access_key();
}

public function teardown() {
Expand Down Expand Up @@ -83,11 +82,10 @@ public function get_with_access_key_test() {
$_SERVER["REQUEST_METHOD"] = "GET";
$_GET["key"] = "value";

$key = rest::get_access_key(1); // admin user
$this->assert_array_equal_to_json(
array("params" => array("key" => "value"),
"method" => "get",
"access_key" => $key->access_key,
"access_key" => rest::access_key(),
"url" => "http://./index.php/gallery_unit_test"),
test::call_and_capture(array(new Rest_Controller(), "mock")));
}
Expand All @@ -96,11 +94,10 @@ public function post_test() {
$_SERVER["REQUEST_METHOD"] = "POST";
$_POST["key"] = "value";

$key = rest::get_access_key(1); // admin user
$this->assert_array_equal_to_json(
array("params" => array("key" => "value"),
"method" => "post",
"access_key" => $key->access_key,
"access_key" => rest::access_key(),
"url" => "http://./index.php/gallery_unit_test"),
test::call_and_capture(array(new Rest_Controller(), "mock")));
}
Expand All @@ -110,11 +107,10 @@ public function put_test() {
$_SERVER["HTTP_X_GALLERY_REQUEST_METHOD"] = "put";
$_POST["key"] = "value";

$key = rest::get_access_key(1); // admin user
$this->assert_array_equal_to_json(
array("params" => array("key" => "value"),
"method" => "put",
"access_key" => $key->access_key,
"access_key" => rest::access_key(),
"url" => "http://./index.php/gallery_unit_test"),
test::call_and_capture(array(new Rest_Controller(), "mock")));
}
Expand All @@ -124,11 +120,10 @@ public function delete_test() {
$_SERVER["HTTP_X_GALLERY_REQUEST_METHOD"] = "delete";
$_POST["key"] = "value";

$key = rest::get_access_key(1); // admin user
$this->assert_array_equal_to_json(
array("params" => array("key" => "value"),
"method" => "delete",
"access_key" => $key->access_key,
"access_key" => rest::access_key(),
"url" => "http://./index.php/gallery_unit_test"),
test::call_and_capture(array(new Rest_Controller(), "mock")));
}
Expand Down

0 comments on commit 9b78867

Please sign in to comment.