Project

General

Profile

Download (12.6 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 Electric Sheep Fencing, LLC
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
##|*MATCH=system_groupmanager.php*
33
##|-PRIV
34

    
35
require_once("guiconfig.inc");
36

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

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

    
43
unset($id);
44

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
138
if (isset($_POST['save'])) {
139
	unset($input_errors);
140
	$pconfig = $_POST;
141

    
142
	/* input validation */
143
	$reqdfields = explode(" ", "groupname");
144
	$reqdfieldsn = array(gettext("Group Name"));
145

    
146
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
147

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

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

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

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

    
181
	if (!$input_errors) {
182
		$group = array();
183
		if (isset($id) && $a_group[$id]) {
184
			$group = $a_group[$id];
185
		}
186

    
187
		$group['name'] = $_POST['groupname'];
188
		$group['description'] = $_POST['description'];
189
		$group['scope'] = $_POST['gtype'];
190

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

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

    
204
		admin_groups_sort();
205

    
206
		conf_mount_rw();
207
		local_group_set($group);
208
		conf_mount_ro();
209

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

    
220
		write_config();
221

    
222
		header("Location: system_groupmanager.php");
223
		exit;
224
	}
225

    
226
	$pconfig['name'] = $_POST['groupname'];
227
}
228

    
229
function build_priv_table() {
230
	global $a_group, $id;
231

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

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

    
250
	}
251

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

    
256
	$privhtml .= '<nav class="action-buttons">';
257
	$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>';
258
	$privhtml .= '</nav>';
259

    
260
	return($privhtml);
261
}
262

    
263
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Groups"));
264

    
265
if ($act == "new" || $act == "edit") {
266
	$pgtitle[] = gettext('Edit');
267
}
268

    
269
include("head.inc");
270

    
271
if ($input_errors) {
272
	print_input_errors($input_errors);
273
}
274

    
275
if ($savemsg) {
276
	print_info_box($savemsg, 'success');
277
}
278

    
279
$tab_array = array();
280
$tab_array[] = array(gettext("Users"), false, "system_usermanager.php");
281
$tab_array[] = array(gettext("Groups"), true, "system_groupmanager.php");
282
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
283
$tab_array[] = array(gettext("Authentication Servers"), false, "system_authservers.php");
284
display_top_tabs($tab_array);
285

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

    
336
<nav class="action-buttons">
337
	<a href="?act=new" class="btn btn-success btn-sm">
338
		<i class="fa fa-plus icon-embed-btn"></i>
339
		<?=gettext("Add")?>
340
	</a>
341
</nav>
342
<?php
343
	include('foot.inc');
344
	exit;
345
}
346

    
347
$form = new Form;
348
$form->setAction('system_groupmanager.php?act=edit');
349
$form->addGlobal(new Form_Input(
350
	'groupid',
351
	null,
352
	'hidden',
353
	$id
354
));
355

    
356
if (isset($id) && $a_group[$id]){
357
	$form->addGlobal(new Form_Input(
358
		'id',
359
		null,
360
		'hidden',
361
		$id
362
	));
363

    
364
	$form->addGlobal(new Form_Input(
365
		'gid',
366
		null,
367
		'hidden',
368
		$pconfig['gid']
369
	));
370
}
371

    
372
$section = new Form_Section('Group Properties');
373

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

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

    
384
	$section->addInput(new Form_Input(
385
		'gtype',
386
		'Scope',
387
		'text',
388
		$pconfig['gtype']
389
	))->setReadonly();
390
} else {
391
	$section->addInput(new Form_Select(
392
		'gtype',
393
		'Scope',
394
		$pconfig['gtype'],
395
		["local" => gettext("Local"), "remote" => gettext("Remote")]
396
	));
397
}
398

    
399
$section->addInput(new Form_Input(
400
	'description',
401
	'Description',
402
	'text',
403
	$pconfig['description']
404
))->setHelp('Group description, for administrative information only');
405

    
406

    
407
$form->add($section);
408
if ($pconfig['gid'] != 1998) { // all users group
409

    
410
	// ==== Group membership ==================================================
411
	$group = new Form_Group('Group membership');
412

    
413
	// Make a list of all the groups configured on the system, and a list of
414
	// those which this user is a member of
415
	$systemGroups = array();
416
	$usersGroups = array();
417

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

    
426
	$group->add(new Form_Select(
427
		'notmembers',
428
		null,
429
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
430
		$systemGroups,
431
		true
432
	))->setHelp('Not members');
433

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

    
442
	$section->add($group);
443

    
444
	$group = new Form_Group('');
445

    
446
	$group->add(new Form_Button(
447
		'movetoenabled',
448
		'Move to "Members"',
449
		null,
450
		'fa-angle-double-right'
451
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass('btn-info btn-sm');
452

    
453
	$group->add(new Form_Button(
454
		'movetodisabled',
455
		'Move to "Not members',
456
		null,
457
		'fa-angle-double-left'
458
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass('btn-info btn-sm');
459

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

    
463
}
464

    
465
if ($_GET['act'] != "new") {
466
	$section = new Form_Section('Assigned Privileges');
467

    
468
	$section->addInput(new Form_StaticText(
469
		null,
470
		build_priv_table()
471
	));
472

    
473

    
474
	$form->add($section);
475
}
476

    
477
print $form;
478
?>
479
<script type="text/javascript">
480
//<![CDATA[
481
events.push(function() {
482

    
483
	// On click . .
484
	$("#movetodisabled").click(function() {
485
		moveOptions($('[name="members[]"] option'), $('[name="notmembers[]"]'));
486
	});
487

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

    
492
	// On submit mark all the user's groups as "selected"
493
	$('form').submit(function() {
494
		AllServers($('[name="members[]"] option'), true);
495
	});
496
});
497
//]]>
498
</script>
499
<?php
500
include('foot.inc');
(201-201/227)