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/etc/inc/upgrade_config.inc
604 604
	$config['system']['webgui']['auth_method'] = "session";
605 605
	$config['system']['webgui']['backing_method'] = "htpasswd";
606 606

  
607
	if (isset($config['system']['username'])) {
608
		$config['system']['group'] = array();
609
		$config['system']['group'][0]['name'] = "admins";
610
		$config['system']['group'][0]['description'] = gettext("System Administrators");
611
		$config['system']['group'][0]['scope'] = "system";
612
		$config['system']['group'][0]['priv'] = "page-all";
613
		$config['system']['group'][0]['home'] = "index.php";
614
		$config['system']['group'][0]['gid'] = "110";
615

  
616
		$config['system']['user'] = array();
617
		$config['system']['user'][0]['name'] = "{$config['system']['username']}";
618
		$config['system']['user'][0]['descr'] = "System Administrator";
619
		$config['system']['user'][0]['scope'] = "system";
620
		$config['system']['user'][0]['groupname'] = "admins";
621
		$config['system']['user'][0]['password'] = "{$config['system']['password']}";
622
		$config['system']['user'][0]['uid'] = "0";
607
	$username_config = config_get_path('system/username', '');
608
	if (!empty($username_config)) {
623 609
		/* Ensure that we follow what this new "admin" username should be in the session. */
624
		$_SESSION["Username"] = "{$config['system']['username']}";
625

  
626
		$config['system']['user'][0]['priv'] = array();
627
		$config['system']['user'][0]['priv'][0]['id'] = "lockwc";
628
		$config['system']['user'][0]['priv'][0]['name'] = "Lock webConfigurator";
629
		$config['system']['user'][0]['priv'][0]['descr'] = gettext("Indicates whether this user will lock access to the webConfigurator for other users.");
630
		$config['system']['user'][0]['priv'][1]['id'] = "lock-ipages";
631
		$config['system']['user'][0]['priv'][1]['name'] = "Lock individual pages";
632
		$config['system']['user'][0]['priv'][1]['descr'] = gettext("Indicates whether this user will lock individual HTML pages after having accessed a particular page (the lock will be freed if the user leaves or saves the page form).");
633
		$config['system']['user'][0]['priv'][2]['id'] = "hasshell";
634
		$config['system']['user'][0]['priv'][2]['name'] = "Has shell access";
635
		$config['system']['user'][0]['priv'][2]['descr'] = gettext("Indicates whether this user is able to login for example via SSH.");
636
		$config['system']['user'][0]['priv'][3]['id'] = "copyfiles";
637
		$config['system']['user'][0]['priv'][3]['name'] = "Is allowed to copy files";
638
		$config['system']['user'][0]['priv'][3]['descr'] = sprintf(gettext("Indicates whether this user is allowed to copy files onto the %s appliance via SCP/SFTP."), g_get('product_label'));
639
		$config['system']['user'][0]['priv'][4]['id'] = "isroot";
640
		$config['system']['user'][0]['priv'][4]['name'] = "Is root user";
641
		$config['system']['user'][0]['priv'][4]['descr'] = gettext("This user is associated with the UNIX root user (this privilege should only be associated with one single user).");
642

  
643
		$config['system']['nextuid'] = "111";
644
		$config['system']['nextgid'] = "111";
610
		$_SESSION["Username"] = $username_config;
611

  
612
		$group_config = [[
613
			'name' => 'admins',
614
			'description' => gettext('System Administrators'),
615
			'scope' => 'system',
616
			'priv' => 'page-all',
617
			'home' => 'index.php',
618
			'gid' => '110'
619
		]];
620
		config_set_path('system/group', $group_config);
621

  
622
		$user_config = [[
623
			'name' => $username_config,
624
			'descr' => 'System Administrator',
625
			'scope' => 'system',
626
			'groupname' => 'admins',
627
			'password' => config_get_path('system/password', ''),
628
			'uid' => '0',
629
			'priv' => [
630
				[
631
					'id' => 'lockwc',
632
					'name' => 'Lock webConfigurator',
633
					'descr' => gettext('Indicates whether this user will lock access to the webConfigurator for other users.'),
634
				],
635
				[
636
					'id' => 'lock-ipages',
637
					'name' => 'Lock individual pages',
638
					'descr' => gettext('Indicates whether this user will lock individual HTML pages after having accessed a particular page (the lock will be freed if the user leaves or saves the page form).'),
639
				],
640
				[
641
					'id' => 'hasshell',
642
					'name' => 'Has shell access',
643
					'descr' => gettext('Indicates whether this user is able to login for example via SSH.'),
644
				],
645
				[
646
					'id' => 'copyfiles',
647
					'name' => 'Is allowed to copy files',
648
					'descr' => sprintf(gettext('Indicates whether this user is allowed to copy files onto the %s appliance via SCP/SFTP.'), g_get('product_label')),
649
				],
650
				[
651
					'id' => 'isroot',
652
					'name' => 'Is root user',
653
					'descr' => gettext('This user is associated with the UNIX root user (this privilege should only be associated with one single user).'),	
654
				]
655
			]
656
		]];
657
		config_set_path('system/user', $user_config);
658

  
659
		config_set_path('system/nextuid', '111');
660
		config_set_path('system/nextgid', '111');
645 661

  
646 662
		config_del_path('system/username');
647
		if (isset($config['system']['password'])) {
648
			config_del_path('system/password');
649
		}
663
		config_del_path('system/password');
650 664
	}
651 665
}
652 666

  
......
1380 1394

  
1381 1395

  
1382 1396
function upgrade_048_to_049() {
1383
	global $config;
1384 1397
	/* setup new all users group */
1385 1398
	$all = array();
1386 1399
	$all['name'] = "all";
......
1389 1402
	$all['gid'] = 1998;
1390 1403
	$all['member'] = array();
1391 1404

  
1392
	if (!is_array($config['system']['user'])) {
1393
		$config['system']['user'] = array();
1394
	}
1395
	if (!is_array($config['system']['group'])) {
1396
		$config['system']['group'] = array();
1397
	}
1405
	config_init_path('system/user');
1406
	$user_config = config_get_path('system/user');
1407
	config_init_path('system/group');
1408
	$group_config = config_get_path('system/group');
1398 1409

  
1399 1410
	/* work around broken uid assignments */
1400
	$config['system']['nextuid'] = 2000;
1401
	foreach ($config['system']['user'] as & $user) {
1411
	$nextuid = 2000;
1412
	foreach ($user_config  as & $user) {
1402 1413
		if (isset($user['uid']) && !$user['uid']) {
1403 1414
			continue;
1404 1415
		}
1405
		$user['uid'] = $config['system']['nextuid']++;
1416
		$user['uid'] = $nextuid++;
1406 1417
	}
1418
	config_set_path('system/nextuid', $nextuid);
1407 1419

  
1408 1420
	/* work around broken gid assignments */
1409
	$config['system']['nextgid'] = 2000;
1410
	foreach ($config['system']['group'] as & $group) {
1421
	$nextgid = 2000;
1422
	foreach ($group_config as & $group) {
1411 1423
		if ($group['name'] == g_get('admin_group')) {
1412 1424
			$group['gid'] = 1999;
1413 1425
		} else {
1414
			$group['gid'] = $config['system']['nextgid']++;
1426
			$group['gid'] = $nextgid++;
1415 1427
		}
1416 1428
	}
1429
	config_set_path('system/nextgid', $nextgid);
1417 1430

  
1418 1431
	/* build group membership information */
1419
	foreach ($config['system']['group'] as & $group) {
1432
	foreach ($group_config as & $group) {
1420 1433
		$group['member'] = array();
1421
		foreach ($config['system']['user'] as & $user) {
1434
		foreach ($user_config as & $user) {
1422 1435
			$groupnames = explode(",", $user['groupname']);
1423 1436
			if (in_array($group['name'], $groupnames)) {
1424 1437
				$group['member'][] = $user['uid'];
......
1427 1440
	}
1428 1441

  
1429 1442
	/* reset user group information */
1430
	foreach ($config['system']['user'] as & $user) {
1443
	foreach ($user_config as & $user) {
1431 1444
		unset($user['groupname']);
1432 1445
		$all['member'][] = $user['uid'];
1433 1446
	}
1434 1447

  
1435 1448
	/* reset group scope information */
1436
	foreach ($config['system']['group'] as & $group) {
1449
	foreach ($user_config as & $group) {
1437 1450
		if ($group['name'] != g_get('admin_group')) {
1438 1451
			$group['scope'] = "user";
1439 1452
		}
1440 1453
	}
1441 1454

  
1442 1455
	/* insert new all group */
1443
	$groups = Array();
1444
	$groups[] = $all;
1445
	$groups = array_merge($config['system']['group'], $groups);
1446
	$config['system']['group'] = $groups;
1456
	$group_config = array_merge($group_config, [[$all]]);
1457

  
1458
	config_set_path('system/user', $user_config);
1459
	config_set_path('system/group', $group_config);
1447 1460
}
1448 1461

  
1449 1462

  
1450 1463
function upgrade_049_to_050() {
1451
	global $config;
1452

  
1453
	if (!is_array($config['system']['user'])) {
1454
		$config['system']['user'] = array();
1455
	}
1464
	config_init_path('system/user');
1465
	$user_config = config_get_path('system/user');
1456 1466
	/* update user privileges */
1457
	foreach ($config['system']['user'] as & $user) {
1467
	foreach ($user_config as & $user) {
1458 1468
		$privs = array();
1459 1469
		if (!is_array($user['priv'])) {
1460 1470
			unset($user['priv']);
......
1473 1483
		$user['priv'] = $privs;
1474 1484
	}
1475 1485

  
1486
	config_init_path('system/group');
1487
	$group_config = config_get_path('system/group');
1476 1488
	/* update group privileges */
1477
	foreach ($config['system']['group'] as & $group) {
1489
	foreach ($group_config as & $group) {
1478 1490
		$privs = array();
1479 1491
		if (!is_array($group['pages'])) {
1480 1492
			unset($group['pages']);
......
1490 1502
		$group['priv'] = $privs;
1491 1503
	}
1492 1504

  
1505
	config_set_path('system/user', $user_config);
1506
	config_set_path('system/group', $group_config);
1507

  
1493 1508
	/* sync all local account information */
1494 1509
	local_reset_accounts();
1495 1510
}
......
1963 1978
	}
1964 1979

  
1965 1980
	/* migrate advanced admin page ssh keys to user manager */
1966
	if ($config['system']['ssh']['authorizedkeys']) {
1967
		$admin_user =& getUserEntryByUID(0);
1968
		$admin_user['authorizedkeys'] = config_get_path('system/ssh/authorizedkeys');
1981
	$authorizedkeys = config_get_path('system/ssh/authorizedkeys');
1982
	if (!empty($authorizedkeys)) {
1983
		$admin_user = getUserEntryByUID(0);
1984
		if (!empty($admin_user['idx'])) {
1985
			config_set_path("system/user/{$admin_user['idx']}/authorizedkeys", $authorizedkeys);
1986
		}
1969 1987
		config_del_path('system/ssh/authorizedkeys');
1970 1988
	}
1971 1989
}
......
2255 2273

  
2256 2274

  
2257 2275
function upgrade_056_to_057() {
2258
	global $config;
2276
	config_init_path('system/user');
2277
	$user_config = config_get_path('system/user');
2259 2278

  
2260
	if (!is_array($config['system']['user'])) {
2261
		$config['system']['user'] = array();
2262
	}
2263 2279
	/* migrate captivate portal to user manager */
2264
	if (is_array($config['captiveportal']['user'])) {
2265
		foreach ($config['captiveportal']['user'] as $user) {
2280
	if (is_array(config_get_path('captiveportal/user'))) {
2281
		$nextuid = config_get_path('system/nextuid');
2282
		foreach (config_get_path('captiveportal/user') as $user) {
2266 2283
			// avoid user conflicts
2267 2284
			$found = false;
2268
			foreach ($config['system']['user'] as $userent) {
2285
			foreach ($user_config as $userent) {
2269 2286
				if ($userent['name'] == $user['name']) {
2270 2287
					$found = true;
2271 2288
					break;
......
2283 2300
				$user['md5-hash'] = $user['password'];
2284 2301
				unset($user['password']);
2285 2302
			}
2286
			$user['uid'] = $config['system']['nextuid']++;
2287
			$config['system']['user'][] = $user;
2303
			$user['uid'] = $nextuid++;
2304
			$user_config[] = $user;
2288 2305
		}
2289 2306
		config_del_path('captiveportal/user');
2307
		config_set_path('system/nextuid', $nextuid);
2308
		config_set_path('system/user', $user_config);
2290 2309
	}
2291 2310
}
2292 2311

  
......
2575 2594
}
2576 2595

  
2577 2596
function upgrade_068_to_069() {
2578
	global $config;
2579
	if (!is_array($config['system']['user'])) {
2597
	$user_config = config_get_path('system/user');
2598
	if (!is_array($user_config)) {
2580 2599
		return;
2581 2600
	}
2582
	foreach ($config['system']['user'] as & $user) {
2601

  
2602
	$cert_config = config_get_path('cert');
2603
	foreach ($user_config as & $user) {
2583 2604
		if (!is_array($user['cert'])) {
2584 2605
			continue;
2585 2606
		}
......
2595 2616
			}
2596 2617
			// Keep the cert references for this user
2597 2618
			$rids[] = $tcert['refid'];
2598
			$config['cert'][] = $tcert;
2619
			$cert_config[] = $tcert;
2599 2620
		}
2600 2621
		// Replace user certs with cert references instead.
2601 2622
		if (count($rids) > 0) {
2602 2623
			$user['cert'] = $rids;
2603 2624
		}
2604 2625
	}
2626
	config_set_path('cert', $cert_config);
2627
	config_set_path('system/user', $user_config);
2605 2628
}
2606 2629

  
2607 2630
function upgrade_069_to_070() {
......
2682 2705
}
2683 2706

  
2684 2707
function upgrade_073_to_074() {
2685
	global $config;
2686
	rename_field($config['system']['user'], 'fullname', 'descr');
2708
	$user_config = config_get_path('system/user');
2709
	rename_field($user_config, 'fullname', 'descr');
2710
	config_set_path('system/user', $user_config);
2687 2711
}
2688 2712

  
2689 2713
function upgrade_074_to_075() {
......
3382 3406
}
3383 3407

  
3384 3408
function upgrade_103_to_104() {
3385
	global $config;
3386

  
3387 3409
	$changed_privs = array(
3388 3410
		"page-diag-system-activity" => "page-diagnostics-system-activity",
3389 3411
		"page-interfacess-groups" => "page-interfaces-groups",
......
3391 3413
		"page-interfacess-qinq" => "page-interfaces-qinq"
3392 3414
	);
3393 3415

  
3416
	$user_config = config_get_path('system/user', []);
3417
	$group_config = config_get_path('system/group', []);
3418

  
3394 3419
	/* update user privileges */
3395
	foreach ($config['system']['user'] as & $user) {
3420
	foreach ($user_config as & $user) {
3396 3421
		if (!is_array($user['priv'])) {
3397 3422
			continue;
3398 3423
		}
......
3404 3429
	}
3405 3430

  
3406 3431
	/* update group privileges */
3407
	foreach ($config['system']['group'] as & $group) {
3432
	foreach ($group_config as & $group) {
3408 3433
		if (!is_array($group['priv'])) {
3409 3434
			continue;
3410 3435
		}
......
3415 3440
		}
3416 3441
	}
3417 3442

  
3443

  
3444
	config_set_path('system/user', $user_config);
3445
	config_set_path('system/group', $group_config);
3446

  
3418 3447
	/* sync all local account information */
3419 3448
	local_reset_accounts();
3420 3449
}
......
3820 3849
}
3821 3850

  
3822 3851
function upgrade_121_to_122() {
3823
	global $config;
3824
	foreach ($config['system']['user'] as &$user) {
3852
	$user_config = config_get_path('system/user', []);
3853
	foreach ($user_config as &$user) {
3825 3854
		if (isset($user['nt-hash'])) {
3826 3855
			unset($user['nt-hash']);
3827 3856
		}
3828 3857
	}
3858
	config_set_path('system/user', $user_config);
3829 3859
}
3830 3860

  
3831 3861
function upgrade_122_to_123() {
......
4701 4731
}
4702 4732

  
4703 4733
function upgrade_147_to_148() {
4704
	global $config;
4734
	$group_config = config_get_path('system/group');
4705 4735

  
4706 4736
	// Ensure there are no spaces in group names by
4707 4737
	// replacing spaces with underscores
4708
	if (is_array($config['system']['group'])) {
4738
	if (is_array($group_config)) {
4709 4739
		$cleargroups = false;
4710
		foreach ($config['system']['group'] as $idx => $grp) {
4740
		foreach ($group_config as &$grp) {
4711 4741
			if (strstr($grp['name'], " ")) {
4712 4742
				$cleargroups = true;
4713
				$config['system']['group'][$idx]['scope'] = "remote";
4743
				$grp['scope'] = "remote";
4714 4744
			}
4715 4745
		}
4746
		config_set_path('system/group', $group_config);
4716 4747

  
4717 4748
		// if there was a space in a group name, there may be multiple
4718 4749
		// groups with the same name in the group file. To prevent pw
......
4720 4751
		// groups here. local_reset_accounts will run shortly after this
4721 4752
		// and add them back. redmine #6012
4722 4753
		if ($cleargroups) {
4723
			foreach ($config['system']['group'] as $grp) {
4754
			foreach ($group_config as $grp) {
4724 4755
				mwexec("/usr/sbin/pw groupdel -g {$grp['gid']}");
4725 4756
			}
4726 4757
		}
......
5156 5187
 * of a widget on the dashboard. Migrate any old settings.
5157 5188
 */
5158 5189
function convert_widget_164($oldname, $newname, $settings_keys) {
5159
	global $config;
5190
	$user_config = config_get_path('system/user');
5160 5191

  
5161 5192
	if ($newname == '') {
5162 5193
		$newname = $oldname . '-0';
......
5179 5210
			}
5180 5211

  
5181 5212
			// Modify any user-specific entries
5182
			foreach ($config['system']['user'] as & $user) {
5213
			foreach ($user_config as & $user) {
5183 5214
				if (isset($user['widgets'][$oldkey])) {
5184 5215
					$user['widgets'][$newname][$newkey] = $user['widgets'][$oldkey];
5185 5216
					unset($user['widgets'][$oldkey]);
......
5202 5233
			}
5203 5234

  
5204 5235
			// Modify any user-specific entries
5205
			foreach ($config['system']['user'] as & $user) {
5236
			foreach ($user_config as & $user) {
5206 5237
				if (isset($user['widgets'][$oldname][$oldkey])) {
5207 5238
					$user['widgets'][$newname][$newkey] = $user['widgets'][$oldname][$oldkey];
5208 5239
					unset($user['widgets'][$oldname][$oldkey]);
......
5218 5249
			config_del_path("widgets/{$oldname}");
5219 5250
		}
5220 5251
	}
5252
	config_set_path('system/user', $user_config);
5221 5253
}
5222 5254

  
5223 5255
function upgrade_163_to_164() {
......
6611 6643
}
6612 6644

  
6613 6645
function upgrade_222_to_223() {
6614
	global $config;
6646
	$user_config = config_get_path('system/user');
6615 6647

  
6616
	foreach ($config['system']['user'] as & $user) {
6648
	foreach ($user_config as & $user) {
6617 6649
		if ($user['name'] == 'admin') {
6618 6650
			$user_home = "/root";
6619 6651
		} else {
......
6625 6657
			@unlink($fn);
6626 6658
		}
6627 6659
	}
6660
	config_set_path('system/user', $user_config);
6628 6661
}
6629 6662

  
6630 6663
function upgrade_223_to_224() {

Also available in: Unified diff