Completed
Push — master ( 59797e...72d3e7 )
by Nazar
11:00
created

Permission::get_permission()   C

Complexity

Conditions 16
Paths 100

Size

Total Lines 42
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 42
rs 5.0151
cc 16
eloc 26
nc 100
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @package   CleverStyle CMS
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs\User;
9
use
10
	cs\Cache,
11
	cs\Group as System_Group,
12
	cs\Permission as System_Permission,
13
	cs\Permission\Any,
14
	cs\User;
15
16
/**
17
 * Trait that contains all methods from <i>>cs\User</i> for working with user permissions
18
 *
19
 * @property int              $id
20
 * @property \cs\Cache\Prefix $cache
21
 *
22
 * @method false|int[]        get_groups(false|int $user)
23
 * @method bool               admin()
24
 */
25
trait Permission {
26
	use
27
		Any;
28
	/**
29
	 * Permissions cache for users
30
	 * @var array
31
	 */
32
	protected $permissions = [];
33
	/**
34
	 * Get permission state for specified user
35
	 *
36
	 * Rule: if not denied - allowed (users), if not allowed - denied (admins)
37
	 *
38
	 * @param string    $group Permission group
39
	 * @param string    $label Permission label
40
	 * @param false|int $user  If not specified - current user assumed
41
	 *
42
	 * @return bool If permission exists - returns its state for specified user, otherwise for admin permissions returns <b>false</b> and for others <b>true</b>
43
	 */
44
	function get_permission ($group, $label, $user = false) {
45
		$user = (int)$user ?: $this->id;
46
		if ($user == User::ROOT_ID) {
47
			return true;
48
		}
49
		if (!$user) {
50
			return false;
51
		}
52
		if (!isset($this->permissions[$user])) {
53
			$this->permissions[$user] = $this->cache->get(
54
				"permissions/$user",
55
				function () use ($user) {
56
					$permissions = [];
57
					if ($user != User::GUEST_ID) {
58
						$Group = System_Group::instance();
59
						foreach ($this->get_groups($user) ?: [] as $group_id) {
60
							$permissions = $Group->get_permissions($group_id) ?: [] + $permissions;
61
						}
62
					}
63
					$permissions = $this->get_permissions($user) ?: [] + $permissions;
64
					return $permissions;
65
				}
66
			);
67
		}
68
		$all_permission       = Cache::instance()->{'permissions/all'} ?: System_Permission::instance()->get_all();
69
		$group_label_exploded = explode('/', "$group/$label");
70
		/**
71
		 * Default permissions values:
72
		 *
73
		 * - only administrators have access to `admin/*` URLs by default
74
		 * - only administrators have access to `api/{module}/admin/*` URLs by default
75
		 * - all other URLs are available to everyone by default
76
		 */
77
		$admin_section = $group_label_exploded[0] === 'admin' || ($group_label_exploded[0] === 'api' && @$group_label_exploded[2] === 'admin');
78
		if (isset($all_permission[$group][$label])) {
79
			$permission = $all_permission[$group][$label];
80
			return isset($this->permissions[$user][$permission])
81
				? (bool)$this->permissions[$user][$permission]
82
				: !$admin_section;
83
		}
84
		return !$admin_section || $this->admin();
85
	}
86
	/**
87
	 * Set permission state for specified user
88
	 *
89
	 * @param string    $group Permission group
90
	 * @param string    $label Permission label
91
	 * @param int       $value 1 - allow, 0 - deny, -1 - undefined (remove permission, and use default value)
92
	 * @param false|int $user  If not specified - current user assumed
93
	 *
94
	 * @return bool
95
	 */
96
	function set_permission ($group, $label, $value, $user = false) {
97
		$permission = System_Permission::instance()->get(null, $group, $label);
98
		if ($permission) {
99
			return $this->set_permissions(
100
				[
101
					$permission['id'] => $value
102
				],
103
				$user
104
			);
105
		}
106
		return false;
107
	}
108
	/**
109
	 * Delete permission state for specified user
110
	 *
111
	 * @param string    $group Permission group
112
	 * @param string    $label Permission label
113
	 * @param false|int $user  If not specified - current user assumed
114
	 *
115
	 * @return bool
116
	 */
117
	function del_permission ($group, $label, $user = false) {
118
		return $this->set_permission($group, $label, -1, $user);
119
	}
120
	/**
121
	 * Get array of all permissions states for specified user
122
	 *
123
	 * @param false|int $user If not specified - current user assumed
124
	 *
125
	 * @return int[]|false
126
	 */
127
	function get_permissions ($user = false) {
128
		$user = (int)$user ?: $this->id;
129
		if (!$user) {
130
			return false;
131
		}
132
		return $this->get_any_permissions($user, 'user');
133
	}
134
	/**
135
	 * Set user's permissions according to the given array
136
	 *
137
	 * @param array     $data
138
	 * @param false|int $user If not specified - current user assumed
139
	 *
140
	 * @return bool
141
	 */
142
	function set_permissions ($data, $user = false) {
143
		$user = (int)$user ?: $this->id;
144
		if (!$user) {
145
			return false;
146
		}
147
		return $this->set_any_permissions($data, $user, 'user');
148
	}
149
	/**
150
	 * Delete all user's permissions
151
	 *
152
	 * @param false|int $user If not specified - current user assumed
153
	 *
154
	 * @return bool
155
	 */
156
	function del_permissions_all ($user = false) {
157
		$user = (int)$user ?: $this->id;
158
		if (!$user) {
159
			return false;
160
		}
161
		return $this->del_any_permissions_all($user, 'user');
162
	}
163
}
164