1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package CleverStyle CMS |
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
5
|
|
|
* @copyright Copyright (c) 2013-2016, Nazar Mokrynskyi |
6
|
|
|
* @license MIT License, see license.txt |
7
|
|
|
*/ |
8
|
|
|
namespace cs; |
9
|
|
|
use |
10
|
|
|
cs\Cache\Prefix; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class for permissions manipulating |
14
|
|
|
* |
15
|
|
|
* @method static $this instance($check = false) |
16
|
|
|
*/ |
17
|
|
|
class Permission { |
18
|
|
|
use |
19
|
|
|
CRUD_helpers, |
20
|
|
|
Singleton; |
21
|
|
|
|
22
|
|
|
protected $data_model = [ |
23
|
|
|
'id' => 'int:0', |
24
|
|
|
'group' => 'text', |
25
|
|
|
'label' => 'text' |
26
|
|
|
]; |
27
|
|
|
protected $table = '[prefix]permissions'; |
28
|
|
|
/** |
29
|
|
|
* Array of all permissions for quick selecting |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $permissions_table = []; |
33
|
|
|
/** |
34
|
|
|
* @var Prefix |
35
|
|
|
*/ |
36
|
|
|
protected $cache; |
37
|
|
|
/** |
38
|
|
|
* Returns database index |
39
|
|
|
* |
40
|
|
|
* @return int |
41
|
|
|
*/ |
42
|
|
|
protected function cdb () { |
43
|
|
|
return Config::instance()->module('System')->db('users'); |
44
|
|
|
} |
45
|
|
|
protected function construct () { |
46
|
|
|
$this->cache = new Prefix('permissions'); |
47
|
|
|
} |
48
|
|
|
/** |
49
|
|
|
* Get permission data<br> |
50
|
|
|
* If <b>$group</b> or/and <b>$label</b> parameter is specified, <b>$id</b> is ignored. |
51
|
|
|
* |
52
|
|
|
* @param int|null $id |
53
|
|
|
* @param null|string $group |
54
|
|
|
* @param null|string $label |
55
|
|
|
* |
56
|
|
|
* @return array|false If only <b>$id</b> specified - result is array of permission data, in other cases result will be array of arrays of corresponding |
|
|
|
|
57
|
|
|
* permissions data |
58
|
|
|
*/ |
59
|
|
|
function get ($id = null, $group = null, $label = null) { |
60
|
|
|
if ($group !== null || $label !== null) { |
61
|
|
|
return $this->read( |
62
|
|
|
$this->search( |
|
|
|
|
63
|
|
|
[ |
64
|
|
|
'group' => $group, |
65
|
|
|
'label' => $label |
66
|
|
|
], |
67
|
|
|
1, |
68
|
|
|
PHP_INT_MAX, |
69
|
|
|
'id', |
70
|
|
|
true |
71
|
|
|
) ?: [] |
72
|
|
|
); |
73
|
|
|
} else { |
74
|
|
|
return $this->read($id); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
/** |
78
|
|
|
* Add permission |
79
|
|
|
* |
80
|
|
|
* @param string $group |
81
|
|
|
* @param string $label |
82
|
|
|
* |
83
|
|
|
* @return false|int Group id or <b>false</b> on failure |
|
|
|
|
84
|
|
|
*/ |
85
|
|
|
function add ($group, $label) { |
86
|
|
|
$id = $this->create([$group, $label]); |
87
|
|
|
if ($id) { |
88
|
|
|
$this->del_all_cache(); |
89
|
|
|
} |
90
|
|
|
return $id; |
91
|
|
|
} |
92
|
|
|
/** |
93
|
|
|
* Set permission |
94
|
|
|
* |
95
|
|
|
* @param int $id |
96
|
|
|
* @param string $group |
97
|
|
|
* @param string $label |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
function set ($id, $group, $label) { |
102
|
|
|
$result = $this->update([$id, $group, $label]); |
103
|
|
|
if ($result) { |
104
|
|
|
$this->del_all_cache(); |
105
|
|
|
} |
106
|
|
|
return $result; |
107
|
|
|
} |
108
|
|
|
/** |
109
|
|
|
* Deletion of permission or array of permissions |
110
|
|
|
* |
111
|
|
|
* @param int|int[] $id |
112
|
|
|
* |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
|
|
function del ($id) { |
116
|
|
|
if (!$id) { |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
$id = implode(',', (array)_int($id)); |
120
|
|
|
if ($this->db_prime()->q( |
121
|
|
|
[ |
122
|
|
|
"DELETE FROM `[prefix]permissions` |
123
|
|
|
WHERE `id` IN ($id)", |
124
|
|
|
"DELETE FROM `[prefix]users_permissions` |
125
|
|
|
WHERE `permission` IN ($id)", |
126
|
|
|
"DELETE FROM `[prefix]groups_permissions` |
127
|
|
|
WHERE `permission` IN ($id)" |
128
|
|
|
] |
129
|
|
|
) |
130
|
|
|
) { |
131
|
|
|
$Cache = $this->cache; |
132
|
|
|
unset( |
133
|
|
|
$Cache->users, |
134
|
|
|
$Cache->groups |
135
|
|
|
); |
136
|
|
|
$this->del_all_cache(); |
137
|
|
|
return true; |
138
|
|
|
} else { |
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
/** |
143
|
|
|
* Returns array of all permissions grouped by permissions groups |
144
|
|
|
* |
145
|
|
|
* @return array Format of array: ['group']['label'] = <i>permission_id</i> |
146
|
|
|
*/ |
147
|
|
|
function get_all () { |
148
|
|
|
if (empty($this->permissions_table)) { |
149
|
|
|
$this->permissions_table = $this->cache->get( |
|
|
|
|
150
|
|
|
'all', |
151
|
|
|
function () { |
152
|
|
|
$data = $this->read( |
153
|
|
|
$this->search([], 1, PHP_INT_MAX, 'id', true) ?: [] |
|
|
|
|
154
|
|
|
); |
155
|
|
|
$all_permissions = []; |
156
|
|
|
foreach ($data as $item) { |
|
|
|
|
157
|
|
|
$all_permissions[$item['group']][$item['label']] = $item['id']; |
158
|
|
|
} |
159
|
|
|
return $all_permissions; |
160
|
|
|
} |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
return $this->permissions_table; |
164
|
|
|
} |
165
|
|
|
/** |
166
|
|
|
* Deletion of permission table (is used after adding, setting or deletion of permission) |
167
|
|
|
*/ |
168
|
|
|
protected function del_all_cache () { |
169
|
|
|
$this->permissions_table = []; |
170
|
|
|
unset($this->cache->all); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.If the return type contains the type array, this check recommends the use of a more specific type like
String[]
orarray<String>
.