Project

General

Profile

Download (13.6 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
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Groups"));
71

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

    
76
$a_group = &$config['system']['group'];
77

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

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

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

    
89
if ($act == "delgroup") {
90

    
91
	if (!isset($id) || !isset($_GET['groupname']) || !isset($a_group[$id]) || ($_GET['groupname'] != $a_group[$id]['name'])) {
92
		pfSenseHeader("system_groupmanager.php");
93
		exit;
94
	}
95

    
96
	conf_mount_rw();
97
	local_group_del($a_group[$id]);
98
	conf_mount_ro();
99
	$groupdeleted = $a_group[$id]['name'];
100
	unset($a_group[$id]);
101
	write_config();
102
	$savemsg = gettext("Group") . " {$groupdeleted} " .
103
		gettext("successfully deleted") . "<br />";
104
}
105

    
106
if ($act == "delpriv") {
107

    
108
	if (!isset($id) || !isset($a_group[$id])) {
109
		pfSenseHeader("system_groupmanager.php");
110
		exit;
111
	}
112

    
113
	$privdeleted = $priv_list[$a_group[$id]['priv'][$_POST['privid']]]['name'];
114
	unset($a_group[$id]['priv'][$_GET['privid']]);
115

    
116
	if (is_array($a_group[$id]['member'])) {
117
		foreach ($a_group[$id]['member'] as $uid) {
118
			$user = getUserEntryByUID($uid);
119
			if ($user) {
120
				local_user_set($user);
121
			}
122
		}
123
	}
124

    
125
	write_config();
126
	$act = "edit";
127
	$savemsg = gettext("Privilege") . " {$privdeleted} " .
128
		gettext("successfully deleted") . "<br />";
129
}
130

    
131
if ($act == "edit") {
132
	if (isset($id) && isset($a_group[$id])) {
133
		$pconfig['name'] = $a_group[$id]['name'];
134
		$pconfig['gid'] = $a_group[$id]['gid'];
135
		$pconfig['gtype'] = $a_group[$id]['scope'];
136
		$pconfig['description'] = $a_group[$id]['description'];
137
		$pconfig['members'] = $a_group[$id]['member'];
138
		$pconfig['priv'] = $a_group[$id]['priv'];
139
	}
140
}
141

    
142
if (isset($_GET['dellall_x'])) {
143

    
144
	$del_groups = $_GET['delete_check'];
145

    
146
	if (!empty($del_groups)) {
147
		foreach ($del_groups as $groupid) {
148
			if (isset($a_group[$groupid]) && $a_group[$groupid]['scope'] != "system") {
149
				conf_mount_rw();
150
				local_group_del($a_group[$groupid]);
151
				conf_mount_ro();
152
				unset($a_group[$groupid]);
153
			}
154
		}
155
		$savemsg = gettext("Selected groups removed successfully!");
156
		write_config($savemsg);
157
	}
158
}
159

    
160
if (isset($_POST['save'])) {
161
	unset($input_errors);
162
	$pconfig = $_POST;
163

    
164
	/* input validation */
165
	$reqdfields = explode(" ", "groupname");
166
	$reqdfieldsn = array(gettext("Group Name"));
167

    
168
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
169

    
170
	if (preg_match("/[^a-zA-Z0-9\.\-_ ]/", $_POST['groupname'])) {
171
		$input_errors[] = gettext("The group name contains invalid characters.");
172
	}
173

    
174
	if (strlen($_POST['groupname']) > 16) {
175
		$input_errors[] = gettext("The group name is longer than 16 characters.");
176
	}
177

    
178
	if (!$input_errors && !(isset($id) && $a_group[$id])) {
179
		/* make sure there are no dupes */
180
		foreach ($a_group as $group) {
181
			if ($group['name'] == $_POST['groupname']) {
182
				$input_errors[] = gettext("Another entry with the same group name already exists.");
183
				break;
184
			}
185
		}
186
	}
187

    
188
	if (!$input_errors) {
189
		$group = array();
190
		if (isset($id) && $a_group[$id]) {
191
			$group = $a_group[$id];
192
		}
193

    
194
		$group['name'] = $_POST['groupname'];
195
		$group['description'] = $_POST['description'];
196

    
197
		if (empty($_POST['members'])) {
198
			unset($group['member']);
199
		} else if ($group['gid'] != 1998) { // all group
200
			$group['member'] = $_POST['members'];
201
		}
202

    
203
		if (isset($id) && $a_group[$id]) {
204
			$a_group[$id] = $group;
205
		} else {
206
			$group['gid'] = $config['system']['nextgid']++;
207
			$a_group[] = $group;
208
		}
209

    
210
		conf_mount_rw();
211
		local_group_set($group);
212
		conf_mount_ro();
213

    
214
		/* Refresh users in this group since their privileges may have changed. */
215
		if (is_array($group['member'])) {
216
			$a_user = &$config['system']['user'];
217
			foreach ($a_user as & $user) {
218
				if (in_array($user['uid'], $group['member'])) {
219
					local_user_set($user);
220
				}
221
			}
222
		}
223

    
224
		write_config();
225

    
226
		header("Location: system_groupmanager.php");
227
		exit;
228
	}
229
}
230

    
231
function build_priv_table() {
232
	global $a_group, $id;
233

    
234
	$privhtml = '<div class="table-responsive">';
235
	$privhtml .=	'<table class="table table-striped table-hover table-condensed">';
236
	$privhtml .=		'<thead>';
237
	$privhtml .=			'<th>' . gettext('Name') . '</th>';
238
	$privhtml .=			'<th>' . gettext('Description') . '</th>';
239
	$privhtml .=		'</thead>';
240
	$privhtml .=		'<tbody>';
241

    
242
	foreach (get_user_privdesc($a_group[$id]) as $i => $priv) {
243
		$privhtml .=		'<tr>';
244
		$privhtml .=			'<td>' . htmlspecialchars($priv['name']) . '</td>';
245
		$privhtml .=			'<td>' . htmlspecialchars($priv['descr']) . '</td>';
246
		$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>';
247
		$privhtml .=		'</tr>';
248

    
249
	}
250

    
251
	$privhtml .=		'</tbody>';
252
	$privhtml .=	'</table>';
253
	$privhtml .= '</div>';
254

    
255
	$privhtml .= '<nav class="action-buttons">';
256
	$privhtml .=	'<a href="system_groupmanager_addprivs.php?groupid=' . $id . '" class="btn btn-success">' . gettext("Add") . '</a>';
257
	$privhtml .= '</nav>';
258

    
259
	return($privhtml);
260
}
261

    
262
include("head.inc");
263

    
264
if ($input_errors) {
265
	print_input_errors($input_errors);
266
}
267
if ($savemsg) {
268
	print_info_box($savemsg, 'success');
269
}
270

    
271
$tab_array = array();
272
$tab_array[] = array(gettext("Users"), false, "system_usermanager.php");
273
$tab_array[] = array(gettext("Groups"), true, "system_groupmanager.php");
274
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
275
$tab_array[] = array(gettext("Servers"), false, "system_authservers.php");
276
display_top_tabs($tab_array);
277

    
278
if (!($_GET['act'] == "new" || $_GET['act'] == "edit")) {
279
?>
280
<div class="panel panel-default">
281
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Groups')?></h2></div>
282
	<div class="panel-body">
283
		<div class="table-responsive">
284
			<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
285
				<thead>
286
					<tr>
287
						<th><?=gettext("Group name")?></th>
288
						<th><?=gettext("Description")?></th>
289
						<th><?=gettext("Member Count")?></th>
290
						<th><?=gettext("Actions")?></th>
291
					</tr>
292
				</thead>
293
				<tbody>
294
<?php
295
	foreach ($a_group as $i => $group):
296
		if ($group["name"] == "all") {
297
			$groupcount = count($config['system']['user']);
298
		} else {
299
			$groupcount = count($group['member']);
300
		}
301
?>
302
					<tr>
303
						<td>
304
							<?=htmlspecialchars($group['name'])?>
305
						</td>
306
						<td>
307
							<?=htmlspecialchars($group['description'])?>
308
						</td>
309
						<td>
310
							<?=$groupcount?>
311
						</td>
312
						<td>
313
							<a class="fa fa-pencil" title="<?=gettext("Edit group"); ?>" href="?act=edit&amp;groupid=<?=$i?>"></a>
314
							<?php if ($group['scope'] != "system"): ?>
315
								<a class="fa fa-trash"	title="<?=gettext("Delete group")?>" href="?act=delgroup&amp;groupid=<?=$i?>&amp;groupname=<?=$group['name']?>"></a>
316
							<?php endif;?>
317
						</td>
318
					</tr>
319
<?php
320
	endforeach;
321
?>
322
				</tbody>
323
			</table>
324
		</div>
325
	</div>
326
</div>
327

    
328
<nav class="action-buttons">
329
	<a href="?act=new" class="btn btn-success btn-sm">
330
		<i class="fa fa-plus icon-embed-btn"></i>
331
		<?=gettext("Add")?>
332
	</a>
333
</nav>
334
<?php
335
	include('foot.inc');
336
	exit;
337
}
338

    
339
$form = new Form;
340
$form->setAction('system_groupmanager.php?act=edit');
341
$form->addGlobal(new Form_Input(
342
	'groupid',
343
	null,
344
	'hidden',
345
	$id
346
));
347

    
348
if (isset($id) && $a_group[$id]){
349
	$form->addGlobal(new Form_Input(
350
		'id',
351
		null,
352
		'hidden',
353
		$id
354
	));
355

    
356
	$form->addGlobal(new Form_Input(
357
		'gid',
358
		null,
359
		'hidden',
360
		$pconfig['gid']
361
	));
362
}
363

    
364
$section = new Form_Section('Group properties');
365

    
366
if ($_GET['act'] != "new") {
367
	$section->addInput(new Form_StaticText(
368
		'Defined by',
369
		strtoupper($pconfig['gtype'])
370
	));
371
}
372

    
373
$section->addInput($input = new Form_Input(
374
	'groupname',
375
	'Group name',
376
	'text',
377
	$pconfig['name']
378
));
379

    
380
if ($pconfig['gtype'] == "system") {
381
	$input->setReadonly();
382
}
383

    
384
$section->addInput(new Form_Input(
385
	'description',
386
	'Description',
387
	'text',
388
	$pconfig['description']
389
))->setHelp('Group description, for your own information only');
390

    
391
$form->add($section);
392
if ($pconfig['gid'] != 1998) { // all users group
393

    
394
	// ==== Group membership ==================================================
395
	$group = new Form_Group('Group membership');
396

    
397
	// Make a list of all the groups configured on the system, and a list of
398
	// those which this user is a member of
399
	$systemGroups = array();
400
	$usersGroups = array();
401

    
402
	foreach ($config['system']['user'] as $user) {
403
		if (is_array($pconfig['members']) && in_array($user['uid'], $pconfig['members'])) {
404
			$usersGroups[ $user['uid'] ] = $user['name'];	// Add it to the user's list
405
		} else {
406
			$systemGroups[ $user['uid'] ] = $user['name']; // Add it to the 'not a member of' list
407
		}
408
	}
409

    
410
	$group->add(new Form_Select(
411
		'notmembers',
412
		null,
413
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
414
		$systemGroups,
415
		true
416
	))->setHelp('Not members');
417

    
418
	$group->add(new Form_Select(
419
		'members',
420
		null,
421
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
422
		$usersGroups,
423
		true
424
	))->setHelp('Members');
425

    
426
	$section->add($group);
427

    
428
	$group = new Form_Group('');
429

    
430
	$group->add(new Form_Button(
431
		'movetoenabled',
432
		'Move to "Members" >'
433
	))->removeClass('btn-primary')->addClass('btn-default btn-sm');
434

    
435
	$group->add(new Form_Button(
436
		'movetodisabled',
437
		'< Move to "Not members'
438
	))->removeClass('btn-primary')->addClass('btn-default btn-sm');
439

    
440
	$group->setHelp('Hold down CTRL (pc)/COMMAND (mac) key to select multiple items');
441
	$section->add($group);
442

    
443
}
444

    
445
if ($_GET['act'] != "new") {
446
	$section = new Form_Section('Assigned Privileges');
447

    
448
	$section->addInput(new Form_StaticText(
449
		null,
450
		build_priv_table()
451
	));
452

    
453

    
454
	$form->add($section);
455
}
456

    
457
print $form;
458
?>
459
<script type="text/javascript">
460
//<![CDATA[
461
events.push(function() {
462

    
463
	// Select every option in the specified multiselect
464
	function AllServers(id, selectAll) {
465
	   for (i = 0; i < id.length; i++)	   {
466
		   id.eq(i).prop('selected', selectAll);
467
	   }
468
	}
469

    
470
	// Move all selected options from one multiselect to another
471
	function moveOptions(From, To)	{
472
		var len = From.length;
473
		var option, value;
474

    
475
		if (len > 1) {
476
			for (i=0; i<len; i++) {
477
				if (From.eq(i).is(':selected')) {
478
					option = From.eq(i).val();
479
					value = From.eq(i).text();
480
					To.append(new Option(value, option));
481
					From.eq(i).remove();
482
				}
483
			}
484
		}
485
	}
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');
(202-202/229)