diff options
Diffstat (limited to 'application')
-rw-r--r-- | application/controllers/api/v1/user.php | 24 | ||||
-rw-r--r-- | application/controllers/user.php | 42 | ||||
-rw-r--r-- | application/service/user.php | 75 |
3 files changed, 102 insertions, 39 deletions
diff --git a/application/controllers/api/v1/user.php b/application/controllers/api/v1/user.php new file mode 100644 index 000000000..831fdb883 --- /dev/null +++ b/application/controllers/api/v1/user.php @@ -0,0 +1,24 @@ +<?php +/* + * Copyright 2014 Florian "Bluewind" Pritz <bluewind@server-speed.net> + * + * Licensed under AGPLv3 + * (see COPYING for full license text) + * + */ +namespace controllers\api\v1; + +class user extends \controllers\api\api_controller { + public function __construct() + { + parent::__construct(); + + $this->load->model('muser'); + } + + public function apikeys() + { + $this->muser->require_access("full"); + return send_json_reply(\service\user::apikeys($this->muser->get_userid())); + } +} diff --git a/application/controllers/user.php b/application/controllers/user.php index a702b63c7..62569e1f1 100644 --- a/application/controllers/user.php +++ b/application/controllers/user.php @@ -91,24 +91,7 @@ class User extends MY_Controller { $access_level = "apikey"; } - $valid_levels = $this->muser->get_access_levels(); - if (array_search($access_level, $valid_levels) === false) { - show_error("Invalid access levels requested."); - } - - if (strlen($comment) > 255) { - show_error("Comment may only be 255 chars long."); - } - - $key = random_alphanum(32); - - $this->db->set(array( - 'key' => $key, - 'user' => $userid, - 'comment' => $comment, - 'access_level' => $access_level - )) - ->insert('apikeys'); + $key = \service\user::create_apikey($userid, $comment, $access_level); if (static_storage("response_type") == "json") { return send_json_reply(array("new_key" => $key)); @@ -140,27 +123,8 @@ class User extends MY_Controller { $this->muser->require_access(); $userid = $this->muser->get_userid(); - - $query = $this->db->select('key, created, comment, access_level') - ->from('apikeys') - ->where('user', $userid) - ->order_by('created', 'desc') - ->get()->result_array(); - - // Convert timestamp to unix timestamp - // TODO: migrate database to integer timestamp and get rid of this - foreach ($query as &$record) { - if (!empty($record['created'])) { - $record['created'] = strtotime($record['created']); - } - } - unset($record); - - if (static_storage("response_type") == "json") { - return send_json_reply($query); - } - - $this->data["query"] = $query; + $apikeys = \service\user::apikeys($userid); + $this->data["query"] = $apikeys; $this->load->view('header', $this->data); $this->load->view($this->var->view_dir.'apikeys', $this->data); diff --git a/application/service/user.php b/application/service/user.php new file mode 100644 index 000000000..d06f78855 --- /dev/null +++ b/application/service/user.php @@ -0,0 +1,75 @@ +<?php +/* + * Copyright 2014 Florian "Bluewind" Pritz <bluewind@server-speed.net> + * + * Licensed under AGPLv3 + * (see COPYING for full license text) + * + */ + +namespace service; + +class user { + + /** + * Create a new api key. + * + * @param userid TODO + * @param comment TODO + * @param access_level TODO + * @return the new key + */ + static public function create_apikey($userid, $comment, $access_level) + { + $CI =& get_instance(); + + + $valid_levels = $CI->muser->get_access_levels(); + if (array_search($access_level, $valid_levels) === false) { + show_error("Invalid access levels requested."); + } + + if (strlen($comment) > 255) { + show_error("Comment may only be 255 chars long."); + } + + $key = random_alphanum(32); + + $CI->db->set(array( + 'key' => $key, + 'user' => $userid, + 'comment' => $comment, + 'access_level' => $access_level + )) + ->insert('apikeys'); + + return $key; + } + + /** + * Get apikeys for a user + * @param userid TODO + * @return array with the key data + */ + static public function apikeys($userid) + { + $CI =& get_instance(); + + $query = $CI->db->select('key, created, comment, access_level') + ->from('apikeys') + ->where('user', $userid) + ->order_by('created', 'desc') + ->get()->result_array(); + + // Convert timestamp to unix timestamp + // TODO: migrate database to integer timestamp and get rid of this + foreach ($query as &$record) { + if (!empty($record['created'])) { + $record['created'] = strtotime($record['created']); + } + } + unset($record); + + return $query; + } +} |