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

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

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

    
44
unset($id);
45

    
46
if (isset($_POST['groupid']) && is_numericint($_POST['groupid'])) {
47
	$id = $_POST['groupid'];
48
}
49

    
50
if (isset($_GET['groupid']) && is_numericint($_GET['groupid'])) {
51
	$id = $_GET['groupid'];
52
}
53

    
54
$act = (isset($_GET['act']) ? $_GET['act'] : '');
55

    
56
function cpusercmp($a, $b) {
57
	return strcasecmp($a['name'], $b['name']);
58
}
59

    
60
function admin_groups_sort() {
61
	global $a_group;
62

    
63
	if (!is_array($a_group)) {
64
		return;
65
	}
66

    
67
	usort($a_group, "cpusercmp");
68
}
69

    
70
if ($act == "delgroup") {
71

    
72
	if (!isset($id) || !isset($_GET['groupname']) || !isset($a_group[$id]) || ($_GET['groupname'] != $a_group[$id]['name'])) {
73
		pfSenseHeader("system_groupmanager.php");
74
		exit;
75
	}
76

    
77
	local_group_del($a_group[$id]);
78
	$groupdeleted = $a_group[$id]['name'];
79
	unset($a_group[$id]);
80
	write_config();
81
	$savemsg = sprintf(gettext("Group %s successfully deleted."), $groupdeleted);
82
}
83

    
84
if ($act == "delpriv") {
85

    
86
	if (!isset($id) || !isset($a_group[$id])) {
87
		pfSenseHeader("system_groupmanager.php");
88
		exit;
89
	}
90

    
91
	$privdeleted = $priv_list[$a_group[$id]['priv'][$_POST['privid']]]['name'];
92
	unset($a_group[$id]['priv'][$_GET['privid']]);
93

    
94
	if (is_array($a_group[$id]['member'])) {
95
		foreach ($a_group[$id]['member'] as $uid) {
96
			$user = getUserEntryByUID($uid);
97
			if ($user) {
98
				local_user_set($user);
99
			}
100
		}
101
	}
102

    
103
	write_config();
104
	$act = "edit";
105
	$savemsg = sprintf(gettext("Privilege %s successfully deleted."), $privdeleted);
106
}
107

    
108
if ($act == "edit") {
109
	if (isset($id) && isset($a_group[$id])) {
110
		$pconfig['name'] = $a_group[$id]['name'];
111
		$pconfig['gid'] = $a_group[$id]['gid'];
112
		$pconfig['gtype'] = empty($a_group[$id]['scope']) ? "local" : $a_group[$id]['scope'];
113
		$pconfig['description'] = $a_group[$id]['description'];
114
		$pconfig['members'] = $a_group[$id]['member'];
115
		$pconfig['priv'] = $a_group[$id]['priv'];
116
	}
117
}
118

    
119
if (isset($_GET['dellall_x'])) {
120

    
121
	$del_groups = $_GET['delete_check'];
122

    
123
	if (!empty($del_groups)) {
124
		foreach ($del_groups as $groupid) {
125
			if (isset($a_group[$groupid]) && $a_group[$groupid]['scope'] != "system") {
126
				local_group_del($a_group[$groupid]);
127
				unset($a_group[$groupid]);
128
			}
129
		}
130
		$savemsg = gettext("Selected groups removed successfully.");
131
		write_config($savemsg);
132
	}
133
}
134

    
135
if (isset($_POST['save'])) {
136
	unset($input_errors);
137
	$pconfig = $_POST;
138

    
139
	/* input validation */
140
	$reqdfields = explode(" ", "groupname");
141
	$reqdfieldsn = array(gettext("Group Name"));
142

    
143
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
144

    
145
	if ($_POST['gtype'] != "remote") {
146
		if (preg_match("/[^a-zA-Z0-9\.\-_]/", $_POST['groupname'])) {
147
			$input_errors[] = sprintf(gettext("The (%s) group name contains invalid characters."), $_POST['gtype']);
148
		}
149
	} else {
150
		if (preg_match("/[^a-zA-Z0-9\.\- _]/", $_POST['groupname'])) {
151
			$input_errors[] = sprintf(gettext("The (%s) group name contains invalid characters."), $_POST['gtype']);
152
		}
153
	}
154

    
155
	if (strlen($_POST['groupname']) > 16) {
156
		$input_errors[] = gettext("The group name is longer than 16 characters.");
157
	}
158

    
159
	/* Check the POSTed members to ensure they are valid and exist */
160
	if (is_array($_POST['members'])) {
161
		foreach ($_POST['members'] as $newmember) {
162
			if (!is_numeric($newmember) || empty(getUserEntryByUID($newmember))) {
163
				$input_errors[] = gettext("One or more invalid group members was submitted.");
164
			}
165
		}
166
	}
167

    
168
	if (!$input_errors && !(isset($id) && $a_group[$id])) {
169
		/* make sure there are no dupes */
170
		foreach ($a_group as $group) {
171
			if ($group['name'] == $_POST['groupname']) {
172
				$input_errors[] = gettext("Another entry with the same group name already exists.");
173
				break;
174
			}
175
		}
176
	}
177

    
178
	if (!$input_errors) {
179
		$group = array();
180
		if (isset($id) && $a_group[$id]) {
181
			$group = $a_group[$id];
182
		}
183

    
184
		$group['name'] = $_POST['groupname'];
185
		$group['description'] = $_POST['description'];
186
		$group['scope'] = $_POST['gtype'];
187

    
188
		if (empty($_POST['members'])) {
189
			unset($group['member']);
190
		} else if ($group['gid'] != 1998) { // all group
191
			$group['member'] = $_POST['members'];
192
		}
193

    
194
		if (isset($id) && $a_group[$id]) {
195
			$a_group[$id] = $group;
196
		} else {
197
			$group['gid'] = $config['system']['nextgid']++;
198
			$a_group[] = $group;
199
		}
200

    
201
		admin_groups_sort();
202

    
203
		local_group_set($group);
204

    
205
		/* Refresh users in this group since their privileges may have changed. */
206
		if (is_array($group['member'])) {
207
			$a_user = &$config['system']['user'];
208
			foreach ($a_user as & $user) {
209
				if (in_array($user['uid'], $group['member'])) {
210
					local_user_set($user);
211
				}
212
			}
213
		}
214

    
215
		write_config();
216

    
217
		header("Location: system_groupmanager.php");
218
		exit;
219
	}
220

    
221
	$pconfig['name'] = $_POST['groupname'];
222
}
223

    
224
function build_priv_table() {
225
	global $a_group, $id;
226

    
227
	$privhtml = '<div class="table-responsive">';
228
	$privhtml .=	'<table class="table table-striped table-hover table-condensed">';
229
	$privhtml .=		'<thead>';
230
	$privhtml .=			'<tr>';
231
	$privhtml .=				'<th>' . gettext('Name') . '</th>';
232
	$privhtml .=				'<th>' . gettext('Description') . '</th>';
233
	$privhtml .=				'<th>' . gettext('Action') . '</th>';
234
	$privhtml .=			'</tr>';
235
	$privhtml .=		'</thead>';
236
	$privhtml .=		'<tbody>';
237

    
238
	$user_has_root_priv = false;
239

    
240
	foreach (get_user_privdesc($a_group[$id]) as $i => $priv) {
241
		$privhtml .=		'<tr>';
242
		$privhtml .=			'<td>' . htmlspecialchars($priv['name']) . '</td>';
243
		$privhtml .=			'<td>' . htmlspecialchars($priv['descr']);
244
		if (isset($priv['warn']) && ($priv['warn'] == 'standard-warning-root')) {
245
			$privhtml .=			' ' . gettext('(admin privilege)');
246
			$user_has_root_priv = true;
247
		}
248
		$privhtml .=			'</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
	if ($user_has_root_priv) {
255
		$privhtml .=		'<tr>';
256
		$privhtml .=			'<td colspan="2">';
257
		$privhtml .=				'<b>' . gettext('Security notice: Users in this group effectively have administrator-level access') . '</b>';
258
		$privhtml .=			'</td>';
259
		$privhtml .=			'<td>';
260
		$privhtml .=			'</td>';
261
		$privhtml .=		'</tr>';
262
		
263
	}
264

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

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

    
273
	return($privhtml);
274
}
275

    
276
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Groups"));
277
$pglinks = array("", "system_usermanager.php", "system_groupmanager.php");
278

    
279
if ($act == "new" || $act == "edit") {
280
	$pgtitle[] = gettext('Edit');
281
	$pglinks[] = "@self";
282
}
283

    
284
include("head.inc");
285

    
286
if ($input_errors) {
287
	print_input_errors($input_errors);
288
}
289

    
290
if ($savemsg) {
291
	print_info_box($savemsg, 'success');
292
}
293

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

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

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

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

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

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

    
387
$section = new Form_Section('Group Properties');
388

    
389
$section->addInput($input = new Form_Input(
390
	'groupname',
391
	'*Group name',
392
	'text',
393
	$pconfig['name']
394
));
395

    
396
if ($pconfig['gtype'] == "system") {
397
	$input->setReadonly();
398

    
399
	$section->addInput(new Form_Input(
400
		'gtype',
401
		'*Scope',
402
		'text',
403
		$pconfig['gtype']
404
	))->setReadonly();
405
} else {
406
	$section->addInput(new Form_Select(
407
		'gtype',
408
		'*Scope',
409
		$pconfig['gtype'],
410
		["local" => gettext("Local"), "remote" => gettext("Remote")]
411
	));
412
}
413

    
414
$section->addInput(new Form_Input(
415
	'description',
416
	'Description',
417
	'text',
418
	$pconfig['description']
419
))->setHelp('Group description, for administrative information only');
420

    
421

    
422
$form->add($section);
423
if ($pconfig['gid'] != 1998) { // all users group
424

    
425
	// ==== Group membership ==================================================
426
	$group = new Form_Group('Group membership');
427

    
428
	// Make a list of all the groups configured on the system, and a list of
429
	// those which this user is a member of
430
	$systemGroups = array();
431
	$usersGroups = array();
432

    
433
	foreach ($config['system']['user'] as $user) {
434
		if (is_array($pconfig['members']) && in_array($user['uid'], $pconfig['members'])) {
435
			$usersGroups[ $user['uid'] ] = $user['name'];	// Add it to the user's list
436
		} else {
437
			$systemGroups[ $user['uid'] ] = $user['name']; // Add it to the 'not a member of' list
438
		}
439
	}
440

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

    
449
	$group->add(new Form_Select(
450
		'members',
451
		null,
452
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
453
		$usersGroups,
454
		true
455
	))->setHelp('Members');
456

    
457
	$section->add($group);
458

    
459
	$group = new Form_Group('');
460

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

    
468
	$group->add(new Form_Button(
469
		'movetodisabled',
470
		'Move to "Not members',
471
		null,
472
		'fa-angle-double-left'
473
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass('btn-info btn-sm');
474

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

    
478
}
479

    
480
if ($_GET['act'] != "new") {
481
	$section = new Form_Section('Assigned Privileges');
482

    
483
	$section->addInput(new Form_StaticText(
484
		null,
485
		build_priv_table()
486
	));
487

    
488

    
489
	$form->add($section);
490
}
491

    
492
print $form;
493
?>
494
<script type="text/javascript">
495
//<![CDATA[
496
events.push(function() {
497

    
498
	// On click . .
499
	$("#movetodisabled").click(function() {
500
		moveOptions($('[name="members[]"] option'), $('[name="notmembers[]"]'));
501
	});
502

    
503
	$("#movetoenabled").click(function() {
504
		moveOptions($('[name="notmembers[]"] option'), $('[name="members[]"]'));
505
	});
506

    
507
	// On submit mark all the user's groups as "selected"
508
	$('form').submit(function() {
509
		AllServers($('[name="members[]"] option'), true);
510
	});
511
});
512
//]]>
513
</script>
514
<?php
515
include('foot.inc');
(199-199/225)