1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Functions to support the permissions controller |
5
|
|
|
* |
6
|
|
|
* @name ElkArte Forum |
7
|
|
|
* @copyright ElkArte Forum contributors |
8
|
|
|
* @license BSD http://opensource.org/licenses/BSD-3-Clause |
9
|
|
|
* |
10
|
|
|
* @version 2.0 dev |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace ElkArte\sources\subs\SettingsFormAdapter; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class to initialize inline permissions sub-form and save its settings |
17
|
|
|
*/ |
18
|
|
|
class InlinePermissions extends Adapter |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $permissions = array(); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string[] |
27
|
|
|
*/ |
28
|
|
|
private $permissionList = array(); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string[] |
32
|
|
|
*/ |
33
|
|
|
private $illegal_permissions = array(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string[] |
37
|
|
|
*/ |
38
|
|
|
private $illegal_guest_permissions = array(); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var int[] |
42
|
|
|
*/ |
43
|
|
|
private $excluded_groups = array(); |
44
|
|
|
|
45
|
|
|
private $permissionsObject; |
46
|
|
|
private $db; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string[] |
50
|
|
|
*/ |
51
|
|
|
public function getPermissions() |
52
|
|
|
{ |
53
|
|
|
return $this->permissions; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string[] $permissions |
58
|
|
|
*/ |
59
|
4 |
|
public function setPermissions($permissions) |
60
|
|
|
{ |
61
|
4 |
|
$this->permissions = $permissions; |
62
|
|
|
|
63
|
|
|
// Load the permission list |
64
|
4 |
|
$this->permissionList = array_map( |
65
|
4 |
|
function ($permission) |
66
|
|
|
{ |
67
|
4 |
|
return $permission[1]; |
68
|
4 |
|
}, $this->permissions |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
// Load the permission settings that guests cannot have |
72
|
4 |
|
$this->illegal_guest_permissions = array_intersect( |
73
|
4 |
|
$this->permissionList, |
74
|
4 |
|
$this->permissionsObject->getIllegalGuestPermissions() |
75
|
|
|
); |
76
|
4 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return int[] |
80
|
|
|
*/ |
81
|
|
|
public function getExcludedGroups() |
82
|
|
|
{ |
83
|
|
|
return $this->excluded_groups; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param int[] $excluded_groups |
88
|
|
|
*/ |
89
|
2 |
|
public function setExcludedGroups($excluded_groups) |
90
|
|
|
{ |
91
|
2 |
|
$this->excluded_groups = $excluded_groups; |
92
|
2 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* InlinePermissions constructor. |
96
|
|
|
*/ |
97
|
4 |
|
public function __construct() |
98
|
|
|
{ |
99
|
4 |
|
$this->db = database(); |
100
|
|
|
|
101
|
|
|
// Make sure they can't do certain things, |
102
|
|
|
// unless they have the right permissions. |
103
|
4 |
|
$this->permissionsObject = new \Permissions; |
104
|
4 |
|
$this->illegal_permissions = $this->permissionsObject->getIllegalPermissions(); |
105
|
4 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Save the permissions of a form containing inline permissions. |
109
|
|
|
*/ |
110
|
2 |
|
public function save() |
111
|
|
|
{ |
112
|
|
|
// No permissions? Not a great deal to do here. |
113
|
2 |
|
if (!allowedTo('manage_permissions')) |
114
|
|
|
{ |
115
|
|
|
return; |
116
|
|
|
} |
117
|
2 |
|
$insertRows = array(); |
118
|
2 |
|
foreach ($this->permissionList as $permission) |
119
|
|
|
{ |
120
|
2 |
|
if (!isset($_POST[$permission])) |
121
|
|
|
{ |
122
|
1 |
|
continue; |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
foreach ($_POST[$permission] as $id_group => $value) |
126
|
|
|
{ |
127
|
2 |
|
if (in_array($value, array('on', 'deny')) && !in_array($permission, $this->illegal_permissions)) |
128
|
|
|
{ |
129
|
2 |
|
$insertRows[] = array($permission, (int) $id_group, $value == 'on' ? 1 : 0); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// Remove the old permissions... |
135
|
2 |
|
$this->permissionsObject->deletePermissions($this->permissionList); |
136
|
|
|
|
137
|
|
|
// ...and replace them with new ones. |
138
|
2 |
|
require_once(SUBSDIR . '/ManagePermissions.subs.php'); |
139
|
2 |
|
replacePermission($insertRows); |
140
|
|
|
|
141
|
|
|
// Do a full child update. |
142
|
2 |
|
$this->permissionsObject->updateChild(array(), -1); |
143
|
|
|
|
144
|
|
|
// Just in case we cached this. |
145
|
2 |
|
updateSettings(array('settings_updated' => time())); |
146
|
2 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Initialize a form with inline permissions settings. |
150
|
|
|
* It loads a context variables for each permission. |
151
|
|
|
* This function is used by several settings screens to set specific permissions. |
152
|
|
|
* |
153
|
|
|
* @uses ManagePermissions language |
154
|
|
|
* @uses ManagePermissions template |
155
|
|
|
*/ |
156
|
4 |
|
public function prepare() |
157
|
|
|
{ |
158
|
4 |
|
global $modSettings; |
159
|
|
|
|
160
|
|
|
// No permissions? Not a great deal to do here. |
161
|
4 |
|
if (!allowedTo('manage_permissions')) |
162
|
|
|
{ |
163
|
|
|
return; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
// Load the permission settings for guests |
167
|
4 |
|
foreach ($this->permissions as $permission) |
168
|
|
|
{ |
169
|
4 |
|
$this->context[$permission[1]] = array( |
170
|
4 |
|
-1 => array( |
171
|
|
|
'id' => -1, |
172
|
|
|
'is_postgroup' => false, |
173
|
|
|
'status' => 'off', |
174
|
|
|
), |
175
|
|
|
0 => array( |
176
|
|
|
'id' => 0, |
177
|
|
|
'is_postgroup' => false, |
178
|
|
|
'status' => 'off', |
179
|
|
|
), |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
4 |
|
$request = $this->db->query('', ' |
184
|
|
|
SELECT id_group, CASE WHEN add_deny = {int:denied} THEN {string:deny} ELSE {string:on} END AS status, permission |
185
|
|
|
FROM {db_prefix}permissions |
186
|
|
|
WHERE id_group IN (-1, 0) |
187
|
|
|
AND permission IN ({array_string:permissions})', |
188
|
|
|
array( |
189
|
4 |
|
'denied' => 0, |
190
|
4 |
|
'permissions' => $this->permissionList, |
191
|
4 |
|
'deny' => 'deny', |
192
|
4 |
|
'on' => 'on', |
193
|
|
|
) |
194
|
|
|
); |
195
|
4 |
|
while ($row = $this->db->fetch_assoc($request)) |
196
|
|
|
{ |
197
|
2 |
|
$this->context[$row['permission']][$row['id_group']]['status'] = $row['status']; |
198
|
|
|
} |
199
|
4 |
|
$this->db->free_result($request); |
200
|
|
|
|
201
|
4 |
|
$request = $this->db->query('', ' |
202
|
|
|
SELECT mg.id_group, mg.group_name, mg.min_posts, COALESCE(p.add_deny, -1) AS status, p.permission |
203
|
|
|
FROM {db_prefix}membergroups AS mg |
204
|
|
|
LEFT JOIN {db_prefix}permissions AS p ON (p.id_group = mg.id_group AND p.permission IN ({array_string:permissions})) |
205
|
|
|
WHERE mg.id_group NOT IN (1, 3) |
206
|
4 |
|
AND mg.id_parent = {int:not_inherited}' . (empty($modSettings['permission_enable_postgroups']) ? ' |
207
|
4 |
|
AND mg.min_posts = {int:min_posts}' : '') . ' |
208
|
|
|
ORDER BY mg.min_posts, CASE WHEN mg.id_group < {int:newbie_group} THEN mg.id_group ELSE 4 END, mg.group_name', |
209
|
|
|
array( |
210
|
4 |
|
'not_inherited' => -2, |
211
|
|
|
'min_posts' => -1, |
212
|
4 |
|
'newbie_group' => 4, |
213
|
4 |
|
'permissions' => $this->permissionList, |
214
|
|
|
) |
215
|
|
|
); |
216
|
4 |
|
while ($row = $this->db->fetch_assoc($request)) |
217
|
|
|
{ |
218
|
|
|
// Initialize each permission as being 'off' until proven otherwise. |
219
|
4 |
|
foreach ($this->permissions as $permission) |
220
|
|
|
{ |
221
|
4 |
|
if (!isset($this->context[$permission[1]][$row['id_group']])) |
222
|
|
|
{ |
223
|
4 |
|
$this->context[$permission[1]][$row['id_group']] = array( |
224
|
4 |
|
'id' => $row['id_group'], |
225
|
4 |
|
'name' => $row['group_name'], |
226
|
4 |
|
'is_postgroup' => $row['min_posts'] != -1, |
227
|
4 |
|
'status' => 'off', |
228
|
|
|
); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
4 |
|
$this->context[$row['permission']][$row['id_group']]['status'] = empty($row['status']) ? 'deny' : ($row['status'] == 1 ? 'on' : 'off'); |
233
|
|
|
} |
234
|
4 |
|
$this->db->free_result($request); |
235
|
4 |
|
$this->filterIllegalPermissions(); |
236
|
4 |
|
$this->prepareContext(); |
237
|
4 |
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Prepare the template by loading context |
241
|
|
|
* variables for each permission. |
242
|
|
|
* |
243
|
|
|
* @uses ManagePermissions template |
244
|
|
|
* @uses ManagePermissions languuge |
245
|
|
|
*/ |
246
|
4 |
|
protected function prepareContext() |
247
|
|
|
{ |
248
|
4 |
|
global $context, $txt; |
249
|
|
|
|
250
|
4 |
|
theme()->getTemplates()->load('ManagePermissions'); |
251
|
4 |
|
theme()->getTemplates()->loadLanguageFile('ManagePermissions'); |
252
|
|
|
|
253
|
|
|
// Load the names for guests |
254
|
4 |
|
foreach ($this->permissions as $permission) |
255
|
|
|
{ |
256
|
4 |
View Code Duplication |
if (isset($this->context[$permission[1]][-1])) |
257
|
|
|
{ |
258
|
4 |
|
$this->context[$permission[1]][-1]['name'] = $txt['membergroups_guests']; |
259
|
|
|
} |
260
|
4 |
View Code Duplication |
if (isset($this->context[$permission[1]][0])) |
261
|
|
|
{ |
262
|
4 |
|
$this->context[$permission[1]][0]['name'] = $txt['membergroups_members']; |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
266
|
4 |
|
$context['permissions'] = $this->context; |
267
|
4 |
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Some permissions cannot be given to certain groups. Remove them. |
271
|
|
|
*/ |
272
|
4 |
|
private function filterIllegalPermissions() |
273
|
|
|
{ |
274
|
4 |
|
foreach ($this->permissions as $permission) |
275
|
|
|
{ |
276
|
4 |
|
$excluded_groups = $this->excluded_groups; |
277
|
4 |
|
if (isset($permission['excluded_groups'])) |
278
|
|
|
{ |
279
|
2 |
|
$excluded_groups += $permission['excluded_groups']; |
280
|
|
|
} |
281
|
4 |
|
foreach ($excluded_groups as $group) |
282
|
|
|
{ |
283
|
2 |
|
if (isset($this->context[$permission[1]][$group])) |
284
|
|
|
{ |
285
|
2 |
|
unset($this->context[$permission[1]][$group]); |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
// Is this permission one that guests can't have? |
289
|
4 |
|
if (in_array($permission[1], $this->illegal_guest_permissions)) |
290
|
|
|
{ |
291
|
2 |
|
unset($this->context[$permission[1]][-1]); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
// Is this permission outright disabled? |
295
|
4 |
|
if (in_array($permission[1], $this->illegal_permissions)) |
296
|
|
|
{ |
297
|
4 |
|
unset($this->context[$permission[1]]); |
298
|
|
|
} |
299
|
|
|
} |
300
|
4 |
|
} |
301
|
|
|
} |
302
|
|
|
|