Project

General

Profile

Download (13 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * system_groupmanager.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7
 * Copyright (c) 2005 Paul Taylor <paultaylor@winn-dixie.com>
8
 * Copyright (c) 2008 Shrew Soft Inc
9
 * All rights reserved.
10
 *
11
 * originally based on m0n0wall (http://m0n0.ch/wall)
12
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
13
 * All rights reserved.
14
 *
15
 * Licensed under the Apache License, Version 2.0 (the "License");
16
 * you may not use this file except in compliance with the License.
17
 * You may obtain a copy of the License at
18
 *
19
 * http://www.apache.org/licenses/LICENSE-2.0
20
 *
21
 * Unless required by applicable law or agreed to in writing, software
22
 * distributed under the License is distributed on an "AS IS" BASIS,
23
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
 * See the License for the specific language governing permissions and
25
 * limitations under the License.
26
 */
27

    
28
##|+PRIV
29
##|*IDENT=page-system-groupmanager
30
##|*NAME=System: Group Manager
31
##|*DESCR=Allow access to the 'System: Group Manager' page.
32
##|*WARN=standard-warning-root
33
##|*MATCH=system_groupmanager.php*
34
##|-PRIV
35

    
36
require_once("guiconfig.inc");
37

    
38
if (!is_array($config['system']['group'])) {
39
	$config['system']['group'] = array();
40
}
41

    
42
$a_group = &$config['system']['group'];
43

    
44
unset($id);
45
$id = $_REQUEST['groupid'];
46
$act = (isset($_REQUEST['act']) ? $_REQUEST['act'] : '');
47

    
48
function cpusercmp($a, $b) {
49
	return strcasecmp($a['name'], $b['name']);
50
}
51

    
52
function admin_groups_sort() {
53
	global $a_group;
54

    
55
	if (!is_array($a_group)) {
56
		return;
57
	}
58

    
59
	usort($a_group, "cpusercmp");
60
}
61

    
62
if ($_POST['act'] == "delgroup") {
63

    
64
	if (!isset($id) || !isset($_REQUEST['groupname']) || !isset($a_group[$id]) || ($_REQUEST['groupname'] != $a_group[$id]['name'])) {
65
		pfSenseHeader("system_groupmanager.php");
66
		exit;
67
	}
68

    
69
	local_group_del($a_group[$id]);
70
	$groupdeleted = $a_group[$id]['name'];
71
	unset($a_group[$id]);
72
	write_config();
73
	$savemsg = sprintf(gettext("Group %s successfully deleted."), $groupdeleted);
74
}
75

    
76
if ($_POST['act'] == "delpriv") {
77

    
78
	if (!isset($id) || !isset($a_group[$id])) {
79
		pfSenseHeader("system_groupmanager.php");
80
		exit;
81
	}
82

    
83
	$privdeleted = $priv_list[$a_group[$id]['priv'][$_REQUEST['privid']]]['name'];
84
	unset($a_group[$id]['priv'][$_REQUEST['privid']]);
85

    
86
	if (is_array($a_group[$id]['member'])) {
87
		foreach ($a_group[$id]['member'] as $uid) {
88
			$user = getUserEntryByUID($uid);
89
			if ($user) {
90
				local_user_set($user);
91
			}
92
		}
93
	}
94

    
95
	write_config();
96
	$act = "edit";
97
	$savemsg = sprintf(gettext("Privilege %s successfully deleted."), $privdeleted);
98
}
99

    
100
if ($act == "edit") {
101
	if (isset($id) && isset($a_group[$id])) {
102
		$pconfig['name'] = $a_group[$id]['name'];
103
		$pconfig['gid'] = $a_group[$id]['gid'];
104
		$pconfig['gtype'] = empty($a_group[$id]['scope']) ? "local" : $a_group[$id]['scope'];
105
		$pconfig['description'] = $a_group[$id]['description'];
106
		$pconfig['members'] = $a_group[$id]['member'];
107
		$pconfig['priv'] = $a_group[$id]['priv'];
108
	}
109
}
110

    
111
if (isset($_POST['dellall_x'])) {
112

    
113
	$del_groups = $_POST['delete_check'];
114

    
115
	if (!empty($del_groups)) {
116
		foreach ($del_groups as $groupid) {
117
			if (isset($a_group[$groupid]) && $a_group[$groupid]['scope'] != "system") {
118
				local_group_del($a_group[$groupid]);
119
				unset($a_group[$groupid]);
120
			}
121
		}
122
		$savemsg = gettext("Selected groups removed successfully.");
123
		write_config($savemsg);
124
	}
125
}
126

    
127
if (isset($_POST['save'])) {
128
	unset($input_errors);
129
	$pconfig = $_POST;
130

    
131
	/* input validation */
132
	$reqdfields = explode(" ", "groupname");
133
	$reqdfieldsn = array(gettext("Group Name"));
134

    
135
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
136

    
137
	if ($_POST['gtype'] != "remote") {
138
		if (preg_match("/[^a-zA-Z0-9\.\-_]/", $_POST['groupname'])) {
139
			$input_errors[] = sprintf(gettext("The (%s) group name contains invalid characters."), $_POST['gtype']);
140
		}
141
	} else {
142
		if (preg_match("/[^a-zA-Z0-9\.\- _]/", $_POST['groupname'])) {
143
			$input_errors[] = sprintf(gettext("The (%s) group name contains invalid characters."), $_POST['gtype']);
144
		}
145
	}
146

    
147
	if (strlen($_POST['groupname']) > 16) {
148
		$input_errors[] = gettext("The group name is longer than 16 characters.");
149
	}
150

    
151
	/* Check the POSTed members to ensure they are valid and exist */
152
	if (is_array($_POST['members'])) {
153
		foreach ($_POST['members'] as $newmember) {
154
			if (!is_numeric($newmember) || empty(getUserEntryByUID($newmember))) {
155
				$input_errors[] = gettext("One or more invalid group members was submitted.");
156
			}
157
		}
158
	}
159

    
160
	if (!$input_errors && !(isset($id) && $a_group[$id])) {
161
		/* make sure there are no dupes */
162
		foreach ($a_group as $group) {
163
			if ($group['name'] == $_POST['groupname']) {
164
				$input_errors[] = gettext("Another entry with the same group name already exists.");
165
				break;
166
			}
167
		}
168
	}
169

    
170
	if (!$input_errors) {
171
		$group = array();
172
		if (isset($id) && $a_group[$id]) {
173
			$group = $a_group[$id];
174
		}
175

    
176
		$group['name'] = $_POST['groupname'];
177
		$group['description'] = $_POST['description'];
178
		$group['scope'] = $_POST['gtype'];
179

    
180
		if (empty($_POST['members'])) {
181
			unset($group['member']);
182
		} else if ($group['gid'] != 1998) { // all group
183
			$group['member'] = $_POST['members'];
184
		}
185

    
186
		if (isset($id) && $a_group[$id]) {
187
			$a_group[$id] = $group;
188
		} else {
189
			$group['gid'] = $config['system']['nextgid']++;
190
			$a_group[] = $group;
191
		}
192

    
193
		admin_groups_sort();
194

    
195
		local_group_set($group);
196

    
197
		/* Refresh users in this group since their privileges may have changed. */
198
		if (is_array($group['member'])) {
199
			$a_user = &$config['system']['user'];
200
			foreach ($a_user as & $user) {
201
				if (in_array($user['uid'], $group['member'])) {
202
					local_user_set($user);
203
				}
204
			}
205
		}
206

    
207
		write_config();
208

    
209
		header("Location: system_groupmanager.php");
210
		exit;
211
	}
212

    
213
	$pconfig['name'] = $_POST['groupname'];
214
}
215

    
216
function build_priv_table() {
217
	global $a_group, $id;
218

    
219
	$privhtml = '<div class="table-responsive">';
220
	$privhtml .=	'<table class="table table-striped table-hover table-condensed">';
221
	$privhtml .=		'<thead>';
222
	$privhtml .=			'<tr>';
223
	$privhtml .=				'<th>' . gettext('Name') . '</th>';
224
	$privhtml .=				'<th>' . gettext('Description') . '</th>';
225
	$privhtml .=				'<th>' . gettext('Action') . '</th>';
226
	$privhtml .=			'</tr>';
227
	$privhtml .=		'</thead>';
228
	$privhtml .=		'<tbody>';
229

    
230
	$user_has_root_priv = false;
231

    
232
	foreach (get_user_privdesc($a_group[$id]) as $i => $priv) {
233
		$privhtml .=		'<tr>';
234
		$privhtml .=			'<td>' . htmlspecialchars($priv['name']) . '</td>';
235
		$privhtml .=			'<td>' . htmlspecialchars($priv['descr']);
236
		if (isset($priv['warn']) && ($priv['warn'] == 'standard-warning-root')) {
237
			$privhtml .=			' ' . gettext('(admin privilege)');
238
			$user_has_root_priv = true;
239
		}
240
		$privhtml .=			'</td>';
241
		$privhtml .=			'<td><a class="fa fa-trash" title="' . gettext('Delete Privilege') . '"	href="system_groupmanager.php?act=delpriv&amp;groupid=' . $id . '&amp;privid=' . $i . '" usepost></a></td>';
242
		$privhtml .=		'</tr>';
243

    
244
	}
245

    
246
	if ($user_has_root_priv) {
247
		$privhtml .=		'<tr>';
248
		$privhtml .=			'<td colspan="2">';
249
		$privhtml .=				'<b>' . gettext('Security notice: Users in this group effectively have administrator-level access') . '</b>';
250
		$privhtml .=			'</td>';
251
		$privhtml .=			'<td>';
252
		$privhtml .=			'</td>';
253
		$privhtml .=		'</tr>';
254

    
255
	}
256

    
257
	$privhtml .=		'</tbody>';
258
	$privhtml .=	'</table>';
259
	$privhtml .= '</div>';
260

    
261
	$privhtml .= '<nav class="action-buttons">';
262
	$privhtml .=	'<a href="system_groupmanager_addprivs.php?groupid=' . $id . '" class="btn btn-success"><i class="fa fa-plus icon-embed-btn"></i>' . gettext("Add") . '</a>';
263
	$privhtml .= '</nav>';
264

    
265
	return($privhtml);
266
}
267

    
268
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Groups"));
269
$pglinks = array("", "system_usermanager.php", "system_groupmanager.php");
270

    
271
if ($act == "new" || $act == "edit") {
272
	$pgtitle[] = gettext('Edit');
273
	$pglinks[] = "@self";
274
}
275

    
276
include("head.inc");
277

    
278
if ($input_errors) {
279
	print_input_errors($input_errors);
280
}
281

    
282
if ($savemsg) {
283
	print_info_box($savemsg, 'success');
284
}
285

    
286
$tab_array = array();
287
$tab_array[] = array(gettext("Users"), false, "system_usermanager.php");
288
$tab_array[] = array(gettext("Groups"), true, "system_groupmanager.php");
289
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
290
$tab_array[] = array(gettext("Authentication Servers"), false, "system_authservers.php");
291
display_top_tabs($tab_array);
292

    
293
if (!($act == "new" || $act == "edit")) {
294
?>
295
<div class="panel panel-default">
296
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Groups')?></h2></div>
297
	<div class="panel-body">
298
		<div class="table-responsive">
299
			<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap table-rowdblclickedit" data-sortable>
300
				<thead>
301
					<tr>
302
						<th><?=gettext("Group name")?></th>
303
						<th><?=gettext("Description")?></th>
304
						<th><?=gettext("Member Count")?></th>
305
						<th><?=gettext("Actions")?></th>
306
					</tr>
307
				</thead>
308
				<tbody>
309
<?php
310
	foreach ($a_group as $i => $group):
311
		if ($group["name"] == "all") {
312
			$groupcount = count($config['system']['user']);
313
		} else {
314
			$groupcount = count($group['member']);
315
		}
316
?>
317
					<tr>
318
						<td>
319
							<?=htmlspecialchars($group['name'])?>
320
						</td>
321
						<td>
322
							<?=htmlspecialchars($group['description'])?>
323
						</td>
324
						<td>
325
							<?=$groupcount?>
326
						</td>
327
						<td>
328
							<a class="fa fa-pencil" title="<?=gettext("Edit group"); ?>" href="?act=edit&amp;groupid=<?=$i?>"></a>
329
							<?php if ($group['scope'] != "system"): ?>
330
								<a class="fa fa-trash"	title="<?=gettext("Delete group")?>" href="?act=delgroup&amp;groupid=<?=$i?>&amp;groupname=<?=$group['name']?>" usepost></a>
331
							<?php endif;?>
332
						</td>
333
					</tr>
334
<?php
335
	endforeach;
336
?>
337
				</tbody>
338
			</table>
339
		</div>
340
	</div>
341
</div>
342

    
343
<nav class="action-buttons">
344
	<a href="?act=new" class="btn btn-success btn-sm">
345
		<i class="fa fa-plus icon-embed-btn"></i>
346
		<?=gettext("Add")?>
347
	</a>
348
</nav>
349
<?php
350
	include('foot.inc');
351
	exit;
352
}
353

    
354
$form = new Form;
355
$form->setAction('system_groupmanager.php?act=edit');
356
$form->addGlobal(new Form_Input(
357
	'groupid',
358
	null,
359
	'hidden',
360
	$id
361
));
362

    
363
if (isset($id) && $a_group[$id]) {
364
	$form->addGlobal(new Form_Input(
365
		'id',
366
		null,
367
		'hidden',
368
		$id
369
	));
370

    
371
	$form->addGlobal(new Form_Input(
372
		'gid',
373
		null,
374
		'hidden',
375
		$pconfig['gid']
376
	));
377
}
378

    
379
$section = new Form_Section('Group Properties');
380

    
381
$section->addInput($input = new Form_Input(
382
	'groupname',
383
	'*Group name',
384
	'text',
385
	$pconfig['name']
386
));
387

    
388
if ($pconfig['gtype'] == "system") {
389
	$input->setReadonly();
390

    
391
	$section->addInput(new Form_Input(
392
		'gtype',
393
		'*Scope',
394
		'text',
395
		$pconfig['gtype']
396
	))->setReadonly();
397
} else {
398
	$section->addInput(new Form_Select(
399
		'gtype',
400
		'*Scope',
401
		$pconfig['gtype'],
402
		["local" => gettext("Local"), "remote" => gettext("Remote")]
403
	));
404
}
405

    
406
$section->addInput(new Form_Input(
407
	'description',
408
	'Description',
409
	'text',
410
	$pconfig['description']
411
))->setHelp('Group description, for administrative information only');
412

    
413

    
414
$form->add($section);
415
if ($pconfig['gid'] != 1998) { // all users group
416

    
417
	// ==== Group membership ==================================================
418
	$group = new Form_Group('Group membership');
419

    
420
	// Make a list of all the groups configured on the system, and a list of
421
	// those which this user is a member of
422
	$systemGroups = array();
423
	$usersGroups = array();
424

    
425
	foreach ($config['system']['user'] as $user) {
426
		if (is_array($pconfig['members']) && in_array($user['uid'], $pconfig['members'])) {
427
			$usersGroups[ $user['uid'] ] = $user['name'];	// Add it to the user's list
428
		} else {
429
			$systemGroups[ $user['uid'] ] = $user['name']; // Add it to the 'not a member of' list
430
		}
431
	}
432

    
433
	$group->add(new Form_Select(
434
		'notmembers',
435
		null,
436
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
437
		$systemGroups,
438
		true
439
	))->setHelp('Not members');
440

    
441
	$group->add(new Form_Select(
442
		'members',
443
		null,
444
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
445
		$usersGroups,
446
		true
447
	))->setHelp('Members');
448

    
449
	$section->add($group);
450

    
451
	$group = new Form_Group('');
452

    
453
	$group->add(new Form_Button(
454
		'movetoenabled',
455
		'Move to "Members"',
456
		null,
457
		'fa-angle-double-right'
458
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass('btn-info btn-sm');
459

    
460
	$group->add(new Form_Button(
461
		'movetodisabled',
462
		'Move to "Not members',
463
		null,
464
		'fa-angle-double-left'
465
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass('btn-info btn-sm');
466

    
467
	$group->setHelp('Hold down CTRL (PC)/COMMAND (Mac) key to select multiple items.');
468
	$section->add($group);
469

    
470
}
471

    
472
if ($_POST['act'] != "new") {
473
	$section = new Form_Section('Assigned Privileges');
474

    
475
	$section->addInput(new Form_StaticText(
476
		null,
477
		build_priv_table()
478
	));
479

    
480

    
481
	$form->add($section);
482
}
483

    
484
print $form;
485
?>
486
<script type="text/javascript">
487
//<![CDATA[
488
events.push(function() {
489

    
490
	// On click . .
491
	$("#movetodisabled").click(function() {
492
		moveOptions($('[name="members[]"] option'), $('[name="notmembers[]"]'));
493
	});
494

    
495
	$("#movetoenabled").click(function() {
496
		moveOptions($('[name="notmembers[]"] option'), $('[name="members[]"]'));
497
	});
498

    
499
	// On submit mark all the user's groups as "selected"
500
	$('form').submit(function() {
501
		AllServers($('[name="members[]"] option'), true);
502
	});
503
});
504
//]]>
505
</script>
506
<?php
507
include('foot.inc');
(197-197/223)