Project

General

Profile

Download (13.4 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
	pfSense_MODULE: auth
62
*/
63

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

    
71
require("guiconfig.inc");
72

    
73
$pgtitle = array(gettext("System"), gettext("Group Manager"));
74

    
75
if (!is_array($config['system']['group'])) {
76
	$config['system']['group'] = array();
77
}
78

    
79
$a_group = &$config['system']['group'];
80

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

    
86
if (isset($_GET['groupid']) && is_numericint($_GET['groupid'])) {
87
	$id = $_GET['groupid'];
88
}
89

    
90
$act = (isset($_GET['act']) ? $_GET['act'] : '');
91

    
92
if ($act == "delgroup") {
93

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

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

    
109
if ($act == "delpriv") {
110

    
111
	if (!isset($id) || !isset($a_group[$id])) {
112
		pfSenseHeader("system_groupmanager.php");
113
		exit;
114
	}
115

    
116
	$privdeleted = $priv_list[$a_group[$id]['priv'][$_POST['privid']]]['name'];
117
	unset($a_group[$id]['priv'][$_GET['privid']]);
118

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

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

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

    
145
if (isset($_GET['dellall_x'])) {
146

    
147
	$del_groups = $_GET['delete_check'];
148

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

    
163
if (isset($_POST['save'])) {
164
	unset($input_errors);
165
	$pconfig = $_POST;
166

    
167
	/* input validation */
168
	$reqdfields = explode(" ", "groupname");
169
	$reqdfieldsn = array(gettext("Group Name"));
170

    
171
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
172

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

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

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

    
191
	if (!$input_errors) {
192
		$group = array();
193
		if (isset($id) && $a_group[$id]) {
194
			$group = $a_group[$id];
195
		}
196

    
197
		$group['name'] = $_POST['groupname'];
198
		$group['description'] = $_POST['description'];
199

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

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

    
213
		conf_mount_rw();
214
		local_group_set($group);
215
		conf_mount_ro();
216

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

    
227
		write_config();
228

    
229
		header("Location: system_groupmanager.php");
230
		exit;
231
	}
232
}
233

    
234
function build_priv_table() {
235
	global $a_group, $id;
236

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

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

    
254
	$privhtml .=		'</tbody>';
255
	$privhtml .=	'</table>';
256
	$privhtml .= '</div>';
257

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

    
262
	return($privhtml);
263
}
264

    
265
include("head.inc");
266

    
267
if ($input_errors)
268
	print_input_errors($input_errors);
269
if ($savemsg)
270
	print_info_box($savemsg);
271

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

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

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

    
335
$form = new Form;
336
$form->setAction('system_groupmanager.php?act=edit');
337
$form->addGlobal(new Form_Input(
338
	'groupid',
339
	null,
340
	'hidden',
341
	$id
342
));
343

    
344
if (isset($id) && $a_group[$id]){
345
	$form->addGlobal(new Form_Input(
346
		'id',
347
		null,
348
		'hidden',
349
		$id
350
	));
351

    
352
	$form->addGlobal(new Form_Input(
353
		'gid',
354
		null,
355
		'hidden',
356
		$pconfig['gid']
357
	));
358
}
359

    
360
$section = new Form_Section('Group properties');
361

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

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

    
377
if ($pconfig['gtype'] == "system")
378
	$input->setReadonly();
379

    
380
$section->addInput(new Form_Input(
381
	'description',
382
	'Description',
383
	'text',
384
	$pconfig['description']
385
))->setHelp('Group description, for your own information only');
386

    
387
$form->add($section);
388
if ($pconfig['gid'] != 1998) // all users group
389
{
390
	// ==== Group membership ==================================================
391
	$group = new Form_Group('Group membership');
392

    
393
	// Make a list of all the groups configured on the system, and a list of
394
	// those which this user is a member of
395
	$systemGroups = array();
396
	$usersGroups = array();
397

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

    
405
	$group->add(new Form_Select(
406
		'notmembers',
407
		null,
408
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
409
		$systemGroups,
410
		true
411
	))->setHelp('Not members');
412

    
413
	$group->add(new Form_Select(
414
		'members',
415
		null,
416
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
417
		$usersGroups,
418
		true
419
	))->setHelp('Members');
420

    
421
	$section->add($group);
422

    
423
	$group = new Form_Group('');
424

    
425
	$group->add(new Form_Button(
426
		'movetoenabled',
427
		'Move to "Members" >'
428
	))->removeClass('btn-primary')->addClass('btn-default btn-sm');
429

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

    
435
	$group->setHelp('Hold down CTRL (pc)/COMMAND (mac) key to select multiple items');
436
	$section->add($group);
437

    
438
}
439

    
440
if ($_GET['act'] != "new")
441
{
442
	$section = new Form_Section('Assigned Privileges');
443

    
444
	$section->addInput(new Form_StaticText(
445
		null,
446
		build_priv_table()
447
	));
448

    
449

    
450
	$form->add($section);
451
}
452

    
453
print $form;
454
?>
455
<script>
456
//<![CDATA[
457
events.push(function(){
458

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

    
466
	// Move all selected options from one multiselect to another
467
	function moveOptions(From, To)	{
468
		var len = From.length;
469
		var option, value;
470

    
471
		if(len > 1) {
472
			for(i=0; i<len; i++) {
473
				if(From.eq(i).is(':selected')) {
474
					option = From.eq(i).val();
475
					value = From.eq(i).text();
476
					To.append(new Option(value, option));
477
					From.eq(i).remove();
478
				}
479
			}
480
		}
481
	}
482

    
483
	// Make buttons plain buttons, not submit
484
	$("#movetodisabled").prop('type','button');
485
	$("#movetoenabled").prop('type','button');
486

    
487

    
488
	// On click . .
489
	$("#movetodisabled").click(function() {
490
		moveOptions($('[name="members[]"] option'), $('[name="notmembers[]"]'));
491
	});
492

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

    
497
	// On submit mark all the user's groups as "selected"
498
	$('form').submit(function(){
499
		AllServers($('[name="members[]"] option'), true);
500
	});
501
});
502
//]]>
503
</script>
504
<?php
505
include('foot.inc');
(199-199/228)