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

Connector   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
C authorize() 0 22 8
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