Project

General

Profile

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

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

    
67
require_once("guiconfig.inc");
68

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

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

    
75
unset($id);
76

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
194
	/* Check the POSTed members to ensure they are valid and exist */
195
	if(is_array($_POST['members'])) {
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

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

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

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

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

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

    
236
		admin_groups_sort();
237

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

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

    
252
		write_config();
253

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

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

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

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

    
275
	foreach (get_user_privdesc($a_group[$id]) as $i => $priv) {
276
		$privhtml .=		'<tr>';
277
		$privhtml .=			'<td>' . htmlspecialchars($priv['name']) . '</td>';
278
		$privhtml .=			'<td>' . htmlspecialchars($priv['descr']) . '</td>';
279
		$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>';
280
		$privhtml .=		'</tr>';
281

    
282
	}
283

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

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

    
292
	return($privhtml);
293
}
294

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

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

    
301
include("head.inc");
302

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

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

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

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

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

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

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

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

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

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

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

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

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

    
438

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

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

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

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

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

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

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

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

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

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

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

    
495
}
496

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

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

    
505

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

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

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

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

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