|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package CleverStyle Framework |
|
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
|
5
|
|
|
* @copyright Copyright (c) 2013-2016, Nazar Mokrynskyi |
|
6
|
|
|
* @license MIT License, see license.txt |
|
7
|
|
|
*/ |
|
8
|
|
|
/** |
|
9
|
|
|
* Provides next events:<br> |
|
10
|
|
|
* |
|
11
|
|
|
* System/User/Group/add |
|
12
|
|
|
* ['id' => <i>group_id</i>] |
|
13
|
|
|
* |
|
14
|
|
|
* System/User/Group/del/before |
|
15
|
|
|
* ['id' => <i>group_id</i>] |
|
16
|
|
|
* |
|
17
|
|
|
* System/User/Group/del/after |
|
18
|
|
|
* ['id' => <i>group_id</i>] |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
namespace cs; |
|
22
|
|
|
use |
|
23
|
|
|
cs\Permission\Any; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class for groups manipulating |
|
27
|
|
|
* |
|
28
|
|
|
* @method static $this instance($check = false) |
|
29
|
|
|
*/ |
|
30
|
|
|
class Group { |
|
31
|
|
|
use |
|
32
|
|
|
CRUD_helpers, |
|
33
|
|
|
Singleton, |
|
34
|
|
|
Any; |
|
35
|
|
|
protected $data_model = [ |
|
36
|
|
|
'id' => 'int:0', |
|
37
|
|
|
'title' => 'html', |
|
38
|
|
|
'description' => 'html' |
|
39
|
|
|
]; |
|
40
|
|
|
protected $table = '[prefix]groups'; |
|
41
|
|
|
/** |
|
42
|
|
|
* @var Cache\Prefix |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $cache; |
|
45
|
|
|
/** |
|
46
|
|
|
* Returns database index |
|
47
|
|
|
* |
|
48
|
|
|
* @return int |
|
49
|
|
|
*/ |
|
50
|
4 |
|
protected function cdb () { |
|
51
|
4 |
|
return Config::instance()->module('System')->db('users'); |
|
52
|
|
|
} |
|
53
|
4 |
|
protected function construct () { |
|
54
|
4 |
|
$this->cache = Cache::prefix('groups'); |
|
55
|
4 |
|
} |
|
56
|
|
|
/** |
|
57
|
|
|
* Get group data |
|
58
|
|
|
* |
|
59
|
|
|
* @param int|int[] $id |
|
60
|
|
|
* |
|
61
|
|
|
* @return array|array[]|false |
|
62
|
|
|
*/ |
|
63
|
2 |
|
function get ($id) { |
|
64
|
2 |
|
if (is_array($id)) { |
|
65
|
2 |
|
foreach ($id as &$i) { |
|
66
|
2 |
|
$i = $this->get($i); |
|
67
|
|
|
} |
|
68
|
2 |
|
return $id; |
|
69
|
|
|
} |
|
70
|
2 |
|
$id = (int)$id; |
|
71
|
2 |
|
if (!$id) { |
|
72
|
2 |
|
return false; |
|
73
|
|
|
} |
|
74
|
2 |
|
return $this->cache->get( |
|
75
|
|
|
$id, |
|
76
|
|
|
function () use ($id) { |
|
77
|
2 |
|
return $this->read($id); |
|
78
|
2 |
|
} |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
/** |
|
82
|
|
|
* Get array of all groups |
|
83
|
|
|
* |
|
84
|
|
|
* @return int[] |
|
85
|
|
|
*/ |
|
86
|
2 |
|
function get_all () { |
|
87
|
2 |
|
return $this->cache->get( |
|
88
|
2 |
|
'all', |
|
89
|
2 |
|
function () { |
|
90
|
2 |
|
return $this->search([], 1, PHP_INT_MAX, 'id', true); |
|
91
|
2 |
|
} |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
/** |
|
95
|
|
|
* Add new group |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $title |
|
98
|
|
|
* @param string $description |
|
99
|
|
|
* |
|
100
|
|
|
* @return false|int |
|
|
|
|
|
|
101
|
|
|
*/ |
|
102
|
4 |
|
function add ($title, $description) { |
|
103
|
4 |
|
$id = $this->create($title, $description); |
|
104
|
4 |
|
if ($id) { |
|
105
|
4 |
|
unset($this->cache->all); |
|
106
|
4 |
|
Event::instance()->fire( |
|
107
|
4 |
|
'System/User/Group/add', |
|
108
|
|
|
[ |
|
109
|
4 |
|
'id' => $id |
|
110
|
|
|
] |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
4 |
|
return $id; |
|
114
|
|
|
} |
|
115
|
|
|
/** |
|
116
|
|
|
* Set group data |
|
117
|
|
|
* |
|
118
|
|
|
* @param int $id |
|
119
|
|
|
* @param string $title |
|
120
|
|
|
* @param string $description |
|
121
|
|
|
* |
|
122
|
|
|
* @return bool |
|
123
|
|
|
*/ |
|
124
|
2 |
|
function set ($id, $title, $description) { |
|
125
|
2 |
|
$id = (int)$id; |
|
126
|
2 |
|
$result = $this->update($id, $title, $description); |
|
127
|
2 |
|
if ($result) { |
|
128
|
2 |
|
$Cache = $this->cache; |
|
129
|
|
|
unset( |
|
130
|
2 |
|
$Cache->$id, |
|
131
|
2 |
|
$Cache->all |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
2 |
|
return (bool)$result; |
|
135
|
|
|
} |
|
136
|
|
|
/** |
|
137
|
|
|
* Delete group |
|
138
|
|
|
* |
|
139
|
|
|
* @param int|int[] $id |
|
140
|
|
|
* |
|
141
|
|
|
* @return bool |
|
142
|
|
|
*/ |
|
143
|
2 |
|
function del ($id) { |
|
144
|
2 |
|
if (is_array($id)) { |
|
145
|
2 |
|
foreach ($id as &$i) { |
|
146
|
2 |
|
$i = (int)$this->del($i); |
|
147
|
|
|
} |
|
148
|
2 |
|
return (bool)array_product($id); |
|
149
|
|
|
} |
|
150
|
2 |
|
$id = (int)$id; |
|
151
|
2 |
|
if (in_array($id, [User::ADMIN_GROUP_ID, User::USER_GROUP_ID])) { |
|
152
|
2 |
|
return false; |
|
153
|
|
|
} |
|
154
|
2 |
|
Event::instance()->fire( |
|
155
|
2 |
|
'System/User/Group/del/before', |
|
156
|
|
|
[ |
|
157
|
2 |
|
'id' => $id |
|
158
|
|
|
] |
|
159
|
|
|
); |
|
160
|
2 |
|
$result = $this->db_prime()->q( |
|
161
|
|
|
[ |
|
162
|
2 |
|
"DELETE FROM `[prefix]groups` WHERE `id` = $id", |
|
163
|
2 |
|
"DELETE FROM `[prefix]users_groups` WHERE `group` = $id" |
|
164
|
|
|
] |
|
165
|
|
|
); |
|
166
|
2 |
|
if ($result) { |
|
167
|
2 |
|
$this->del_permissions_all($id); |
|
168
|
2 |
|
$Cache = $this->cache; |
|
169
|
|
|
unset( |
|
170
|
2 |
|
Cache::instance()->{'users/groups'}, |
|
171
|
2 |
|
$Cache->$id, |
|
172
|
2 |
|
$Cache->all |
|
173
|
|
|
); |
|
174
|
2 |
|
Event::instance()->fire( |
|
175
|
2 |
|
'System/User/Group/del/after', |
|
176
|
|
|
[ |
|
177
|
2 |
|
'id' => $id |
|
178
|
|
|
] |
|
179
|
|
|
); |
|
180
|
|
|
} |
|
181
|
2 |
|
return (bool)$result; |
|
182
|
|
|
} |
|
183
|
|
|
/** |
|
184
|
|
|
* Get group permissions |
|
185
|
|
|
* |
|
186
|
|
|
* @param int $group |
|
187
|
|
|
* |
|
188
|
|
|
* @return int[]|false |
|
189
|
|
|
*/ |
|
190
|
4 |
|
function get_permissions ($group) { |
|
191
|
4 |
|
return $this->get_any_permissions($group, 'group'); |
|
192
|
|
|
} |
|
193
|
|
|
/** |
|
194
|
|
|
* Set group permissions |
|
195
|
|
|
* |
|
196
|
|
|
* @param array $data |
|
197
|
|
|
* @param int $group |
|
198
|
|
|
* |
|
199
|
|
|
* @return bool |
|
200
|
|
|
*/ |
|
201
|
4 |
|
function set_permissions ($data, $group) { |
|
202
|
4 |
|
return $this->set_any_permissions($data, (int)$group, 'group'); |
|
203
|
|
|
} |
|
204
|
|
|
/** |
|
205
|
|
|
* Delete all permissions of specified group |
|
206
|
|
|
* |
|
207
|
|
|
* @param int $group |
|
208
|
|
|
* |
|
209
|
|
|
* @return bool |
|
210
|
|
|
*/ |
|
211
|
2 |
|
function del_permissions_all ($group) { |
|
212
|
2 |
|
return $this->del_any_permissions_all((int)$group, 'group'); |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.