From 8fd7c6c2ab80240ab1d163c9a4134822c7524144 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 11 Jan 2015 01:40:07 +0100 Subject: add initial user api Signed-off-by: Florian Pritz --- application/service/user.php | 75 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 application/service/user.php (limited to 'application/service/user.php') 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 @@ + + * + * 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; + } +} -- cgit v1.2.3-24-g4f1b