Project

General

Profile

Download (12.5 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
##|*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
	local_group_del($a_group[$id]);
77
	$groupdeleted = $a_group[$id]['name'];
78
	unset($a_group[$id]);
79
	write_config();
80
	$savemsg = sprintf(gettext("Group %s successfully deleted."), $groupdeleted);
81
}
82

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

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

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

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

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

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

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

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

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

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

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

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

    
144
	if ($_POST['gtype'] != "remote") {
145
		if (preg_match("/[^a-zA-Z0-9\.\-_]/", $_POST['groupname'])) {
146
			$input_errors[] = sprintf(gettext("The (%s) group name contains invalid characters."), $_POST['gtype']);
147
		}
148
	} else {
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
	}
153

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

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

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

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

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

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

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

    
200
		admin_groups_sort();
201

    
202
		local_group_set($group);
203

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

    
214
		write_config();
215

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

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

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

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

    
237
	foreach (get_user_privdesc($a_group[$id]) as $i => $priv) {
238
		$privhtml .=		'<tr>';
239
		$privhtml .=			'<td>' . htmlspecialchars($priv['name']) . '</td>';
240
		$privhtml .=			'<td>' . htmlspecialchars($priv['descr']) . '</td>';
241
		$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>';
242
		$privhtml .=		'</tr>';
243

    
244
	}
245

    
246
	$privhtml .=		'</tbody>';
247
	$privhtml .=	'</table>';
248
	$privhtml .= '</div>';
249

    
250
	$privhtml .= '<nav class="action-buttons">';
251
	$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>';
252
	$privhtml .= '</nav>';
253

    
254
	return($privhtml);
255
}
256

    
257
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Groups"));
258

    
259
if ($act == "new" || $act == "edit") {
260
	$pgtitle[] = gettext('Edit');
261
}
262

    
263
include("head.inc");
264

    
265
if ($input_errors) {
266
	print_input_errors($input_errors);
267
}
268

    
269
if ($savemsg) {
270
	print_info_box($savemsg, 'success');
271
}
272

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

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

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

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

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

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

    
366
$section = new Form_Section('Group Properties');
367

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

    
375
if ($pconfig['gtype'] == "system") {
376
	$input->setReadonly();
377

    
378
	$section->addInput(new Form_Input(
379
		'gtype',
380
		'Scope',
381
		'text',
382
		$pconfig['gtype']
383
	))->setReadonly();
384
} else {
385
	$section->addInput(new Form_Select(
386
		'gtype',
387
		'Scope',
388
		$pconfig['gtype'],
389
		["local" => gettext("Local"), "remote" => gettext("Remote")]
390
	));
391
}
392

    
393
$section->addInput(new Form_Input(
394
	'description',
395
	'Description',
396
	'text',
397
	$pconfig['description']
398
))->setHelp('Group description, for administrative information only');
399

    
400

    
401
$form->add($section);
402
if ($pconfig['gid'] != 1998) { // all users group
403

    
404
	// ==== Group membership ==================================================
405
	$group = new Form_Group('Group membership');
406

    
407
	// Make a list of all the groups configured on the system, and a list of
408
	// those which this user is a member of
409
	$systemGroups = array();
410
	$usersGroups = array();
411

    
412
	foreach ($config['system']['user'] as $user) {
413
		if (is_array($pconfig['members']) && in_array($user['uid'], $pconfig['members'])) {
414
			$usersGroups[ $user['uid'] ] = $user['name'];	// Add it to the user's list
415
		} else {
416
			$systemGroups[ $user['uid'] ] = $user['name']; // Add it to the 'not a member of' list
417
		}
418
	}
419

    
420
	$group->add(new Form_Select(
421
		'notmembers',
422
		null,
423
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
424
		$systemGroups,
425
		true
426
	))->setHelp('Not members');
427

    
428
	$group->add(new Form_Select(
429
		'members',
430
		null,
431
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
432
		$usersGroups,
433
		true
434
	))->setHelp('Members');
435

    
436
	$section->add($group);
437

    
438
	$group = new Form_Group('');
439

    
440
	$group->add(new Form_Button(
441
		'movetoenabled',
442
		'Move to "Members"',
443
		null,
444
		'fa-angle-double-right'
445
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass('btn-info btn-sm');
446

    
447
	$group->add(new Form_Button(
448
		'movetodisabled',
449
		'Move to "Not members',
450
		null,
451
		'fa-angle-double-left'
452
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass('btn-info btn-sm');
453

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

    
457
}
458

    
459
if ($_GET['act'] != "new") {
460
	$section = new Form_Section('Assigned Privileges');
461

    
462
	$section->addInput(new Form_StaticText(
463
		null,
464
		build_priv_table()
465
	));
466

    
467

    
468
	$form->add($section);
469
}
470

    
471
print $form;
472
?>
473
<script type="text/javascript">
474
//<![CDATA[
475
events.push(function() {
476

    
477
	// On click . .
478
	$("#movetodisabled").click(function() {
479
		moveOptions($('[name="members[]"] option'), $('[name="notmembers[]"]'));
480
	});
481

    
482
	$("#movetoenabled").click(function() {
483
		moveOptions($('[name="notmembers[]"] option'), $('[name="members[]"]'));
484
	});
485

    
486
	// On submit mark all the user's groups as "selected"
487
	$('form').submit(function() {
488
		AllServers($('[name="members[]"] option'), true);
489
	});
490
});
491
//]]>
492
</script>
493
<?php
494
include('foot.inc');
(199-199/225)