Project

General

Profile

Download (14 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

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

    
196
	if (!$input_errors && !(isset($id) && $a_group[$id])) {
197
		/* make sure there are no dupes */
198
		foreach ($a_group as $group) {
199
			if ($group['name'] == $_POST['groupname']) {
200
				$input_errors[] = gettext("Another entry with the same group name already exists.");
201
				break;
202
			}
203
		}
204
	}
205

    
206
	if (!$input_errors) {
207
		$group = array();
208
		if (isset($id) && $a_group[$id]) {
209
			$group = $a_group[$id];
210
		}
211

    
212
		$group['name'] = $_POST['groupname'];
213
		$group['description'] = $_POST['description'];
214
		$group['scope'] = $_POST['gtype'];
215

    
216
		if (empty($_POST['members'])) {
217
			unset($group['member']);
218
		} else if ($group['gid'] != 1998) { // all group
219
			$group['member'] = $_POST['members'];
220
		}
221

    
222
		if (isset($id) && $a_group[$id]) {
223
			$a_group[$id] = $group;
224
		} else {
225
			$group['gid'] = $config['system']['nextgid']++;
226
			$a_group[] = $group;
227
		}
228

    
229
		admin_groups_sort();
230

    
231
		conf_mount_rw();
232
		local_group_set($group);
233
		conf_mount_ro();
234

    
235
		/* Refresh users in this group since their privileges may have changed. */
236
		if (is_array($group['member'])) {
237
			$a_user = &$config['system']['user'];
238
			foreach ($a_user as & $user) {
239
				if (in_array($user['uid'], $group['member'])) {
240
					local_user_set($user);
241
				}
242
			}
243
		}
244

    
245
		write_config();
246

    
247
		header("Location: system_groupmanager.php");
248
		exit;
249
	}
250

    
251
	$pconfig['name'] = $_POST['groupname'];
252
}
253

    
254
function build_priv_table() {
255
	global $a_group, $id;
256

    
257
	$privhtml = '<div class="table-responsive">';
258
	$privhtml .=	'<table class="table table-striped table-hover table-condensed">';
259
	$privhtml .=		'<thead>';
260
	$privhtml .=			'<tr>';
261
	$privhtml .=				'<th>' . gettext('Name') . '</th>';
262
	$privhtml .=				'<th>' . gettext('Description') . '</th>';
263
	$privhtml .=				'<th>' . gettext('Action') . '</th>';
264
	$privhtml .=			'</tr>';
265
	$privhtml .=		'</thead>';
266
	$privhtml .=		'<tbody>';
267

    
268
	foreach (get_user_privdesc($a_group[$id]) as $i => $priv) {
269
		$privhtml .=		'<tr>';
270
		$privhtml .=			'<td>' . htmlspecialchars($priv['name']) . '</td>';
271
		$privhtml .=			'<td>' . htmlspecialchars($priv['descr']) . '</td>';
272
		$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>';
273
		$privhtml .=		'</tr>';
274

    
275
	}
276

    
277
	$privhtml .=		'</tbody>';
278
	$privhtml .=	'</table>';
279
	$privhtml .= '</div>';
280

    
281
	$privhtml .= '<nav class="action-buttons">';
282
	$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>';
283
	$privhtml .= '</nav>';
284

    
285
	return($privhtml);
286
}
287

    
288
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Groups"));
289

    
290
if ($act == "new" || $act == "edit") {
291
	$pgtitle[] = gettext('Edit');
292
}
293

    
294
include("head.inc");
295

    
296
if ($input_errors) {
297
	print_input_errors($input_errors);
298
}
299

    
300
if ($savemsg) {
301
	print_info_box($savemsg, 'success');
302
}
303

    
304
$tab_array = array();
305
$tab_array[] = array(gettext("Users"), false, "system_usermanager.php");
306
$tab_array[] = array(gettext("Groups"), true, "system_groupmanager.php");
307
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
308
$tab_array[] = array(gettext("Authentication Servers"), false, "system_authservers.php");
309
display_top_tabs($tab_array);
310

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

    
361
<nav class="action-buttons">
362
	<a href="?act=new" class="btn btn-success btn-sm">
363
		<i class="fa fa-plus icon-embed-btn"></i>
364
		<?=gettext("Add")?>
365
	</a>
366
</nav>
367
<?php
368
	include('foot.inc');
369
	exit;
370
}
371

    
372
$form = new Form;
373
$form->setAction('system_groupmanager.php?act=edit');
374
$form->addGlobal(new Form_Input(
375
	'groupid',
376
	null,
377
	'hidden',
378
	$id
379
));
380

    
381
if (isset($id) && $a_group[$id]){
382
	$form->addGlobal(new Form_Input(
383
		'id',
384
		null,
385
		'hidden',
386
		$id
387
	));
388

    
389
	$form->addGlobal(new Form_Input(
390
		'gid',
391
		null,
392
		'hidden',
393
		$pconfig['gid']
394
	));
395
}
396

    
397
$section = new Form_Section('Group Properties');
398

    
399
$section->addInput($input = new Form_Input(
400
	'groupname',
401
	'Group name',
402
	'text',
403
	$pconfig['name']
404
));
405

    
406
if ($pconfig['gtype'] == "system") {
407
	$input->setReadonly();
408

    
409
	$section->addInput(new Form_Input(
410
		'gtype',
411
		'Scope',
412
		'text',
413
		$pconfig['gtype']
414
	))->setReadonly();
415
} else {
416
	$section->addInput(new Form_Select(
417
		'gtype',
418
		'Scope',
419
		$pconfig['gtype'],
420
		["local" => gettext("Local"), "remote" => gettext("Remote")]
421
	));
422
}
423

    
424
$section->addInput(new Form_Input(
425
	'description',
426
	'Description',
427
	'text',
428
	$pconfig['description']
429
))->setHelp('Group description, for administrative information only');
430

    
431

    
432
$form->add($section);
433
if ($pconfig['gid'] != 1998) { // all users group
434

    
435
	// ==== Group membership ==================================================
436
	$group = new Form_Group('Group membership');
437

    
438
	// Make a list of all the groups configured on the system, and a list of
439
	// those which this user is a member of
440
	$systemGroups = array();
441
	$usersGroups = array();
442

    
443
	foreach ($config['system']['user'] as $user) {
444
		if (is_array($pconfig['members']) && in_array($user['uid'], $pconfig['members'])) {
445
			$usersGroups[ $user['uid'] ] = $user['name'];	// Add it to the user's list
446
		} else {
447
			$systemGroups[ $user['uid'] ] = $user['name']; // Add it to the 'not a member of' list
448
		}
449
	}
450

    
451
	$group->add(new Form_Select(
452
		'notmembers',
453
		null,
454
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
455
		$systemGroups,
456
		true
457
	))->setHelp('Not members');
458

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

    
467
	$section->add($group);
468

    
469
	$group = new Form_Group('');
470

    
471
	$group->add(new Form_Button(
472
		'movetoenabled',
473
		'Move to "Members"',
474
		null,
475
		'fa-angle-double-right'
476
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass('btn-info btn-sm');
477

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

    
485
	$group->setHelp('Hold down CTRL (pc)/COMMAND (mac) key to select multiple items');
486
	$section->add($group);
487

    
488
}
489

    
490
if ($_GET['act'] != "new") {
491
	$section = new Form_Section('Assigned Privileges');
492

    
493
	$section->addInput(new Form_StaticText(
494
		null,
495
		build_priv_table()
496
	));
497

    
498

    
499
	$form->add($section);
500
}
501

    
502
print $form;
503
?>
504
<script type="text/javascript">
505
//<![CDATA[
506
events.push(function() {
507

    
508
	// On click . .
509
	$("#movetodisabled").click(function() {
510
		moveOptions($('[name="members[]"] option'), $('[name="notmembers[]"]'));
511
	});
512

    
513
	$("#movetoenabled").click(function() {
514
		moveOptions($('[name="notmembers[]"] option'), $('[name="members[]"]'));
515
	});
516

    
517
	// On submit mark all the user's groups as "selected"
518
	$('form').submit(function() {
519
		AllServers($('[name="members[]"] option'), true);
520
	});
521
});
522
//]]>
523
</script>
524
<?php
525
include('foot.inc');
(200-200/225)