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
|
|
|
if (isset($all_permission[$group][$label])) { |
70
|
|
|
$permission = $all_permission[$group][$label]; |
71
|
|
|
if (isset($this->permissions[$user][$permission])) { |
72
|
|
|
return (bool)$this->permissions[$user][$permission]; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
$group_label_exploded = explode('/', "$group/$label"); |
76
|
|
|
/** |
77
|
|
|
* Default permissions values: |
78
|
|
|
* |
79
|
|
|
* - only administrators have access to `admin/*` URLs by default |
80
|
|
|
* - only administrators have access to `api/{module}/admin/*` URLs by default |
81
|
|
|
* - all other URLs are available to everyone by default |
82
|
|
|
*/ |
83
|
|
|
return $this->admin() |
84
|
|
|
? true |
85
|
|
|
: $group_label_exploded[0] !== 'admin' && ($group_label_exploded[0] !== 'api' || @$group_label_exploded[2] !== 'admin'); |
86
|
|
|
} |
87
|
|
|
/** |
88
|
|
|
* Set permission state for specified user |
89
|
|
|
* |
90
|
|
|
* @param string $group Permission group |
91
|
|
|
* @param string $label Permission label |
92
|
|
|
* @param int $value 1 - allow, 0 - deny, -1 - undefined (remove permission, and use default value) |
93
|
|
|
* @param false|int $user If not specified - current user assumed |
94
|
|
|
* |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
function set_permission ($group, $label, $value, $user = false) { |
98
|
|
|
$permission = System_Permission::instance()->get(null, $group, $label); |
99
|
|
|
if ($permission) { |
100
|
|
|
return $this->set_permissions( |
101
|
|
|
[ |
102
|
|
|
$permission['id'] => $value |
103
|
|
|
], |
104
|
|
|
$user |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
/** |
110
|
|
|
* Delete permission state for specified user |
111
|
|
|
* |
112
|
|
|
* @param string $group Permission group |
113
|
|
|
* @param string $label Permission label |
114
|
|
|
* @param false|int $user If not specified - current user assumed |
115
|
|
|
* |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
function del_permission ($group, $label, $user = false) { |
119
|
|
|
return $this->set_permission($group, $label, -1, $user); |
120
|
|
|
} |
121
|
|
|
/** |
122
|
|
|
* Get array of all permissions states for specified user |
123
|
|
|
* |
124
|
|
|
* @param false|int $user If not specified - current user assumed |
125
|
|
|
* |
126
|
|
|
* @return int[]|false |
127
|
|
|
*/ |
128
|
|
|
function get_permissions ($user = false) { |
129
|
|
|
$user = (int)$user ?: $this->id; |
130
|
|
|
if (!$user) { |
131
|
|
|
return false; |
132
|
|
|
} |
133
|
|
|
return $this->get_any_permissions($user, 'user'); |
134
|
|
|
} |
135
|
|
|
/** |
136
|
|
|
* Set user's permissions according to the given array |
137
|
|
|
* |
138
|
|
|
* @param array $data |
139
|
|
|
* @param false|int $user If not specified - current user assumed |
140
|
|
|
* |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
|
|
function set_permissions ($data, $user = false) { |
144
|
|
|
$user = (int)$user ?: $this->id; |
145
|
|
|
if (!$user) { |
146
|
|
|
return false; |
147
|
|
|
} |
148
|
|
|
return $this->set_any_permissions($data, $user, 'user'); |
149
|
|
|
} |
150
|
|
|
/** |
151
|
|
|
* Delete all user's permissions |
152
|
|
|
* |
153
|
|
|
* @param false|int $user If not specified - current user assumed |
154
|
|
|
* |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
|
function del_permissions_all ($user = false) { |
158
|
|
|
$user = (int)$user ?: $this->id; |
159
|
|
|
if (!$user) { |
160
|
|
|
return false; |
161
|
|
|
} |
162
|
|
|
return $this->del_any_permissions_all($user, 'user'); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|