-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathViewManageRoles.php
More file actions
376 lines (337 loc) · 12.5 KB
/
Copy pathViewManageRoles.php
File metadata and controls
376 lines (337 loc) · 12.5 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
<?php
/*****************************************************************************
*
* ViewManageRoles.php - Dialog for managing roles and permissions
*
* Copyright (c) 2004-2016 NagVis Project (Contact: info@nagvis.org)
*
* License:
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*****************************************************************************/
class ViewManageRoles
{
/** @var string|null */
private $error = null;
/**
* @return void
* @throws FieldInputError
* @throws NagVisException
*/
private function addForm()
{
/** @var CoreAuthorisationHandler $AUTHORISATION */
global $AUTHORISATION;
echo '<h2>' . l('Create Role') . '</h2>';
if (is_action() && post('mode') == 'create') {
try {
$name = post('name');
if (!$name) {
throw new FieldInputError('name', l('Please specify a name'));
}
if (strlen($name) > AUTH_MAX_ROLENAME_LENGTH) {
throw new FieldInputError('name', l('This name is too long'));
}
if (!preg_match(MATCH_ROLE_NAME, $name)) {
throw new FieldInputError(
'name',
l('Invalid value provided. Needs to match: [P].', ['P' => MATCH_ROLE_NAME])
);
}
if ($AUTHORISATION->checkRoleExists($name)) {
throw new FieldInputError('name', l('A role with this name already exists.'));
}
if ($AUTHORISATION->createRole($name)) {
success(l('The role has been created.'));
} else {
throw new NagVisException('Failed to create the role');
}
} catch (FieldInputError $e) {
form_error($e->field, $e->msg);
} catch (NagVisException $e) {
form_error(null, $e->message());
} catch (Exception $e) {
if (isset($e->msg)) {
form_error(null, $e->msg);
} else {
throw $e;
}
}
}
echo $this->error;
js_form_start('create');
hidden('mode', 'create');
echo '<table class="mytable">';
echo '<tr><td class="tdlabel">' . l('Name') . '</td>';
echo '<td class="tdfield">';
input('name');
echo '</td></tr>';
echo '</table>';
submit(l('Create'));
form_end();
}
/**
* @return void
* @throws FieldInputError
* @throws NagVisException
*/
private function modifyForm()
{
/** @var CoreAuthorisationHandler $AUTHORISATION */
global $AUTHORISATION;
echo '<h2>' . l('Modify Role') . '</h2>';
$role_id = submitted('edit') ? post('role_id') : null;
if (is_action() && post('mode') == 'edit') {
try {
if ($role_id === null || $role_id === '') {
throw new FieldInputError('role_id', l('Please choose a role to edit.'));
}
if (!is_numeric($role_id)) {
throw new FieldInputError('role_id', l('Invalid value provided.'));
}
$role_id = intval($role_id);
$found = false;
foreach ($AUTHORISATION->getAllRoles() as $role) {
if ($role['roleId'] == $role_id) {
$found = true;
}
}
if (!$found) {
throw new NagVisException('Invalid role id provided');
}
// Load permissions from parameters
$perms = [];
foreach (array_keys($_POST) as $key) {
if (str_contains($key, 'perm_')) {
$key_parts = explode('_', $key);
$perm_id = $key_parts[1];
$perms[$perm_id] = get_checkbox($key);
}
}
scroll_up(); // On success, always scroll to top of page
if ($AUTHORISATION->updateRolePerms($role_id, $perms)) {
success(l('The permissions for this role have been updated.'));
} else {
throw new NagVisException(l('Problem while updating role permissions.'));
}
} catch (FieldInputError $e) {
form_error($e->field, $e->msg);
} catch (NagVisException $e) {
form_error(null, $e->message());
} catch (Exception $e) {
if (isset($e->msg)) {
form_error(null, $e->msg);
} else {
throw $e;
}
}
}
echo $this->error;
js_form_start('edit');
hidden('mode', 'edit');
echo '<table class="mytable">';
echo '<tr><td class="tdlabel">' . l('Select Role') . '</td>';
echo '<td class="tdfield">';
$choices = ['' => l('Please choose')];
foreach ($AUTHORISATION->getAllRoles() as $role) {
$choices[$role['roleId']] = $role['name'];
}
select('role_id', $choices, '', 'updateForm(this.form)');
echo '</td></tr>';
echo '</table>';
$this->renderPermissions($role_id);
submit(l('Save'));
form_end();
}
/**
* @param string $role_id
* @return void
*/
private function renderPermissions($role_id)
{
/** @var CoreAuthorisationHandler $AUTHORISATION */
global $AUTHORISATION;
if (!$role_id) {
return;
}
$sections = [
'general' => l('General'),
'maps' => l('Maps'),
'rotations' => l('Rotations'),
];
echo '<h3>' . l('Permissions') . '</h3>';
$open = get_open_section('general');
render_section_navigation($open, $sections);
$permissions_by_section = [
'general' => [],
'maps' => [],
'rotations' => [],
];
foreach ($AUTHORISATION->getAllVisiblePerms() as $perm) {
if (
$perm['mod'] == 'Map'
&& $perm['act'] != 'add'
&& $perm['act'] != 'manage'
&& $perm['act'] != 'editHtml'
) {
$map_name = $perm['obj'];
if (!isset($permissions_by_section['maps'][$map_name])) {
$permissions_by_section['maps'][$map_name] = [];
}
$permissions_by_section['maps'][$map_name][$perm['act']] = $perm;
} elseif ($perm['mod'] == 'Rotation') {
$permissions_by_section['rotations'][] = $perm;
} else {
$permissions_by_section['general'][] = $perm;
}
}
$permitted = $AUTHORISATION->getRolePerms($role_id);
foreach ($permissions_by_section as $sec => $permissions) {
render_section_start($sec, $open);
if ($sec == 'maps') {
$this->renderMapsSection($permissions, $permitted);
} else {
$this->renderOtherSection($permissions, $permitted);
}
render_section_end();
}
}
public function renderMapsSection($permissions, $permitted)
{
echo '<table class="mytable perms">';
echo '<tr>';
echo '<th>' . l('Map') . '</th>';
echo '<th>' . l('View') . '</th>';
echo '<th>' . l('Edit') . '</th>';
echo '<th>' . l('Delete') . '</th>';
echo '</tr>';
foreach ($permissions as $map_name => $map_perms) {
echo '<tr>';
echo '<td>' . $map_name . '</td>';
$levels = ["view", "edit", "delete"];
foreach ($levels as $level) {
$perm = $map_perms[$level];
unset($_REQUEST['perm_' . $perm['permId']]);
echo '<td class=perm>';
checkbox('perm_' . $perm['permId'], isset($permitted[$perm['permId']]));
echo '</td>';
}
echo '</tr>';
}
echo '</table>';
}
public function renderOtherSection($permissions, $permitted)
{
echo '<table class="mytable perms">';
echo '<tr>';
echo '<th>' . l('Module') . '</th>';
echo '<th>' . l('Action') . '</th>';
echo '<th>' . l('Object') . '</th>';
echo '<th>' . l('Permitted') . '</th>';
echo '</tr>';
foreach ($permissions as $perm) {
unset($_REQUEST['perm_' . $perm['permId']]);
echo '<tr>';
echo '<td>' . $perm['mod'] . '</td>';
echo '<td>' . $perm['act'] . '</td>';
echo '<td>' . $perm['obj'] . '</td>';
echo '<td class=perm>';
checkbox('perm_' . $perm['permId'], isset($permitted[$perm['permId']]));
echo '</td>';
echo '</tr>';
}
echo '</table>';
}
/**
* @return void
* @throws FieldInputError
* @throws NagVisException
*/
private function deleteForm()
{
/** @var CoreAuthorisationHandler $AUTHORISATION */
global $AUTHORISATION;
echo '<h2>' . l('Delete Role') . '</h2>';
if (is_action() && post('mode') == 'delete') {
try {
$role_id = post('role_id');
if ($role_id === null || $role_id === '') {
throw new FieldInputError('role_id', l('Please choose a role to delete.'));
}
if (!is_numeric($role_id)) {
throw new FieldInputError('role_id', l('Invalid value provided.'));
}
$role_id = intval($role_id);
// Check not to delete any referenced role
$used_by = $AUTHORISATION->roleUsedBy($role_id);
if (count($used_by) > 0) {
throw new NagVisException(l(
'Not deleting this role, the role is in use by the users [U].',
['U' => implode(', ', $used_by)]
));
}
if ($AUTHORISATION->deleteRole($role_id)) {
success(l('The role has been deleted.'));
} else {
throw new NagVisException(l('Problem while deleting the role.'));
}
} catch (FieldInputError $e) {
form_error($e->field, $e->msg);
} catch (NagVisException $e) {
form_error(null, $e->message());
} catch (Exception $e) {
if (isset($e->msg)) {
form_error(null, $e->msg);
} else {
throw $e;
}
}
}
echo $this->error;
js_form_start('delete');
hidden('mode', 'delete');
echo '<table class="mytable">';
echo '<tr><td class="tdlabel">' . l('Name') . '</td>';
echo '<td class="tdfield">';
$choices = ['' => l('Please choose')];
foreach ($AUTHORISATION->getAllRoles() as $role) {
$choices[$role['roleId']] = $role['name'];
}
select('role_id', $choices);
echo '</td></tr>';
echo '</table>';
submit(l('Delete'));
form_end();
}
/**
* @return string
* @throws FieldInputError
* @throws NagVisException
*/
public function parse()
{
/** @var CoreAuthorisationHandler $AUTHORISATION */
global $AUTHORISATION;
ob_start();
// Delete permissions, which are not needed anymore when opening the
// "manage roles" dialog. This could be done during usual page
// processing, but would add overhead which is not really needed.
$AUTHORISATION->cleanupPermissions();
$this->addForm();
$this->modifyForm();
$this->deleteForm();
return ob_get_clean();
}
}