Completed
Push — master ( a45681...0ad265 )
by Nazar
04:09
created

user_   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 45
c 4
b 0
f 0
lcom 0
cbo 9
dl 0
loc 191
rs 8.3673

5 Methods

Rating   Name   Duplication   Size   Complexity  
B user_change_password() 0 22 6
D user_registration() 0 77 18
C user_restore_password() 0 34 7
C user_sign_in() 0 41 11
A user_sign_out() 0 15 3

How to fix   Complexity   

Complex Class

Complex classes like user_ often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use user_, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * @package    CleverStyle CMS
4
 * @subpackage System module
5
 * @category   modules
6
 * @author     Nazar Mokrynskyi <[email protected]>
7
 * @copyright  Copyright (c) 2015-2016, Nazar Mokrynskyi
8
 * @license    MIT License, see license.txt
9
 */
10
namespace cs\modules\System\api\Controller;
11
use
12
	cs\Config,
13
	cs\ExitException,
14
	cs\Language,
15
	cs\Language\Prefix,
16
	cs\Mail,
17
	cs\Page,
18
	cs\Request,
19
	cs\Response,
20
	cs\Session,
21
	cs\User;
22
23
trait user_ {
24
	static function user_change_password () {
25
		$L    = new Prefix('system_profile_');
26
		$Page = Page::instance();
27
		$User = User::instance();
28
		if (!isset($_POST['current_password'], $_POST['new_password'])) {
29
			throw new ExitException(400);
30
		}
31
		if (!$User->user()) {
32
			throw new ExitException(403);
33
		} elseif (!$_POST['new_password']) {
34
			throw new ExitException($L->please_type_new_password, 400);
35
		} elseif (!$User->validate_password($_POST['current_password'], $User->id, true)) {
36
			throw new ExitException($L->wrong_current_password, 400);
37
		}
38
		$id = $User->id;
39
		if ($User->set_password($_POST['new_password'], $id, true)) {
40
			Session::instance()->add($id);
41
			$Page->json('OK');
42
		} else {
43
			throw new ExitException($L->change_password_server_error, 400);
44
		}
45
	}
46
	static function user_registration () {
47
		$Config  = Config::instance();
48
		$L       = new Prefix('system_profile_registration_');
49
		$Page    = Page::instance();
50
		$Request = Request::instance();
51
		$User    = User::instance();
52
		if (!isset($Request->data['email'])) {
53
			throw new ExitException(400);
54
		} elseif (!$User->guest()) {
55
			$Page->json('reload');
56
			return;
57
		} elseif (!$Config->core['allow_user_registration']) {
58
			throw new ExitException($L->prohibited, 403);
59
		} elseif (empty($Request->data['email'])) {
60
			throw new ExitException($L->please_type_your_email, 400);
61
		}
62
		$email  = mb_strtolower($Request->data['email']);
63
		$result = $User->registration($email);
64
		if ($result === false) {
65
			throw new ExitException($L->please_type_correct_email, 400);
66
		} elseif ($result == 'error') {
67
			throw new ExitException($L->server_error, 500);
68
		} elseif ($result == 'exists') {
69
			throw new ExitException($L->error_exists, 400);
70
		}
71
		$confirm = $result['reg_key'] !== true;
72
		if ($Request->data['username']) {
73
			$User->set('username', $Request->data['username'], $result['id']);
74
		}
75
		// Actually `sha512(sha512(password) + public_key)` instead of plain password
76
		if ($Request->data['password']) {
77
			$User->set_password($Request->data['password'], $result['id'], true);
78
		}
79
		if ($Request->data['language']) {
80
			$User->set('language', $Request->data['language'], $result['id']);
81
		}
82
		if ($Request->data['timezone']) {
83
			$User->set('timezone', $Request->data['timezone'], $result['id']);
84
		}
85
		if ($Request->data['avatar']) {
86
			$User->set('avatar', $Request->data['avatar'], $result['id']);
87
		}
88
		if ($confirm) {
89
			$body = $L->need_confirmation_mail_body(
90
				$User->username($result['id']),
91
				get_core_ml_text('name'),
92
				$Config->core_url()."/profile/registration_confirmation/$result[reg_key]",
93
				$L->time($Config->core['registration_confirmation_time'], 'd')
94
			);
95
		} elseif ($result['password']) {
96
			$body = $L->success_mail_with_password_body(
97
				$User->username($result['id']),
98
				get_core_ml_text('name'),
99
				$Config->core_url().'/profile/settings',
100
				$User->get('login', $result['id']),
101
				$result['password']
102
			);
103
		} else {
104
			$body = $L->success_mail(
105
				$User->username($result['id']),
106
				get_core_ml_text('name'),
107
				$Config->core_url().'/profile/settings',
108
				$User->get('login', $result['id'])
109
			);
110
		}
111
		if (Mail::instance()->send_to(
112
			$email,
113
			$L->{$confirm ? 'need_confirmation_mail' : 'success_mail'}(get_core_ml_text('name')),
114
			$body
115
		)
116
		) {
117
			$Page->json($confirm ? 'registration_confirmation' : 'registration_success');
118
		} else {
119
			$User->registration_cancel();
120
			throw new ExitException($L->mail_sending_error, 500);
121
		}
122
	}
123
	static function user_restore_password () {
124
		$Config = Config::instance();
125
		$L      = new Prefix('system_profile_restore_password_');
126
		$Page   = Page::instance();
127
		$User   = User::instance();
128
		if (!isset($_POST['email'])) {
129
			throw new ExitException(400);
130
		} elseif (!$User->guest()) {
131
			throw new ExitException(403);
132
		} elseif (!$_POST['email']) {
133
			throw new ExitException($L->please_type_your_email, 400);
134
		}
135
		$id = $User->get_id(mb_strtolower($_POST['email']));
136
		if (!$id) {
137
			throw new ExitException($L->user_with_such_login_email_not_found, 400);
138
		}
139
		if (
140
			($key = $User->restore_password($id)) &&
141
			Mail::instance()->send_to(
142
				$User->get('email', $id),
143
				$L->confirmation_mail(get_core_ml_text('name')),
144
				$L->confirmation_mail_body(
145
					$User->username($id),
146
					get_core_ml_text('name'),
147
					$Config->core_url()."/profile/restore_password_confirmation/$key",
148
					$L->time($Config->core['registration_confirmation_time'], 'd')
149
				)
150
			)
151
		) {
152
			$Page->json('OK');
153
		} else {
154
			throw new ExitException($L->server_error, 500);
155
		}
156
	}
157
	static function user_sign_in () {
158
		$Config = Config::instance();
159
		$L      = new Prefix('system_profile_sign_in_');
160
		$User   = User::instance();
161
		if (!$User->guest()) {
162
			return;
163
		} elseif (
164
			$Config->core['sign_in_attempts_block_count'] &&
165
			$User->get_sign_in_attempts_count(@$_POST['login']) >= $Config->core['sign_in_attempts_block_count']
166
		) {
167
			$User->sign_in_result(false, @$_POST['login']);
168
			throw new ExitException($L->attempts_are_over_try_again_in(format_time($Config->core['sign_in_attempts_block_time'])), 403);
169
		}
170
		$id = $User->get_id(@$_POST['login']);
171
		if (
172
			$id &&
173
			$User->validate_password(@$_POST['password'], $id, true)
174
		) {
175
			$status      = $User->get('status', $id);
176
			$block_until = $User->get('block_until', $id);
177
			if ($status == User::STATUS_NOT_ACTIVATED) {
178
				throw new ExitException($L->your_account_is_not_active, 403);
179
			} elseif ($status == User::STATUS_INACTIVE) {
180
				throw new ExitException($L->your_account_disabled, 403);
181
			} elseif ($block_until > time()) {
182
				throw new ExitException($L->your_account_blocked_until(date($L->_datetime, $block_until)), 403);
183
			}
184
			Session::instance()->add($id);
185
			$User->sign_in_result(true, $_POST['login']);
186
		} else {
187
			$User->sign_in_result(false, @$_POST['login']);
188
			$content = $L->authentication_error;
189
			if (
190
				$Config->core['sign_in_attempts_block_count'] &&
191
				$User->get_sign_in_attempts_count(@$_POST['login']) >= $Config->core['sign_in_attempts_block_count'] * 2 / 3
192
			) {
193
				$content .= ' '.$L->attempts_left($Config->core['sign_in_attempts_block_count'] - $User->get_sign_in_attempts_count(@$_POST['login']));
194
			}
195
			throw new ExitException($content, 400);
196
		}
197
	}
198
	static function user_sign_out () {
199
		$User = User::instance();
200
		if ($User->guest()) {
201
			Page::instance()->json(1);
202
			return;
203
		}
204
		if (isset($_POST['sign_out'])) {
205
			Session::instance()->del();
206
			/**
207
			 * Hack for 403 after sign out in administration
208
			 */
209
			Response::instance()->cookie('sign_out', 1, TIME + 5, true);
210
			Page::instance()->json(1);
211
		}
212
	}
213
}
214