Project

General

Profile

« Previous | Next » 

Revision 1bb9c407

Added by Marcos M about 1 year ago

Use config accessors in users and groups functions

View differences:

src/usr/local/www/system_usermanager.php
54 54
	$id = $_REQUEST['userid'];
55 55
}
56 56

  
57
init_config_arr(array('system', 'user'));
58
$a_user = &$config['system']['user'];
57
config_init_path('system/user');
59 58
$act = $_REQUEST['act'];
60 59

  
61 60
if (isset($_SERVER['HTTP_REFERER'])) {
......
64 63
	$referer = '/system_usermanager.php';
65 64
}
66 65

  
67
if (isset($id) && $a_user[$id]) {
68
	$pconfig['usernamefld'] = $a_user[$id]['name'];
69
	$pconfig['descr'] = $a_user[$id]['descr'];
70
	$pconfig['expires'] = $a_user[$id]['expires'];
71
	$pconfig['customsettings'] = isset($a_user[$id]['customsettings']);
72
	$pconfig['webguicss'] = $a_user[$id]['webguicss'];
73
	$pconfig['webguifixedmenu'] = $a_user[$id]['webguifixedmenu'];
74
	$pconfig['webguihostnamemenu'] = $a_user[$id]['webguihostnamemenu'];
75
	$pconfig['dashboardcolumns'] = $a_user[$id]['dashboardcolumns'];
76
	$pconfig['interfacessort'] = isset($a_user[$id]['interfacessort']);
77
	$pconfig['dashboardavailablewidgetspanel'] = isset($a_user[$id]['dashboardavailablewidgetspanel']);
78
	$pconfig['systemlogsfilterpanel'] = isset($a_user[$id]['systemlogsfilterpanel']);
79
	$pconfig['systemlogsmanagelogpanel'] = isset($a_user[$id]['systemlogsmanagelogpanel']);
80
	$pconfig['statusmonitoringsettingspanel'] = isset($a_user[$id]['statusmonitoringsettingspanel']);
81
	$pconfig['webguileftcolumnhyper'] = isset($a_user[$id]['webguileftcolumnhyper']);
82
	$pconfig['disablealiaspopupdetail'] = isset($a_user[$id]['disablealiaspopupdetail']);
83
	$pconfig['pagenamefirst'] = isset($a_user[$id]['pagenamefirst']);
84
	$pconfig['groups'] = local_user_get_groups($a_user[$id]);
85
	$pconfig['utype'] = $a_user[$id]['scope'];
86
	$pconfig['uid'] = $a_user[$id]['uid'];
87
	$pconfig['authorizedkeys'] = base64_decode($a_user[$id]['authorizedkeys']);
88
	$pconfig['priv'] = $a_user[$id]['priv'];
89
	$pconfig['ipsecpsk'] = $a_user[$id]['ipsecpsk'];
90
	$pconfig['disabled'] = isset($a_user[$id]['disabled']);
91
	$pconfig['keephistory'] = isset($a_user[$id]['keephistory']);
66
if (isset($id)) {
67
	$this_user = config_get_path("system/user/{$id}");
68
}
69
if ($this_user) {
70
	$pconfig['usernamefld'] = $this_user['name'];
71
	$pconfig['descr'] = $this_user['descr'];
72
	$pconfig['expires'] = $this_user['expires'];
73
	$pconfig['customsettings'] = isset($this_user['customsettings']);
74
	$pconfig['webguicss'] = $this_user['webguicss'];
75
	$pconfig['webguifixedmenu'] = $this_user['webguifixedmenu'];
76
	$pconfig['webguihostnamemenu'] = $this_user['webguihostnamemenu'];
77
	$pconfig['dashboardcolumns'] = $this_user['dashboardcolumns'];
78
	$pconfig['interfacessort'] = isset($this_user['interfacessort']);
79
	$pconfig['dashboardavailablewidgetspanel'] = isset($this_user['dashboardavailablewidgetspanel']);
80
	$pconfig['systemlogsfilterpanel'] = isset($this_user['systemlogsfilterpanel']);
81
	$pconfig['systemlogsmanagelogpanel'] = isset($this_user['systemlogsmanagelogpanel']);
82
	$pconfig['statusmonitoringsettingspanel'] = isset($this_user['statusmonitoringsettingspanel']);
83
	$pconfig['webguileftcolumnhyper'] = isset($this_user['webguileftcolumnhyper']);
84
	$pconfig['disablealiaspopupdetail'] = isset($this_user['disablealiaspopupdetail']);
85
	$pconfig['pagenamefirst'] = isset($this_user['pagenamefirst']);
86
	$pconfig['groups'] = local_user_get_groups($this_user);
87
	$pconfig['utype'] = $this_user['scope'];
88
	$pconfig['uid'] = $this_user['uid'];
89
	$pconfig['authorizedkeys'] = base64_decode($this_user['authorizedkeys']);
90
	$pconfig['priv'] = $this_user['priv'];
91
	$pconfig['ipsecpsk'] = $this_user['ipsecpsk'];
92
	$pconfig['disabled'] = isset($this_user['disabled']);
93
	$pconfig['keephistory'] = isset($this_user['keephistory']);
92 94
}
93 95

  
94 96
/*
......
98 100
 */
99 101
phpsession_begin();
100 102
$guiuser = getUserEntry($_SESSION['Username']);
103
$guiuser = $guiuser['item'];
101 104
$read_only = (is_array($guiuser) && userHasPrivilege($guiuser, "user-config-readonly"));
102 105
phpsession_end();
103 106

  
......
107 110

  
108 111
if (($_POST['act'] == "deluser") && !$read_only) {
109 112

  
110
	if (!isset($_POST['username']) || !isset($a_user[$id]) || ($_POST['username'] != $a_user[$id]['name'])) {
113
	if (!isset($_POST['username']) || (config_get_path("system/user/{$id}") === null) || ($_POST['username'] != config_get_path("system/user/{$id}/name"))) {
111 114
		pfSenseHeader("system_usermanager.php");
112 115
		exit;
113 116
	}
......
115 118
	if ($_POST['username'] == $_SESSION['Username']) {
116 119
		$delete_errors[] = sprintf(gettext("Cannot delete user %s because you are currently logged in as that user."), $_POST['username']);
117 120
	} else {
118
		local_user_del($a_user[$id]);
119
		$userdeleted = $a_user[$id]['name'];
120
		unset($a_user[$id]);
121
		local_user_del(config_get_path("system/user/{$id}"));
122
		$userdeleted = config_get_path("system/user/{$id}/name");
123
		config_del_path("system/user/{$id}");
121 124
		/* Reindex the array to avoid operating on an incorrect index https://redmine.pfsense.org/issues/7733 */
122
		$a_user = array_values($a_user);
125
		config_set_path('system/user', array_values(config_get_path('system/user')));
123 126
		$savemsg = sprintf(gettext("Successfully deleted user: %s"), $userdeleted);
124 127
		write_config($savemsg);
125 128
		syslog($logging_level, "{$logging_prefix}: {$savemsg}");
......
135 138
	$pconfig['lifetime'] = 3650;
136 139

  
137 140
	$nonPrvCas = array();
138
	if (is_array($config['ca']) && count($config['ca']) > 0) {
139
		foreach (config_get_path('ca', []) as $ca) {
140
			if (!$ca['prv']) {
141
				continue;
142
			}
143

  
144
			$nonPrvCas[ $ca['refid'] ] = $ca['descr'];
141
	foreach (config_get_path('ca', []) as $ca) {
142
		if (!$ca['prv']) {
143
			continue;
145 144
		}
145

  
146
		$nonPrvCas[ $ca['refid'] ] = $ca['descr'];
146 147
	}
147 148

  
148 149
}
......
154 155

  
155 156
	if (!empty($del_users)) {
156 157
		foreach ($del_users as $userid) {
157
			if (isset($a_user[$userid]) && $a_user[$userid]['scope'] != "system") {
158
				if ($a_user[$userid]['name'] == $_SESSION['Username']) {
159
					$delete_errors[] = sprintf(gettext("Cannot delete user %s because you are currently logged in as that user."), $a_user[$userid]['name']);
158
			$tmp_user = config_get_path("system/user/{$userid}", []);
159
			if ($tmp_user['scope'] != "system") {
160
				if ($tmp_user['name'] == $_SESSION['Username']) {
161
					$delete_errors[] = sprintf(gettext("Cannot delete user %s because you are currently logged in as that user."), $tmp_user['name']);
160 162
				} else {
161
					$deleted_users[] = $a_user[$userid]['name'];
162
					local_user_del($a_user[$userid]);
163
					unset($a_user[$userid]);
163
					$deleted_users[] = $tmp_user['name'];
164
					local_user_del($tmp_user);
165
					config_del_path("system/user/{$userid}");
164 166
				}
165 167
			} else {
166
				$delete_errors[] = sprintf(gettext("Cannot delete user %s because it is a system user."), $a_user[$userid]['name']);
168
				$delete_errors[] = sprintf(gettext("Cannot delete user %s because it is a system user."), $tmp_user['name']);
167 169
			}
168 170
		}
169 171

  
170 172
		if (count($deleted_users) > 0) {
171 173
			$savemsg = sprintf(gettext("Successfully deleted %s: %s"), (count($deleted_users) == 1) ? gettext("user") : gettext("users"), implode(', ', $deleted_users));
172 174
			/* Reindex the array to avoid operating on an incorrect index https://redmine.pfsense.org/issues/7733 */
173
			$a_user = array_values($a_user);
175
			config_set_path('system/user', array_values(config_get_path('system/user')));
174 176
			write_config($savemsg);
175 177
			syslog($logging_level, "{$logging_prefix}: {$savemsg}");
176 178
		}
......
179 181

  
180 182
if (($_POST['act'] == "delcert") && !$read_only) {
181 183

  
182
	if (!$a_user[$id]) {
184
	if (!config_get_path("system/user/{$id}")) {
183 185
		pfSenseHeader("system_usermanager.php");
184 186
		exit;
185 187
	}
186 188

  
187
	$certdeleted = lookup_cert($a_user[$id]['cert'][$_POST['certid']]);
188
	$certdeleted = $certdeleted['descr'];
189
	unset($a_user[$id]['cert'][$_POST['certid']]);
190
	$savemsg = sprintf(gettext("Removed certificate association \"%s\" from user %s"), $certdeleted, $a_user[$id]['name']);
189
	$certdeleted = lookup_cert(config_get_path("system/user/{$id}/cert/{$_POST['certid']}"));
190
	$certdeleted = $certdeleted['item']['descr'];
191
	$savemsg = sprintf(gettext("Removed certificate association \"%s\" from user %s"), $certdeleted, config_get_path("system/user/{$id}/name"));
192
	config_del_path("system/user/{$id}/cert/{$_POST['certid']}");
191 193
	write_config($savemsg);
192 194
	syslog($logging_level, "{$logging_prefix}: {$savemsg}");
193 195
	$_POST['act'] = "edit";
194 196
}
195 197

  
196 198
if (($_POST['act'] == "delprivid") && !$read_only) {
197
	$privdeleted = $priv_list[$a_user[$id]['priv'][$_POST['privid']]]['name'];
198
	unset($a_user[$id]['priv'][$_POST['privid']]);
199
	local_user_set($a_user[$id]);
200
	$savemsg = sprintf(gettext("Removed Privilege \"%s\" from user %s"), $privdeleted, $a_user[$id]['name']);
199
	$privdeleted = array_get_path($priv_list, (config_get_path("system/user/{$id}/priv/{$_POST['privid']}") . '/name'));
200
	config_del_path("system/user/{$id}/priv/{$_POST['privid']}");
201
	local_user_set(config_get_path("system/user/{$id}"));
202
	$savemsg = sprintf(gettext("Removed Privilege \"%s\" from user %s"), $privdeleted, config_get_path("system/user/{$id}/name"));
201 203
	write_config($savemsg);
202 204
	syslog($logging_level, "{$logging_prefix}: {$savemsg}");
203 205
	$_POST['act'] = "edit";
......
209 211
	$pconfig = $_POST;
210 212

  
211 213
	/* input validation */
212
	if (isset($id) && ($a_user[$id])) {
214
	if (isset($id) && config_get_path("system/user/{$id}")) {
213 215
		$reqdfields = explode(" ", "usernamefld");
214 216
		$reqdfieldsn = array(gettext("Username"));
215 217
	} else {
......
259 261
		}
260 262
	}
261 263

  
262
	if (isset($id) && $a_user[$id]) {
263
		$oldusername = $a_user[$id]['name'];
264
	} else {
265
		$oldusername = "";
266
	}
264
	$oldusername = config_get_path("system/user/{$id}/name", '');
267 265
	/* make sure this user name is unique */
268 266
	if (!$input_errors) {
269
		foreach ($a_user as $userent) {
267
		foreach (config_get_path('system/user') as $userent) {
270 268
			if ($userent['name'] == $_POST['usernamefld'] && $oldusername != $_POST['usernamefld']) {
271 269
				$input_errors[] = gettext("Another entry with the same username already exists.");
272 270
				break;
......
315 313
	validate_dashboardcolumns_field($input_errors, $_POST['dashboardcolumns']);
316 314

  
317 315
	if (!$input_errors) {
318

  
319
		$userent = array();
320
		if (isset($id) && $a_user[$id]) {
321
			$userent = $a_user[$id];
316
		if (isset($id) && config_get_path("system/user/{$id}")) {
317
			$user_item_config = [
318
				'idx' => $id,
319
				'item' => config_get_path("system/user/{$id}")
320
			];
321
		} else {
322
			$user_item_config = ['idx' => null, 'item' => null];
322 323
		}
324
		$userent = &$user_item_config['item'];
323 325

  
324 326
		isset($_POST['utype']) ? $userent['scope'] = $_POST['utype'] : $userent['scope'] = "system";
325 327

  
......
331 333

  
332 334
		/* the user password was modified */
333 335
		if ($_POST['passwordfld1']) {
334
			local_user_set_password($userent, $_POST['passwordfld1']);
336
			local_user_set_password($user_item_config, $_POST['passwordfld1']);
335 337
		}
336 338

  
337 339
		/* only change description if sent */
......
429 431
			unset($userent['keephistory']);
430 432
		}
431 433

  
432
		if (isset($id) && $a_user[$id]) {
433
			$a_user[$id] = $userent;
434
		if (isset($id) && config_get_path("system/user/{$id}")) {
435
			config_set_path("system/user/{$id}", $userent);
434 436
		} else {
435 437
			if (!empty($_POST['name'])) {
436 438
				$cert = array();
......
468 470
					$_POST['digest_alg'], $_POST['keytype'],
469 471
					$_POST['ecname']);
470 472

  
471
				if (!is_array($config['cert'])) {
472
					config_set_path('cert', array());
473
				}
474
				$config['cert'][] = $cert;
473
				config_set_path('cert/', $cert);
475 474
				$userent['cert'][] = $cert['refid'];
476 475
			}
477
			$userent['uid'] = $config['system']['nextuid']++;
476
			$nextuid_config = config_get_path('system/nextuid');
477
			$userent['uid'] = $nextuid_config++;
478
			config_set_path('system/nextuid', $nextuid_config);
478 479
			/* Add the user to All Users group. */
479
			foreach (config_get_path('system/group', []) as $gidx => $group) {
480
			$group_config = config_get_path('system/group', []);
481
			foreach ($group_config as $gidx => &$group) {
480 482
				if ($group['name'] == "all") {
481
					if (!is_array($config['system']['group'][$gidx]['member'])) {
482
						$config['system']['group'][$gidx]['member'] = array();
483
					if (!is_array($group['member'])) {
484
						$group['member'] = [];
483 485
					}
484
					$config['system']['group'][$gidx]['member'][] = $userent['uid'];
486
					$group['member'][] = $userent['uid'];
485 487
					break;
486 488
				}
487 489
			}
490
			unset($group);
491
			config_set_path('system/group', $group_config);
488 492

  
489
			$a_user[] = $userent;
493
			config_set_path('system/user/', $userent);
490 494
		}
491 495

  
492 496
		/* Sort it alphabetically */
493
		usort($config['system']['user'], function($a, $b) {
497
		$user_config = config_get_path('system/user');
498
		usort($user_config, function($a, $b) {
494 499
			return strcmp($a['name'], $b['name']);
495 500
		});
501
		config_set_path('system/user', $user_config);
496 502

  
497 503
		local_user_set_groups($userent, $_POST['groups']);
498 504
		local_user_set($userent);
......
518 524
}
519 525

  
520 526
function build_priv_table() {
521
	global $a_user, $id, $read_only;
527
	global $id, $read_only;
522 528

  
523 529
	$privhtml = '<div class="table-responsive">';
524 530
	$privhtml .=	'<table class="table table-striped table-hover table-condensed">';
......
535 541
	$i = 0;
536 542
	$user_has_root_priv = false;
537 543

  
538
	foreach (get_user_privdesc($a_user[$id]) as $priv) {
544
	foreach (get_user_privdesc(config_get_path("system/user/{$id}")) as $priv) {
539 545
		$group = false;
540 546
		if ($priv['group']) {
541 547
			$group = $priv['group'];
......
588 594
}
589 595

  
590 596
function build_cert_table() {
591
	global $a_user, $id, $read_only;
597
	global $id, $read_only;
592 598

  
593 599
	$certhtml = '<div class="table-responsive">';
594 600
	$certhtml .=	'<table class="table table-striped table-hover table-condensed">';
......
601 607
	$certhtml .=		'</thead>';
602 608
	$certhtml .=		'<tbody>';
603 609

  
604
	$a_cert = $a_user[$id]['cert'];
605
	if (is_array($a_cert)) {
606
		$i = 0;
607
		foreach ($a_cert as $certref) {
608
			$cert = lookup_cert($certref);
609
			$ca = lookup_ca($cert['caref']);
610
			$revokedstr =	is_cert_revoked($cert) ? '<b> Revoked</b>':'';
611

  
612
			$certhtml .=	'<tr>';
613
			$certhtml .=		'<td>' . htmlspecialchars($cert['descr']) . $revokedstr . '</td>';
614
			$certhtml .=		'<td>' . htmlspecialchars($ca['descr']) . '</td>';
615
			$certhtml .=		'<td>';
616
			if (!$read_only) {
617
				$certhtml .=			'<a id="delcert' . $i .'" class="fa-solid fa-trash-can no-confirm icon-pointer" title="';
618
				$certhtml .=			gettext('Remove this certificate association? (Certificate will not be deleted)') . '"></a>';
619
			}
620
			$certhtml .=		'</td>';
621
			$certhtml .=	'</tr>';
622
			$i++;
610
	$i = 0;
611
	foreach (config_get_path("system/user/{$id}/cert", []) as $certref) {
612
		$cert = lookup_cert($certref);
613
		$ca = lookup_ca($cert['caref']);
614
		$revokedstr =	is_cert_revoked($cert) ? '<b> Revoked</b>':'';
615

  
616
		$certhtml .=	'<tr>';
617
		$certhtml .=		'<td>' . htmlspecialchars($cert['descr']) . $revokedstr . '</td>';
618
		$certhtml .=		'<td>' . htmlspecialchars($ca['descr']) . '</td>';
619
		$certhtml .=		'<td>';
620
		if (!$read_only) {
621
			$certhtml .=			'<a id="delcert' . $i .'" class="fa-solid fa-trash-can no-confirm icon-pointer" title="';
622
			$certhtml .=			gettext('Remove this certificate association? (Certificate will not be deleted)') . '"></a>';
623 623
		}
624

  
624
		$certhtml .=		'</td>';
625
		$certhtml .=	'</tr>';
626
		$i++;
625 627
	}
626 628

  
627 629
	$certhtml .=		'</tbody>';
......
687 689
				</thead>
688 690
				<tbody>
689 691
<?php
690
foreach ($a_user as $i => $userent):
692
foreach (config_get_path("system/user/{$id}", []) as $i => $userent):
691 693
	?>
692 694
					<tr>
693 695
						<td>
......
896 898

  
897 899
	foreach (config_get_path('system/group', []) as $Ggroup) {
898 900
		if ($Ggroup['name'] != "all") {
899
			if (($act == 'edit' || $input_errors) && $Ggroup['member'] && in_array($a_user[$id]['uid'], $Ggroup['member'])) {
901
			if (($act == 'edit' || $input_errors) && $Ggroup['member'] && in_array(config_get_path("system/user/{$id}/uid", []), $Ggroup['member'])) {
900 902
				$usersGroups[ $Ggroup['name'] ] = $Ggroup['name'];	// Add it to the user's list
901 903
			} else {
902 904
				$systemGroups[ $Ggroup['name'] ] = $Ggroup['name']; // Add it to the 'not a member of' list
......
987 989
	}
988 990

  
989 991
	// ==== Add user certificate for a new user
990
	if (is_array($config['ca']) && count($config['ca']) > 0) {
992
	if (count(config_get_path('ca', [])) > 0) {
991 993
		$section = new Form_Section('Create Certificate for User');
992 994
		$section->addClass('cert-options');
993 995

  

Also available in: Unified diff