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

UsernameCheck::index()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.4609

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 19
ccs 5
cts 11
cp 0.4545
crap 4.4609
rs 9.6333
c 0
b 0
f 0
1
<?php defined('BASEPATH') or exit('No direct script access allowed');
2
3
class UsernameCheck extends AJAX_Controller {
4 1
	public function __construct() {
5 1
		parent::__construct();
6
7 1
		$this->load->library('Limiter');
8 1
		$this->load->library('form_validation');
9 1
	}
10
11
	/**
12
	 * Used by the signup to do a AJAX username check.
13
	 *
14
	 * REQ_PARAMS: username
15
	 * METHOD:     POST
16
	 * URL:        /ajax/username_check
17
	 */
18 1
	public function index() : void {
19 1
		$this->form_validation->set_rules('username', 'Username', 'required|max_length[100]');
20
21 1
		if($this->form_validation->run() === TRUE) {
22
			if(!$this->limiter->limit('username_check', 25)) {
23
				$is_unique_username = $this->form_validation->is_unique_username($this->input->post('username'));
24
25
				$data = [
26
					'success'    => $is_unique_username,
27
					'csrf_token' => $this->security->get_csrf_hash() //CHECK: Does returning this within the same query make the CSRF pointless?
28
				];
29
				$this->_render_json($data);
30
			} else {
31
				$this->output->set_status_header('429', 'Rate limit reached.'); //rate limited reached
32
			}
33
		} else {
34 1
			$this->output->set_status_header('400', 'Missing/invalid parameters.');
35
		}
36 1
	}
37
}
38