Completed
Push — master ( 4a1c4b...dec28d )
by Angus
03:08
created

GetKey::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php defined('BASEPATH') or exit('No direct script access allowed');
2
3
class GetKey extends AJAX_Controller {
4
	public function __construct() {
5
		parent::__construct();
6
7
		$this->load->library('Limiter');
8
		$this->load->library('form_validation');
9
	}
10
11
	/**
12
	 * Used to generate the API Key the userscript can use.
13
	 *
14
	 * REQ_PARAMS: N/A
15
	 * METHOD:     POST
16
	 * URL:        /ajax/get_apikey
17
	 */
18 View Code Duplication
	public function index() : void {
19
		if($this->ion_auth->logged_in()) {
20
			if(!$this->limiter->limit('new_api_key', 10)) {
21
				$api_key = $this->User->get_new_api_key();
22
				$json    = ['api-key' => $api_key];
23
24
				$this->output
25
					->set_content_type('application/json')
26
					->set_output(json_encode($json));
27
			} else {
28
				$this->output->set_status_header('429', 'Rate limit reached.');
29
			}
30
		} else {
31
			$this->output->set_status_header('400', 'Not logged in.');
32
		}
33
	}
34
35 View Code Duplication
	public function restore() : void {
36
		if($this->ion_auth->logged_in()) {
37
			if(!$this->limiter->limit('new_api_key', 10)) {
38
				$api_key = $this->User->restore_api_key();
39
				$json    = ['api-key' => $api_key];
40
41
				$this->output
42
					->set_content_type('application/json')
43
					->set_output(json_encode($json));
44
			} else {
45
				$this->output->set_status_header('429', 'Rate limit reached.');
46
			}
47
		} else {
48
			$this->output->set_status_header('400', 'Not logged in.');
49
		}
50
	}
51
}
52