Project

General

Profile

Download (14.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	system_groupmanager.php
4
*/
5
/* ====================================================================
6
 *	Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *	Copyright (c)  2005 Paul Taylor <paultaylor@winn-dixie.com>
8
 *	Copyright (c)  2008 Shrew Soft Inc
9
 *
10
 *	Some or all of this file is based on the m0n0wall project which is
11
 *	Copyright (c)  2004 Manuel Kasper (BSD 2 clause)
12
 *
13
 *	Redistribution and use in source and binary forms, with or without modification,
14
 *	are permitted provided that the following conditions are met:
15
 *
16
 *	1. Redistributions of source code must retain the above copyright notice,
17
 *		this list of conditions and the following disclaimer.
18
 *
19
 *	2. Redistributions in binary form must reproduce the above copyright
20
 *		notice, this list of conditions and the following disclaimer in
21
 *		the documentation and/or other materials provided with the
22
 *		distribution.
23
 *
24
 *	3. All advertising materials mentioning features or use of this software
25
 *		must display the following acknowledgment:
26
 *		"This product includes software developed by the pfSense Project
27
 *		 for use in the pfSense software distribution. (http://www.pfsense.org/).
28
 *
29
 *	4. The names "pfSense" and "pfSense Project" must not be used to
30
 *		 endorse or promote products derived from this software without
31
 *		 prior written permission. For written permission, please contact
32
 *		 coreteam@pfsense.org.
33
 *
34
 *	5. Products derived from this software may not be called "pfSense"
35
 *		nor may "pfSense" appear in their names without prior written
36
 *		permission of the Electric Sheep Fencing, LLC.
37
 *
38
 *	6. Redistributions of any form whatsoever must retain the following
39
 *		acknowledgment:
40
 *
41
 *	"This product includes software developed by the pfSense Project
42
 *	for use in the pfSense software distribution (http://www.pfsense.org/).
43
 *
44
 *	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
45
 *	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46
 *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
47
 *	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
48
 *	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49
 *	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50
 *	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52
 *	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
53
 *	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
 *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
55
 *	OF THE POSSIBILITY OF SUCH DAMAGE.
56
 *
57
 *	====================================================================
58
 *
59
 */
60

    
61
##|+PRIV
62
##|*IDENT=page-system-groupmanager
63
##|*NAME=System: Group manager
64
##|*DESCR=Allow access to the 'System: Group manager' page.
65
##|*MATCH=system_groupmanager.php*
66
##|-PRIV
67

    
68
require("guiconfig.inc");
69

    
70
if (!is_array($config['system']['group'])) {
71
	$config['system']['group'] = array();
72
}
73

    
74
$a_group = &$config['system']['group'];
75

    
76
unset($id);
77

    
78
if (isset($_POST['groupid']) && is_numericint($_POST['groupid'])) {
79
	$id = $_POST['groupid'];
80
}
81

    
82
if (isset($_GET['groupid']) && is_numericint($_GET['groupid'])) {
83
	$id = $_GET['groupid'];
84
}
85

    
86
$act = (isset($_GET['act']) ? $_GET['act'] : '');
87

    
88
function cpusercmp($a, $b) {
89
	return strcasecmp($a['name'], $b['name']);
90
}
91

    
92
function admin_groups_sort() {
93
	global $a_group;
94

    
95
	if (!is_array($a_group)) {
96
		return;
97
	}
98

    
99
	usort($a_group, "cpusercmp");
100
}
101

    
102
if ($act == "delgroup") {
103

    
104
	if (!isset($id) || !isset($_GET['groupname']) || !isset($a_group[$id]) || ($_GET['groupname'] != $a_group[$id]['name'])) {
105
		pfSenseHeader("system_groupmanager.php");
106
		exit;
107
	}
108

    
109
	conf_mount_rw();
110
	local_group_del($a_group[$id]);
111
	conf_mount_ro();
112
	$groupdeleted = $a_group[$id]['name'];
113
	unset($a_group[$id]);
114
	write_config();
115
	$savemsg = sprintf(gettext("Group %s successfully deleted."), $groupdeleted);
116
}
117

    
118
if ($act == "delpriv") {
119

    
120
	if (!isset($id) || !isset($a_group[$id])) {
121
		pfSenseHeader("system_groupmanager.php");
122
		exit;
123
	}
124

    
125
	$privdeleted = $priv_list[$a_group[$id]['priv'][$_POST['privid']]]['name'];
126
	unset($a_group[$id]['priv'][$_GET['privid']]);
127

    
128
	if (is_array($a_group[$id]['member'])) {
129
		foreach ($a_group[$id]['member'] as $uid) {
130
			$user = getUserEntryByUID($uid);
131
			if ($user) {
132
				local_user_set($user);
133
			}
134
		}
135
	}
136

    
137
	write_config();
138
	$act = "edit";
139
	$savemsg = sprintf(gettext("Privilege %s successfully deleted."), $privdeleted);
140
}
141

    
142
if ($act == "edit") {
143
	if (isset($id) && isset($a_group[$id])) {
144
		$pconfig['name'] = $a_group[$id]['name'];
145
		$pconfig['gid'] = $a_group[$id]['gid'];
146
		$pconfig['gtype'] = empty($a_group[$id]['scope']) ? "local" : $a_group[$id]['scope'];
147
		$pconfig['description'] = $a_group[$id]['description'];
148
		$pconfig['members'] = $a_group[$id]['member'];
149
		$pconfig['priv'] = $a_group[$id]['priv'];
150
	}
151
}
152

    
153
if (isset($_GET['dellall_x'])) {
154

    
155
	$del_groups = $_GET['delete_check'];
156

    
157
	if (!empty($del_groups)) {
158
		foreach ($del_groups as $groupid) {
159
			if (isset($a_group[$groupid]) && $a_group[$groupid]['scope'] != "system") {
160
				conf_mount_rw();
161
				local_group_del($a_group[$groupid]);
162
				conf_mount_ro();
163
				unset($a_group[$groupid]);
164
			}
165
		}
166
		$savemsg = gettext("Selected groups removed successfully.");
167
		write_config($savemsg);
168
	}
169
}
170

    
171
if (isset($_POST['save'])) {
172
	unset($input_errors);
173
	$pconfig = $_POST;
174

    
175
	/* input validation */
176
	$reqdfields = explode(" ", "groupname");
177
	$reqdfieldsn = array(gettext("Group Name"));
178

    
179
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
180

    
181
	if ($_POST['gtype'] != "remote") {
182
		if (preg_match("/[^a-zA-Z0-9\.\-_]/", $_POST['groupname'])) {
183
			$input_errors[] = sprintf(gettext("The (%s) group name contains invalid characters."), $_POST['gtype']);
184
		}
185
	} else {
186
		if (preg_match("/[^a-zA-Z0-9\.\- _]/", $_POST['groupname'])) {
187
			$input_errors[] = sprintf(gettext("The (%s) group name contains invalid characters."), $_POST['gtype']);
188
		}
189
	}
190

    
191
	if (strlen($_POST['groupname']) > 16) {
192
		$input_errors[] = gettext("The group name is longer than 16 characters.");
193
	}
194

    
195
	/* Check the POSTed members to ensure they are valid and exist */
196
	foreach ($_POST['members'] as $newmember) {
197
		if (!is_numeric($newmember) || empty(getUserEntryByUID($newmember))) {
198
			$input_errors[] = gettext("One or more invalid group members was submitted.");
199
		}
200
	}
201

    
202
	if (!$input_errors && !(isset($id) && $a_group[$id])) {
203
		/* make sure there are no dupes */
204
		foreach ($a_group as $group) {
205
			if ($group['name'] == $_POST['groupname']) {
206
				$input_errors[] = gettext("Another entry with the same group name already exists.");
207
				break;
208
			}
209
		}
210
	}
211

    
212
	if (!$input_errors) {
213
		$group = array();
214
		if (isset($id) && $a_group[$id]) {
215
			$group = $a_group[$id];
216
		}
217

    
218
		$group['name'] = $_POST['groupname'];
219
		$group['description'] = $_POST['description'];
220
		$group['scope'] = $_POST['gtype'];
221

    
222
		if (empty($_POST['members'])) {
223
			unset($group['member']);
224
		} else if ($group['gid'] != 1998) { // all group
225
			$group['member'] = $_POST['members'];
226
		}
227

    
228
		if (isset($id) && $a_group[$id]) {
229
			$a_group[$id] = $group;
230
		} else {
231
			$group['gid'] = $config['system']['nextgid']++;
232
			$a_group[] = $group;
233
		}
234

    
235
		admin_groups_sort();
236

    
237
		conf_mount_rw();
238
		local_group_set($group);
239
		conf_mount_ro();
240

    
241
		/* Refresh users in this group since their privileges may have changed. */
242
		if (is_array($group['member'])) {
243
			$a_user = &$config['system']['user'];
244
			foreach ($a_user as & $user) {
245
				if (in_array($user['uid'], $group['member'])) {
246
					local_user_set($user);
247
				}
248
			}
249
		}
250

    
251
		write_config();
252

    
253
		header("Location: system_groupmanager.php");
254
		exit;
255
	}
256

    
257
	$pconfig['name'] = $_POST['groupname'];
258
}
259

    
260
function build_priv_table() {
261
	global $a_group, $id;
262

    
263
	$privhtml = '<div class="table-responsive">';
264
	$privhtml .=	'<table class="table table-striped table-hover table-condensed">';
265
	$privhtml .=		'<thead>';
266
	$privhtml .=			'<tr>';
267
	$privhtml .=				'<th>' . gettext('Name') . '</th>';
268
	$privhtml .=				'<th>' . gettext('Description') . '</th>';
269
	$privhtml .=				'<th>' . gettext('Action') . '</th>';
270
	$privhtml .=			'</tr>';
271
	$privhtml .=		'</thead>';
272
	$privhtml .=		'<tbody>';
273

    
274
	foreach (get_user_privdesc($a_group[$id]) as $i => $priv) {
275
		$privhtml .=		'<tr>';
276
		$privhtml .=			'<td>' . htmlspecialchars($priv['name']) . '</td>';
277
		$privhtml .=			'<td>' . htmlspecialchars($priv['descr']) . '</td>';
278
		$privhtml .=			'<td><a class="fa fa-trash" title="' . gettext('Delete Privilege') . '"	href="system_groupmanager.php?act=delpriv&amp;groupid=' . $id . '&amp;privid=' . $i . '"></a></td>';
279
		$privhtml .=		'</tr>';
280

    
281
	}
282

    
283
	$privhtml .=		'</tbody>';
284
	$privhtml .=	'</table>';
285
	$privhtml .= '</div>';
286

    
287
	$privhtml .= '<nav class="action-buttons">';
288
	$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>';
289
	$privhtml .= '</nav>';
290

    
291
	return($privhtml);
292
}
293

    
294
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Groups"));
295

    
296
if ($act == "new" || $act == "edit") {
297
	$pgtitle[] = gettext('Edit');
298
}
299

    
300
include("head.inc");
301

    
302
if ($input_errors) {
303
	print_input_errors($input_errors);
304
}
305

    
306
if ($savemsg) {
307
	print_info_box($savemsg, 'success');
308
}
309

    
310
$tab_array = array();
311
$tab_array[] = array(gettext("Users"), false, "system_usermanager.php");
312
$tab_array[] = array(gettext("Groups"), true, "system_groupmanager.php");
313
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
314
$tab_array[] = array(gettext("Authentication Servers"), false, "system_authservers.php");
315
display_top_tabs($tab_array);
316

    
317
if (!($_GET['act'] == "new" || $_GET['act'] == "edit")) {
318
?>
319
<div class="panel panel-default">
320
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Groups')?></h2></div>
321
	<div class="panel-body">
322
		<div class="table-responsive">
323
			<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
324
				<thead>
325
					<tr>
326
						<th><?=gettext("Group name")?></th>
327
						<th><?=gettext("Description")?></th>
328
						<th><?=gettext("Member Count")?></th>
329
						<th><?=gettext("Actions")?></th>
330
					</tr>
331
				</thead>
332
				<tbody>
333
<?php
334
	foreach ($a_group as $i => $group):
335
		if ($group["name"] == "all") {
336
			$groupcount = count($config['system']['user']);
337
		} else {
338
			$groupcount = count($group['member']);
339
		}
340
?>
341
					<tr>
342
						<td>
343
							<?=htmlspecialchars($group['name'])?>
344
						</td>
345
						<td>
346
							<?=htmlspecialchars($group['description'])?>
347
						</td>
348
						<td>
349
							<?=$groupcount?>
350
						</td>
351
						<td>
352
							<a class="fa fa-pencil" title="<?=gettext("Edit group"); ?>" href="?act=edit&amp;groupid=<?=$i?>"></a>
353
							<?php if ($group['scope'] != "system"): ?>
354
								<a class="fa fa-trash"	title="<?=gettext("Delete group")?>" href="?act=delgroup&amp;groupid=<?=$i?>&amp;groupname=<?=$group['name']?>"></a>
355
							<?php endif;?>
356
						</td>
357
					</tr>
358
<?php
359
	endforeach;
360
?>
361
				</tbody>
362
			</table>
363
		</div>
364
	</div>
365
</div>
366

    
367
<nav class="action-buttons">
368
	<a href="?act=new" class="btn btn-success btn-sm">
369
		<i class="fa fa-plus icon-embed-btn"></i>
370
		<?=gettext("Add")?>
371
	</a>
372
</nav>
373
<?php
374
	include('foot.inc');
375
	exit;
376
}
377

    
378
$form = new Form;
379
$form->setAction('system_groupmanager.php?act=edit');
380
$form->addGlobal(new Form_Input(
381
	'groupid',
382
	null,
383
	'hidden',
384
	$id
385
));
386

    
387
if (isset($id) && $a_group[$id]){
388
	$form->addGlobal(new Form_Input(
389
		'id',
390
		null,
391
		'hidden',
392
		$id
393
	));
394

    
395
	$form->addGlobal(new Form_Input(
396
		'gid',
397
		null,
398
		'hidden',
399
		$pconfig['gid']
400
	));
401
}
402

    
403
$section = new Form_Section('Group Properties');
404

    
405
$section->addInput($input = new Form_Input(
406
	'groupname',
407
	'Group name',
408
	'text',
409
	$pconfig['name']
410
));
411

    
412
if ($pconfig['gtype'] == "system") {
413
	$input->setReadonly();
414

    
415
	$section->addInput(new Form_Input(
416
		'gtype',
417
		'Scope',
418
		'text',
419
		$pconfig['gtype']
420
	))->setReadonly();
421
} else {
422
	$section->addInput(new Form_Select(
423
		'gtype',
424
		'Scope',
425
		$pconfig['gtype'],
426
		["local" => gettext("Local"), "remote" => gettext("Remote")]
427
	));
428
}
429

    
430
$section->addInput(new Form_Input(
431
	'description',
432
	'Description',
433
	'text',
434
	$pconfig['description']
435
))->setHelp('Group description, for administrative information only');
436

    
437

    
438
$form->add($section);
439
if ($pconfig['gid'] != 1998) { // all users group
440

    
441
	// ==== Group membership ==================================================
442
	$group = new Form_Group('Group membership');
443

    
444
	// Make a list of all the groups configured on the system, and a list of
445
	// those which this user is a member of
446
	$systemGroups = array();
447
	$usersGroups = array();
448

    
449
	foreach ($config['system']['user'] as $user) {
450
		if (is_array($pconfig['members']) && in_array($user['uid'], $pconfig['members'])) {
451
			$usersGroups[ $user['uid'] ] = $user['name'];	// Add it to the user's list
452
		} else {
453
			$systemGroups[ $user['uid'] ] = $user['name']; // Add it to the 'not a member of' list
454
		}
455
	}
456

    
457
	$group->add(new Form_Select(
458
		'notmembers',
459
		null,
460
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
461
		$systemGroups,
462
		true
463
	))->setHelp('Not members');
464

    
465
	$group->add(new Form_Select(
466
		'members',
467
		null,
468
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
469
		$usersGroups,
470
		true
471
	))->setHelp('Members');
472

    
473
	$section->add($group);
474

    
475
	$group = new Form_Group('');
476

    
477
	$group->add(new Form_Button(
478
		'movetoenabled',
479
		'Move to "Members"',
480
		null,
481
		'fa-angle-double-right'
482
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass('btn-info btn-sm');
483

    
484
	$group->add(new Form_Button(
485
		'movetodisabled',
486
		'Move to "Not members',
487
		null,
488
		'fa-angle-double-left'
489
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass('btn-info btn-sm');
490

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

    
494
}
495

    
496
if ($_GET['act'] != "new") {
497
	$section = new Form_Section('Assigned Privileges');
498

    
499
	$section->addInput(new Form_StaticText(
500
		null,
501
		build_priv_table()
502
	));
503

    
504

    
505
	$form->add($section);
506
}
507

    
508
print $form;
509
?>
510
<script type="text/javascript">
511
//<![CDATA[
512
events.push(function() {
513

    
514
	// On click . .
515
	$("#movetodisabled").click(function() {
516
		moveOptions($('[name="members[]"] option'), $('[name="notmembers[]"]'));
517
	});
518

    
519
	$("#movetoenabled").click(function() {
520
		moveOptions($('[name="notmembers[]"] option'), $('[name="members[]"]'));
521
	});
522

    
523
	// On submit mark all the user's groups as "selected"
524
	$('form').submit(function() {
525
		AllServers($('[name="members[]"] option'), true);
526
	});
527
});
528
//]]>
529
</script>
530
<?php
531
include('foot.inc');
(200-200/225)