-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPermissions.php
More file actions
393 lines (349 loc) · 12.9 KB
/
Permissions.php
File metadata and controls
393 lines (349 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?php
/**
* Copyright (C) 2015-2019 FeatherBB
* based on code by (C) 2008-2015 FluxBB
* and Rickard Andersson (C) 2002-2008 PunBB
* License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
*/
namespace FeatherBB\Core;
use FeatherBB\Core\Database as DB;
use FeatherBB\Core\Interfaces\Cache;
use FeatherBB\Core\Interfaces\Container;
use FeatherBB\Core\Interfaces\ForumEnv;
use FeatherBB\Core\Interfaces\User;
class Permissions
{
protected $permissions = [];
protected $parents = [];
protected $regexs = [];
public function getParents($gid = null)
{
// Remove below line if we use again group inheritance later
return false;
$gid = (int) $gid;
if ($gid > 0) {
if (!isset($this->parents[$gid])) {
$this->parents[$gid] = [];
$group = DB::table('groups')->findOne($gid);
if (!$group) {
throw new \ErrorException('Internal error: Unknown group ID', 500);
}
if (!empty($group['inherit'])) {
$data = unserialize($group['inherit']);
$this->parents[$gid] = $data;
}
}
return $this->parents[$gid];
}
return false;
}
public function addParent($gid = null, $newParents = null)
{
$gid = (int) $gid;
$newParent = (array) $newParents;
$oldParents = ($this->getParents($gid)) ? $this->getParents($gid) : [];
foreach ($newParents as $id => $parent) {
if ($gid == $parent) {
throw new \ErrorException('Internal error: A group cannot be a parent of itself', 500);
}
if (!in_array($parent, $oldParents)) {
$this->parents[$gid][] = $parent;
}
}
$result = DB::table('groups')
->findOne($gid)
->set('inherit', serialize($this->parents[$gid]))
->save();
return $this;
}
public function delParent($gid = null, $parent = null)
{
$gid = (int) $gid;
$parent = (int) $parent;
if ($gid == $parent) {
throw new \ErrorException('Internal error: A group cannot be a parent of itself', 500);
}
$parents = ($this->getParents($gid)) ? $this->getParents($gid) : [];
if (($key = array_search($parent, $this->parents[$gid])) !== false) {
unset($this->parents[$gid][$key]);
$result = DB::table('groups')
->findOne($gid)
->set('inherit', serialize($this->parents[$gid]))
->save();
} else {
throw new \ErrorException('Internal error: Group '.$parent.' is not a parent of group '.$gid, 500);
}
return $this;
}
public function allowUser($user = null, $permission = null)
{
if (is_array($permission)) {
foreach ($permission as $id => $perm) {
$this->allowUser($user, $perm);
}
return $this;
}
list($uid, $gid) = $this->getInfosFromUser($user);
$permission = (string) $permission;
if (!isset($this->permissions[$gid][$uid])) {
$this->getUserPermissions($uid);
}
if (!isset($this->permissions[$gid][$uid][$permission])) {
$result = DB::table('permissions')
->where('permission_name', $permission)
->where('user', $uid)
->where('deny', 1)
->findOne();
if ($result) {
$result->delete();
}
$result = DB::table('permissions')
->create()
->set([
'permission_name' => $permission,
'user' => $uid,
'allow' => 1])
->save();
if ($result) {
$this->permissions[$gid][$uid][$permission] = true;
$this->buildRegex($uid, $gid);
} else {
throw new \ErrorException('Internal error: Unable to add new permission to user', 500);
}
}
// Reload permissions cache
Cache::store('permissions', \FeatherBB\Model\Cache::getPermissions());
return $this;
}
public function denyUser($user = null, $permission = null)
{
if (is_array($permission)) {
foreach ($permission as $id => $perm) {
$this->denyUser($user, $perm);
}
return $this;
}
list($uid, $gid) = $this->getInfosFromUser($user);
$permission = (string) $permission;
if (!isset($this->permissions[$gid][$uid])) {
$this->getUserPermissions($uid);
}
if (!isset($this->permissions[$gid][$uid][$permission])) {
$result = DB::table('permissions')
->where('permission_name', $permission)
->where('user', $uid)
->where('allow', 1)
->findOne();
if ($result) {
$result->delete();
unset($this->permissions[$gid][$uid][$permission]);
$this->buildRegex($uid, $gid);
}
$result = DB::table('permissions')
->create()
->set([
'permission_name' => $permission,
'deny' => 1,
'user' => $uid
])
->save();
}
// Reload permissions cache
Cache::store('permissions', \FeatherBB\Model\Cache::getPermissions());
return $this;
}
public function allowGroup($gid = null, $permission = null)
{
$gid = (int) $gid;
if (is_array($permission)) {
foreach ($permission as $id => $perm) {
$this->allowGroup($gid, $perm);
}
return $this;
}
if ($gid > 0) {
$group = DB::table('groups')->findOne($gid);
if (!$group) {
throw new \ErrorException('Internal error: Unknown group ID', 500);
}
}
$result = DB::table('permissions')
->where('permission_name', $permission)
->where('group', $gid)
->where('deny', 1)
->findOne();
if ($result) {
$result->delete();
}
// If group or one of his parents have not the permission, add it
if (!$this->getGroupPermissions($gid, $permission)) {
DB::table('permissions')
->create()
->set([
'permission_name' => $permission,
'group' => $gid,
'allow' => 1,
'deny' => null
])
->save();
}
return $this;
}
public function denyGroup($gid = null, $permission = null)
{
$gid = (int) $gid;
if (is_array($permission)) {
foreach ($permission as $id => $perm) {
$this->denyGroup($gid, $perm);
}
return $this;
}
if ($gid > 0) {
$group = DB::table('groups')->findOne($gid);
if (!$group) {
throw new \ErrorException('Internal error: Unknown user ID', 500);
}
}
// Remove group permission from DB if exists
$result = DB::table('permissions')
->where('permission_name', $permission)
->where('group', $gid)
->where('allow', 1)
->findOne();
if ($result) {
$result->delete();
}
// Check if one of his parents have the permission, and force denied permission if needed
if ($this->getGroupPermissions($gid, $permission)) {
DB::table('permissions')
->create()
->set([
'permission_name' => $permission,
'group' => $gid,
'allow' => null,
'deny' => 1
])
->save();
}
return $this;
}
public function can($user = null, $permission = null)
{
list($uid, $gid) = $this->getInfosFromUser($user);
if (!isset($this->regexs[$gid][$uid])) {
if (!isset($this->permissions[$gid][$uid])) {
$this->getUserPermissions($user);
}
$this->buildRegex($uid, $gid);
}
return (bool) preg_match($this->regexs[$gid][$uid], $permission);
}
public function dump()
{
return $this->permissions;
}
protected function buildRegex($uid = null, $gid = null)
{
$perms = array_map(function ($value) {
$value = str_replace('.', '\.', $value);
return str_replace('*', '.*', $value);
}, array_keys($this->permissions[$gid][$uid]));
$this->regexs[$gid][$uid] = '/^(?:'.implode('|', $perms).')$/';
}
public function getUserPermissions($user = null)
{
list($uid, $gid) = $this->getInfosFromUser($user);
// Admins 'got the power!
if ($gid == ForumEnv::get('FEATHER_ADMIN')) {
$userPerms = ['*' => true];
} else { // Regular user
$allPermissions = Cache::retrieve('permissions');
if (isset($allPermissions[$gid][0])) {
$groupPerms = $allPermissions[$gid];
} else {
$groupPerms = [[]];
}
// Init user permissions with the group defaults
$userPerms = $groupPerms[0];
if (array_key_exists($uid, $groupPerms)) {
// If user have custom permissions, override the defaults
foreach ($groupPerms[$uid] as $permissionName => $isAllowed) {
if (!isset($userPerms[$permissionName])) {
if ((bool) $isAllowed) {
$userPerms[$permissionName] = true;
}
} else {
if ((bool) !$isAllowed) {
unset($userPerms[$permissionName]);
}
}
}
}
}
$this->permissions[$gid][$uid] = $userPerms;
$this->buildRegex($uid, $gid);
return $userPerms;
// Legacy code which may be useful later
// $where = array(
// ['p.user' => $uid],
// ['p.group' => $gid]);
//
// if ($parents = $this->getParents($gid)) {
// foreach ($parents as $parentId) {
// $where[] = ['p.group' => (int) $parentId];
// }
// }
//
// $result = DB::forTable('permissions')
// ->tableAlias('p')
// ->selectMany('p.permission_name', 'p.allow', 'p.deny')
// ->innerJoin('users', array('u.id', '=', $uid), 'u', true)
// ->whereAnyIs($where)
// ->orderByDesc('p.group') // Read groups first to allow user override
// ->findArray();
//
// $this->permissions[$gid][$uid] = array();
// foreach ($result as $perm) {
// if (!isset($this->permissions[$gid][$uid][$perm['permission_name']])) {
// if ((bool) $perm['allow']) {
// $this->permissions[$gid][$uid][$perm['permission_name']] = true;
// }
// } else {
// if ((bool) $perm['deny']) {
// unset($this->permissions[$gid][$uid][$perm['permission_name']]);
// }
// }
// }
//
// $this->buildRegex($uid, $gid);
// return $this->permissions;
}
protected function getInfosFromUser($user = null)
{
if (is_object($user)) {
$uid = $user->id;
$gid = $user->group_id;
} elseif ((int) $user > 0) {
$data = User::getBasic((int) $user);
if (!$data) {
throw new \ErrorException('Internal error: Unknown user ID', 500);
}
$uid = $data['id'];
$gid = $data['group_id'];
} else {
throw new \ErrorException('Internal error: wrong user object type', 500);
}
return [(int) $uid, (int) $gid];
}
public function getGroupPermissions($groupId = null, $perm = null)
{
$groupId = (int) $groupId;
$permissions = Cache::retrieve('permissions');
// Return empty perms array if group id doesn't exist in cache
if (!isset($permissions[$groupId]) || !isset($permissions[$groupId][0])) {
return [];
}
// Return full group permissions or the one we asked for
return !empty($perm) ? isset($permissions[$groupId][0][$perm]) : $permissions[$groupId][0];
}
}