Completed
Push — master ( 701c1d...a14c1c )
by Nazar
04:37
created

Permission::set_permissions()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 2
dl 0
loc 7
ccs 0
cts 5
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package   CleverStyle Framework
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 6
	function get_permission ($group, $label, $user = false) {
45 6
		$user = (int)$user ?: $this->id;
46 6
		if ($user == User::ROOT_ID) {
47
			return true;
48
		}
49 6
		$group_label_exploded = explode('/', "$group/$label");
50
		/**
51
		 * Default permissions values:
52
		 *
53
		 * - only administrators have access to `admin/*` URLs by default
54
		 * - only administrators have access to `api/{module}/admin/*` URLs by default
55
		 * - all other URLs are available to everyone by default
56
		 */
57 6
		$admin_section = $group_label_exploded[0] === 'admin' || ($group_label_exploded[0] === 'api' && @$group_label_exploded[2] === 'admin');
58 6
		if (!$user || ($admin_section && !$this->admin())) {
59 2
			return false;
60
		}
61 4
		$all_permission = Cache::instance()->{'permissions/all'} ?: System_Permission::instance()->get_all();
62 4
		if (isset($all_permission[$group][$label])) {
63 2
			$user_permissions = $this->get_permission_internal($user);
64 2
			$permission_id    = $all_permission[$group][$label];
65 2
			return isset($user_permissions[$permission_id]) ? (bool)$user_permissions[$permission_id] : !$admin_section;
66
		}
67 4
		return true;
68
	}
69
	/**
70
	 * @param int $user
71
	 *
72
	 * @return array
73
	 */
74 2
	protected function get_permission_internal ($user) {
75 2
		if (!isset($this->permissions[$user])) {
76 2
			$this->permissions[$user] = $this->cache->get(
77 2
				"permissions/$user",
78 2
				function () use ($user) {
79 2
					$permissions = [];
80 2
					if ($user != User::GUEST_ID) {
81
						$Group = System_Group::instance();
82
						foreach ($this->get_groups($user) ?: [] as $group_id) {
83
							$permissions = $Group->get_permissions($group_id) ?: [] + $permissions;
84
						}
85
					}
86 2
					$permissions = $this->get_permissions($user) ?: [] + $permissions;
87 2
					return $permissions;
88 2
				}
89
			);
90
		}
91 2
		return $this->permissions[$user];
92
	}
93
	/**
94
	 * Set permission state for specified user
95
	 *
96
	 * @param string    $group Permission group
97
	 * @param string    $label Permission label
98
	 * @param int       $value 1 - allow, 0 - deny, -1 - undefined (remove permission, and use default value)
99
	 * @param false|int $user  If not specified - current user assumed
100
	 *
101
	 * @return bool
102
	 */
103
	function set_permission ($group, $label, $value, $user = false) {
104
		$permission = System_Permission::instance()->get(null, $group, $label);
105
		if ($permission) {
106
			return $this->set_permissions(
107
				[
108
					$permission['id'] => $value
109
				],
110
				$user
111
			);
112
		}
113
		return false;
114
	}
115
	/**
116
	 * Delete permission state for specified user
117
	 *
118
	 * @param string    $group Permission group
119
	 * @param string    $label Permission label
120
	 * @param false|int $user  If not specified - current user assumed
121
	 *
122
	 * @return bool
123
	 */
124
	function del_permission ($group, $label, $user = false) {
125
		return $this->set_permission($group, $label, -1, $user);
126
	}
127
	/**
128
	 * Get array of all permissions states for specified user
129
	 *
130
	 * @param false|int $user If not specified - current user assumed
131
	 *
132
	 * @return int[]|false
133
	 */
134 2
	function get_permissions ($user = false) {
135 2
		$user = (int)$user ?: $this->id;
136 2
		if (!$user) {
137
			return false;
138
		}
139 2
		return $this->get_any_permissions($user, 'user');
140
	}
141
	/**
142
	 * Set user's permissions according to the given array
143
	 *
144
	 * @param array     $data
145
	 * @param false|int $user If not specified - current user assumed
146
	 *
147
	 * @return bool
148
	 */
149
	function set_permissions ($data, $user = false) {
150
		$user = (int)$user ?: $this->id;
151
		if (!$user) {
152
			return false;
153
		}
154
		return $this->set_any_permissions($data, $user, 'user');
155
	}
156
	/**
157
	 * Delete all user's permissions
158
	 *
159
	 * @param false|int $user If not specified - current user assumed
160
	 *
161
	 * @return bool
162
	 */
163 2
	function del_permissions_all ($user = false) {
164 2
		$user = (int)$user ?: $this->id;
165 2
		if (!$user) {
166
			return false;
167
		}
168 2
		return $this->del_any_permissions_all($user, 'user');
169
	}
170
}
171