Project

General

Profile

Download (13.1 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 Rubicon Communications, LLC (Netgate)
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
 * Licensed under the Apache License, Version 2.0 (the "License");
16
 * you may not use this file except in compliance with the License.
17
 * You may obtain a copy of the License at
18
 *
19
 * http://www.apache.org/licenses/LICENSE-2.0
20
 *
21
 * Unless required by applicable law or agreed to in writing, software
22
 * distributed under the License is distributed on an "AS IS" BASIS,
23
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
 * See the License for the specific language governing permissions and
25
 * limitations under the License.
26
 */
27

    
28
##|+PRIV
29
##|*IDENT=page-system-groupmanager
30
##|*NAME=System: Group Manager
31
##|*DESCR=Allow access to the 'System: Group Manager' page.
32
##|*WARN=standard-warning-root
33
##|*MATCH=system_groupmanager.php*
34
##|-PRIV
35

    
36
require_once("guiconfig.inc");
37
require_once("pfsense-utils.inc");
38

    
39
if (!is_array($config['system']['group'])) {
40
	$config['system']['group'] = array();
41
}
42

    
43
$a_group = &$config['system']['group'];
44

    
45
unset($id);
46
$id = $_REQUEST['groupid'];
47
$act = (isset($_REQUEST['act']) ? $_REQUEST['act'] : '');
48

    
49
function cpusercmp($a, $b) {
50
	return strcasecmp($a['name'], $b['name']);
51
}
52

    
53
function admin_groups_sort() {
54
	global $a_group;
55

    
56
	if (!is_array($a_group)) {
57
		return;
58
	}
59

    
60
	usort($a_group, "cpusercmp");
61
}
62

    
63
if ($_POST['act'] == "delgroup") {
64

    
65
	if (!isset($id) || !isset($_REQUEST['groupname']) || !isset($a_group[$id]) || ($_REQUEST['groupname'] != $a_group[$id]['name'])) {
66
		pfSenseHeader("system_groupmanager.php");
67
		exit;
68
	}
69

    
70
	local_group_del($a_group[$id]);
71
	$groupdeleted = $a_group[$id]['name'];
72
	unset($a_group[$id]);
73
	write_config();
74
	$savemsg = sprintf(gettext("Group %s successfully deleted."), $groupdeleted);
75
}
76

    
77
if ($_POST['act'] == "delpriv") {
78

    
79
	if (!isset($id) || !isset($a_group[$id])) {
80
		pfSenseHeader("system_groupmanager.php");
81
		exit;
82
	}
83

    
84
	$privdeleted = $priv_list[$a_group[$id]['priv'][$_REQUEST['privid']]]['name'];
85
	unset($a_group[$id]['priv'][$_REQUEST['privid']]);
86

    
87
	if (is_array($a_group[$id]['member'])) {
88
		foreach ($a_group[$id]['member'] as $uid) {
89
			$user = getUserEntryByUID($uid);
90
			if ($user) {
91
				local_user_set($user);
92
			}
93
		}
94
	}
95

    
96
	write_config();
97
	$act = "edit";
98
	$savemsg = sprintf(gettext("Privilege %s successfully deleted."), $privdeleted);
99
}
100

    
101
if ($act == "edit") {
102
	if (isset($id) && isset($a_group[$id])) {
103
		$pconfig['name'] = $a_group[$id]['name'];
104
		$pconfig['gid'] = $a_group[$id]['gid'];
105
		$pconfig['gtype'] = empty($a_group[$id]['scope']) ? "local" : $a_group[$id]['scope'];
106
		$pconfig['description'] = $a_group[$id]['description'];
107
		$pconfig['members'] = $a_group[$id]['member'];
108
		$pconfig['priv'] = $a_group[$id]['priv'];
109
	}
110
}
111

    
112
if (isset($_POST['dellall_x'])) {
113

    
114
	$del_groups = $_POST['delete_check'];
115

    
116
	if (!empty($del_groups)) {
117
		foreach ($del_groups as $groupid) {
118
			if (isset($a_group[$groupid]) && $a_group[$groupid]['scope'] != "system") {
119
				local_group_del($a_group[$groupid]);
120
				unset($a_group[$groupid]);
121
			}
122
		}
123
		$savemsg = gettext("Selected groups removed successfully.");
124
		write_config($savemsg);
125
	}
126
}
127

    
128
if (isset($_POST['save'])) {
129
	unset($input_errors);
130
	$pconfig = $_POST;
131

    
132
	/* input validation */
133
	$reqdfields = explode(" ", "groupname");
134
	$reqdfieldsn = array(gettext("Group Name"));
135

    
136
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
137

    
138
	if ($_POST['gtype'] != "remote") {
139
		if (preg_match("/[^a-zA-Z0-9\.\-_]/", $_POST['groupname'])) {
140
			$input_errors[] = sprintf(gettext("The (%s) group name contains invalid characters."), $_POST['gtype']);
141
		}
142
	} else {
143
		if (preg_match("/[^a-zA-Z0-9\.\- _]/", $_POST['groupname'])) {
144
			$input_errors[] = sprintf(gettext("The (%s) group name contains invalid characters."), $_POST['gtype']);
145
		}
146
	}
147

    
148
	if (strlen($_POST['groupname']) > 16) {
149
		$input_errors[] = gettext("The group name is longer than 16 characters.");
150
	}
151

    
152
	/* Check the POSTed members to ensure they are valid and exist */
153
	if (is_array($_POST['members'])) {
154
		foreach ($_POST['members'] as $newmember) {
155
			if (!is_numeric($newmember) || empty(getUserEntryByUID($newmember))) {
156
				$input_errors[] = gettext("One or more invalid group members was submitted.");
157
			}
158
		}
159
	}
160

    
161
	if (!$input_errors && !(isset($id) && $a_group[$id])) {
162
		/* make sure there are no dupes */
163
		foreach ($a_group as $group) {
164
			if ($group['name'] == $_POST['groupname']) {
165
				$input_errors[] = gettext("Another entry with the same group name already exists.");
166
				break;
167
			}
168
		}
169
	}
170

    
171
	if (!$input_errors) {
172
		$group = array();
173
		if (isset($id) && $a_group[$id]) {
174
			$group = $a_group[$id];
175
		}
176

    
177
		$group['name'] = $_POST['groupname'];
178
		$group['description'] = $_POST['description'];
179
		$group['scope'] = $_POST['gtype'];
180

    
181
		if (empty($_POST['members'])) {
182
			unset($group['member']);
183
		} else if ($group['gid'] != 1998) { // all group
184
			$group['member'] = $_POST['members'];
185
		}
186

    
187
		if (isset($id) && $a_group[$id]) {
188
			$a_group[$id] = $group;
189
		} else {
190
			$group['gid'] = $config['system']['nextgid']++;
191
			$a_group[] = $group;
192
		}
193

    
194
		admin_groups_sort();
195

    
196
		local_group_set($group);
197

    
198
		/* Refresh users in this group since their privileges may have changed. */
199
		if (is_array($group['member'])) {
200
			$a_user = &$config['system']['user'];
201
			foreach ($a_user as & $user) {
202
				if (in_array($user['uid'], $group['member'])) {
203
					local_user_set($user);
204
				}
205
			}
206
		}
207

    
208
		write_config();
209

    
210
		header("Location: system_groupmanager.php");
211
		exit;
212
	}
213

    
214
	$pconfig['name'] = $_POST['groupname'];
215
}
216

    
217
function build_priv_table() {
218
	global $a_group, $id;
219

    
220
	$privhtml = '<div class="table-responsive">';
221
	$privhtml .=	'<table class="table table-striped table-hover table-condensed">';
222
	$privhtml .=		'<thead>';
223
	$privhtml .=			'<tr>';
224
	$privhtml .=				'<th>' . gettext('Name') . '</th>';
225
	$privhtml .=				'<th>' . gettext('Description') . '</th>';
226
	$privhtml .=				'<th>' . gettext('Action') . '</th>';
227
	$privhtml .=			'</tr>';
228
	$privhtml .=		'</thead>';
229
	$privhtml .=		'<tbody>';
230

    
231
	$user_has_root_priv = false;
232

    
233
	foreach (get_user_privdesc($a_group[$id]) as $i => $priv) {
234
		$privhtml .=		'<tr>';
235
		$privhtml .=			'<td>' . htmlspecialchars($priv['name']) . '</td>';
236
		$privhtml .=			'<td>' . htmlspecialchars($priv['descr']);
237
		if (isset($priv['warn']) && ($priv['warn'] == 'standard-warning-root')) {
238
			$privhtml .=			' ' . gettext('(admin privilege)');
239
			$user_has_root_priv = true;
240
		}
241
		$privhtml .=			'</td>';
242
		$privhtml .=			'<td><a class="fa fa-trash" title="' . gettext('Delete Privilege') . '"	href="system_groupmanager.php?act=delpriv&amp;groupid=' . $id . '&amp;privid=' . $i . '" usepost></a></td>';
243
		$privhtml .=		'</tr>';
244

    
245
	}
246

    
247
	if ($user_has_root_priv) {
248
		$privhtml .=		'<tr>';
249
		$privhtml .=			'<td colspan="2">';
250
		$privhtml .=				'<b>' . gettext('Security notice: Users in this group effectively have administrator-level access') . '</b>';
251
		$privhtml .=			'</td>';
252
		$privhtml .=			'<td>';
253
		$privhtml .=			'</td>';
254
		$privhtml .=		'</tr>';
255

    
256
	}
257

    
258
	$privhtml .=		'</tbody>';
259
	$privhtml .=	'</table>';
260
	$privhtml .= '</div>';
261

    
262
	$privhtml .= '<nav class="action-buttons">';
263
	$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>';
264
	$privhtml .= '</nav>';
265

    
266
	return($privhtml);
267
}
268

    
269
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Groups"));
270
$pglinks = array("", "system_usermanager.php", "system_groupmanager.php");
271

    
272
if ($act == "new" || $act == "edit") {
273
	$pgtitle[] = gettext('Edit');
274
	$pglinks[] = "@self";
275
}
276

    
277
include("head.inc");
278

    
279
if ($input_errors) {
280
	print_input_errors($input_errors);
281
}
282

    
283
if ($savemsg) {
284
	print_info_box($savemsg, 'success');
285
}
286

    
287
$tab_array = array();
288
$tab_array[] = array(gettext("Users"), false, "system_usermanager.php");
289
$tab_array[] = array(gettext("Groups"), true, "system_groupmanager.php");
290
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
291
$tab_array[] = array(gettext("Authentication Servers"), false, "system_authservers.php");
292
display_top_tabs($tab_array);
293

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

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

    
355
$form = new Form;
356
$form->setAction('system_groupmanager.php?act=edit');
357
$form->addGlobal(new Form_Input(
358
	'groupid',
359
	null,
360
	'hidden',
361
	$id
362
));
363

    
364
if (isset($id) && $a_group[$id]) {
365
	$form->addGlobal(new Form_Input(
366
		'id',
367
		null,
368
		'hidden',
369
		$id
370
	));
371

    
372
	$form->addGlobal(new Form_Input(
373
		'gid',
374
		null,
375
		'hidden',
376
		$pconfig['gid']
377
	));
378
}
379

    
380
$section = new Form_Section('Group Properties');
381

    
382
$section->addInput($input = new Form_Input(
383
	'groupname',
384
	'*Group name',
385
	'text',
386
	$pconfig['name']
387
));
388

    
389
if ($pconfig['gtype'] == "system") {
390
	$input->setReadonly();
391

    
392
	$section->addInput(new Form_Input(
393
		'gtype',
394
		'*Scope',
395
		'text',
396
		$pconfig['gtype']
397
	))->setReadonly();
398
} else {
399
	$section->addInput(new Form_Select(
400
		'gtype',
401
		'*Scope',
402
		$pconfig['gtype'],
403
		["local" => gettext("Local"), "remote" => gettext("Remote")]
404
	));
405
}
406

    
407
$section->addInput(new Form_Input(
408
	'description',
409
	'Description',
410
	'text',
411
	$pconfig['description']
412
))->setHelp('Group description, for administrative information only');
413

    
414

    
415
$form->add($section);
416
if ($pconfig['gid'] != 1998) { // all users group
417

    
418
	// ==== Group membership ==================================================
419
	$group = new Form_Group('Group membership');
420

    
421
	// Make a list of all the groups configured on the system, and a list of
422
	// those which this user is a member of
423
	$systemGroups = array();
424
	$usersGroups = array();
425

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

    
434
	$group->add(new Form_Select(
435
		'notmembers',
436
		null,
437
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
438
		$systemGroups,
439
		true
440
	))->setHelp('Not members');
441

    
442
	$group->add(new Form_Select(
443
		'members',
444
		null,
445
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
446
		$usersGroups,
447
		true
448
	))->setHelp('Members');
449

    
450
	$section->add($group);
451

    
452
	$group = new Form_Group('');
453

    
454
	$group->add(new Form_Button(
455
		'movetoenabled',
456
		'Move to "Members"',
457
		null,
458
		'fa-angle-double-right'
459
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass('btn-info btn-sm');
460

    
461
	$group->add(new Form_Button(
462
		'movetodisabled',
463
		'Move to "Not members',
464
		null,
465
		'fa-angle-double-left'
466
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass('btn-info btn-sm');
467

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

    
471
}
472

    
473
if ($_POST['act'] != "new") {
474
	$section = new Form_Section('Assigned Privileges');
475

    
476
	$section->addInput(new Form_StaticText(
477
		null,
478
		build_priv_table()
479
	));
480

    
481

    
482
	$form->add($section);
483
}
484

    
485
print $form;
486
?>
487
<script type="text/javascript">
488
//<![CDATA[
489
events.push(function() {
490

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

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

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