Passed
Pull Request — master (#16)
by Anton
03:08
created

Connector::authorize()   C

Complexity

Conditions 8
Paths 6

Size

Total Lines 22
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 7
nc 6
nop 2
dl 0
loc 22
rs 6.6037
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @package Cadmium\System\Modules\Auth
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2017, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace Modules\Auth\Utils {
11
12
	use Modules\Entitizer, Utils\Validate;
13
14
	abstract class Connector {
15
16
		# Connector configuration interface
17
18
		protected static $type = '', $lifetime = '';
19
20
		/**
21
		 * Authorize with a given session/secret code and an authorization mode
22
		 *
23
		 * @return array|false : the array of type ['auth' => $auth, 'user' => $user],
24
		 *         where $auth is an object of type Entitizer\Entity\User\Session or Entitizer\Entity\User\Secret,
25
		 *         and $user is an object of type Entitizer\Entity\User, or false on failure
26
		 */
27
28
		public static function authorize(string $code, bool $admin) {
29
30
			# Check code
31
32
			if (false === ($code = Validate::authCode($code))) return false;
33
34
			# Get auth
35
36
			if (!($auth = Entitizer::get(static::$type))->init($code, 'code')) return false;
37
38
			if (($auth->ip !== REQUEST_CLIENT_IP) || ($auth->time < (REQUEST_TIME - static::$lifetime))) return false;
39
40
			# Get user
41
42
			if (0 === ($user = Entitizer::get(TABLE_USERS, $auth->id))->id) return false;
43
44
			if ($user->rank < ($admin ? RANK_ADMINISTRATOR : RANK_USER)) return false;
45
46
			# ------------------------
47
48
			return ['auth' => $auth, 'user' => $user];
49
		}
50
	}
51
}
52