Completed
Branch 0.2.1 (e70612)
by Anton
09:15
created

Reset   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6
Metric Value
wmc 10
lcom 0
cbo 6
dl 0
loc 55
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C __invoke() 0 50 10
1
<?php
2
3
namespace Modules\Auth\Controller {
4
5
	use Modules\Auth, Modules\Entitizer, Utils\Security, Str;
6
7
	class Reset {
8
9
		# Invoker
10
11
		public function __invoke(array $post) {
12
13
			if (Auth::check()) return true;
14
15
			# Declare variables
16
17
			$name = ''; $captcha = '';
18
19
			# Extract post array
20
21
			extract($post);
22
23
			# Validate values
24
25
			if (false === ($name = Auth\Validate::userName($name))) return 'USER_ERROR_NAME_INVALID';
26
27
			if (false === Security::checkCaptcha($captcha)) return 'USER_ERROR_CAPTCHA_INCORRECT';
28
29
			# Create user object
30
31
			$user = Entitizer::get(ENTITY_TYPE_USER);
32
33
			# Init user
34
35
			if (!$user->init($name, 'name')) return 'USER_ERROR_NAME_INCORRECT';
36
37
			if (Auth::admin() && ($user->rank < RANK_ADMINISTRATOR)) return 'USER_ERROR_NAME_INCORRECT';
38
39
			# Check access
40
41
			if (!Auth::admin() && ($user->rank === RANK_GUEST)) return 'USER_ERROR_ACCESS';
42
43
			# Create session
44
45
			$secret = Entitizer::get(ENTITY_TYPE_USER_SECRET, $user->id); $secret->remove();
46
47
			$code = Str::random(40); $ip = REQUEST_CLIENT_IP; $time = REQUEST_TIME;
48
49
			$data = ['id' => $user->id, 'code' => $code, 'ip' => $ip, 'time' => $time];
50
51
			if (!$secret->create($data)) return 'USER_ERROR_AUTH_RESET';
52
53
			# Send mail
54
55
			Auth\Utils\Mail::reset($user, $code);
56
57
			# ------------------------
58
59
			return true;
60
		}
61
	}
62
}
63