summaryrefslogtreecommitdiffstats
path: root/application/controllers/api/v2/user.php
blob: 677a870c4f22e6cadb269da0e601a04c331c6b7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/*
 * Copyright 2014-2016 Florian "Bluewind" Pritz <bluewind@server-speed.net>
 *
 * Licensed under AGPLv3
 * (see COPYING for full license text)
 *
 */
namespace controllers\api\v2;

class user extends \controllers\api\api_controller {
	public function __construct()
	{
		parent::__construct();

		$this->CI->load->model('muser');
	}

	public function apikeys()
	{
		$this->CI->muser->require_access("full");
		return \service\user::apikeys($this->CI->muser->get_userid());
	}

	public function create_apikey()
	{
		$username = $this->CI->input->post("username");
		$password = $this->CI->input->post("password");
		if ($username && $password) {
			if (!$this->CI->muser->login($username, $password)) {
				throw new \exceptions\NotAuthenticatedException("user/login-failed", "Login failed");
			}
		}

		$this->CI->muser->require_access("full");

		$userid = $this->CI->muser->get_userid();
		$comment = $this->CI->input->post("comment");
		$comment = $comment === null ? "" : $comment;
		$access_level = $this->CI->input->post("access_level");

		$key = \service\user::create_apikey($userid, $comment, $access_level);

		return array(
			"new_key" => $key,
		);
	}

	public function delete_apikey()
	{
		$this->CI->muser->require_access("full");

		$userid = $this->CI->muser->get_userid();
		$key = $this->CI->input->post("delete_key");

		$this->CI->db->where('user', $userid)
			->where('key', $key)
			->delete('apikeys');

		$affected = $this->CI->db->affected_rows();

		assert($affected >= 0 && $affected <= 1);
		if ($affected == 1) {
			return array(
				"deleted_keys" => array(
					$key => array (
						"key" => $key,
					),
				),
			);
		} else {
			throw new \exceptions\PublicApiException('user/delete_apikey/failed', 'Apikey deletion failed. Possibly wrong owner.');
		}
	}
}