Project

General

Profile

Download (13.5 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
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'] = $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 (preg_match("/[^a-zA-Z0-9\.\-_ ]/", $_POST['groupname'])) {
181
		$input_errors[] = gettext("The group name contains invalid characters.");
182
	}
183

    
184
	if (strlen($_POST['groupname']) > 16) {
185
		$input_errors[] = gettext("The group name is longer than 16 characters.");
186
	}
187

    
188
	if (!$input_errors && !(isset($id) && $a_group[$id])) {
189
		/* make sure there are no dupes */
190
		foreach ($a_group as $group) {
191
			if ($group['name'] == $_POST['groupname']) {
192
				$input_errors[] = gettext("Another entry with the same group name already exists.");
193
				break;
194
			}
195
		}
196
	}
197

    
198
	if (!$input_errors) {
199
		$group = array();
200
		if (isset($id) && $a_group[$id]) {
201
			$group = $a_group[$id];
202
		}
203

    
204
		$group['name'] = $_POST['groupname'];
205
		$group['description'] = $_POST['description'];
206

    
207
		if (empty($_POST['members'])) {
208
			unset($group['member']);
209
		} else if ($group['gid'] != 1998) { // all group
210
			$group['member'] = $_POST['members'];
211
		}
212

    
213
		if (isset($id) && $a_group[$id]) {
214
			$a_group[$id] = $group;
215
		} else {
216
			$group['gid'] = $config['system']['nextgid']++;
217
			$a_group[] = $group;
218
		}
219

    
220
		admin_groups_sort();
221

    
222
		conf_mount_rw();
223
		local_group_set($group);
224
		conf_mount_ro();
225

    
226
		/* Refresh users in this group since their privileges may have changed. */
227
		if (is_array($group['member'])) {
228
			$a_user = &$config['system']['user'];
229
			foreach ($a_user as & $user) {
230
				if (in_array($user['uid'], $group['member'])) {
231
					local_user_set($user);
232
				}
233
			}
234
		}
235

    
236
		write_config();
237

    
238
		header("Location: system_groupmanager.php");
239
		exit;
240
	}
241
}
242

    
243
function build_priv_table() {
244
	global $a_group, $id;
245

    
246
	$privhtml = '<div class="table-responsive">';
247
	$privhtml .=	'<table class="table table-striped table-hover table-condensed">';
248
	$privhtml .=		'<thead>';
249
	$privhtml .=			'<tr>';
250
	$privhtml .=				'<th>' . gettext('Name') . '</th>';
251
	$privhtml .=				'<th>' . gettext('Description') . '</th>';
252
	$privhtml .=				'<th>' . gettext('Action') . '</th>';
253
	$privhtml .=			'</tr>';
254
	$privhtml .=		'</thead>';
255
	$privhtml .=		'<tbody>';
256

    
257
	foreach (get_user_privdesc($a_group[$id]) as $i => $priv) {
258
		$privhtml .=		'<tr>';
259
		$privhtml .=			'<td>' . htmlspecialchars($priv['name']) . '</td>';
260
		$privhtml .=			'<td>' . htmlspecialchars($priv['descr']) . '</td>';
261
		$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>';
262
		$privhtml .=		'</tr>';
263

    
264
	}
265

    
266
	$privhtml .=		'</tbody>';
267
	$privhtml .=	'</table>';
268
	$privhtml .= '</div>';
269

    
270
	$privhtml .= '<nav class="action-buttons">';
271
	$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>';
272
	$privhtml .= '</nav>';
273

    
274
	return($privhtml);
275
}
276

    
277
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Groups"));
278

    
279
if ($act == "new" || $act == "edit") {
280
	$pgtitle[] = gettext('Edit');
281
}
282
include("head.inc");
283

    
284
if ($input_errors) {
285
	print_input_errors($input_errors);
286
}
287
if ($savemsg) {
288
	print_info_box($savemsg, 'success');
289
}
290

    
291
$tab_array = array();
292
$tab_array[] = array(gettext("Users"), false, "system_usermanager.php");
293
$tab_array[] = array(gettext("Groups"), true, "system_groupmanager.php");
294
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
295
$tab_array[] = array(gettext("Authentication Servers"), false, "system_authservers.php");
296
display_top_tabs($tab_array);
297

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

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

    
359
$form = new Form;
360
$form->setAction('system_groupmanager.php?act=edit');
361
$form->addGlobal(new Form_Input(
362
	'groupid',
363
	null,
364
	'hidden',
365
	$id
366
));
367

    
368
if (isset($id) && $a_group[$id]){
369
	$form->addGlobal(new Form_Input(
370
		'id',
371
		null,
372
		'hidden',
373
		$id
374
	));
375

    
376
	$form->addGlobal(new Form_Input(
377
		'gid',
378
		null,
379
		'hidden',
380
		$pconfig['gid']
381
	));
382
}
383

    
384
$section = new Form_Section('Group Properties');
385

    
386
if ($_GET['act'] != "new") {
387
	$section->addInput(new Form_StaticText(
388
		'Defined by',
389
		strtoupper($pconfig['gtype'])
390
	));
391
}
392

    
393
$section->addInput($input = new Form_Input(
394
	'groupname',
395
	'Group name',
396
	'text',
397
	$pconfig['name']
398
));
399

    
400
if ($pconfig['gtype'] == "system") {
401
	$input->setReadonly();
402
}
403

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

    
411
$form->add($section);
412
if ($pconfig['gid'] != 1998) { // all users group
413

    
414
	// ==== Group membership ==================================================
415
	$group = new Form_Group('Group membership');
416

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

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

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

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

    
446
	$section->add($group);
447

    
448
	$group = new Form_Group('');
449

    
450
	$group->add(new Form_Button(
451
		'movetoenabled',
452
		'Move to "Members"',
453
		null,
454
		'fa-angle-double-right'
455
	))->removeClass('btn-primary')->addClass('btn-info btn-sm');
456

    
457
	$group->add(new Form_Button(
458
		'movetodisabled',
459
		'Move to "Not members',
460
		null,
461
		'fa-angle-double-left'
462
	))->removeClass('btn-primary')->addClass('btn-info btn-sm');
463

    
464
	$group->setHelp('Hold down CTRL (pc)/COMMAND (mac) key to select multiple items');
465
	$section->add($group);
466

    
467
}
468

    
469
if ($_GET['act'] != "new") {
470
	$section = new Form_Section('Assigned Privileges');
471

    
472
	$section->addInput(new Form_StaticText(
473
		null,
474
		build_priv_table()
475
	));
476

    
477

    
478
	$form->add($section);
479
}
480

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

    
487
	// Make buttons plain buttons, not submit
488
	$("#movetodisabled").prop('type','button');
489
	$("#movetoenabled").prop('type','button');
490

    
491

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

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

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