|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | +namespace OCA\Settings\SetupChecks; |
| 10 | + |
| 11 | +use IPLib\Factory; |
| 12 | +use OC\Security\RemoteIpAddress; |
| 13 | +use OCP\IConfig; |
| 14 | +use OCP\IL10N; |
| 15 | +use OCP\SetupCheck\ISetupCheck; |
| 16 | +use OCP\SetupCheck\SetupResult; |
| 17 | + |
| 18 | +class AllowedAdminRanges implements ISetupCheck { |
| 19 | + public function __construct( |
| 20 | + private IConfig $config, |
| 21 | + private IL10N $l10n, |
| 22 | + ) { |
| 23 | + } |
| 24 | + |
| 25 | + public function getCategory(): string { |
| 26 | + return 'system'; |
| 27 | + } |
| 28 | + |
| 29 | + public function getName(): string { |
| 30 | + return $this->l10n->t('Allowed admin IP ranges'); |
| 31 | + } |
| 32 | + |
| 33 | + public function run(): SetupResult { |
| 34 | + $allowedAdminRanges = $this->config->getSystemValue(RemoteIpAddress::SETTING_NAME, false); |
| 35 | + if ($allowedAdminRanges === false) { |
| 36 | + return SetupResult::success($this->l10n->t('Admin IP filtering isn’t applied.')); |
| 37 | + } |
| 38 | + |
| 39 | + if (!is_array($allowedAdminRanges)) { |
| 40 | + return SetupResult::error( |
| 41 | + $this->l10n->t( |
| 42 | + 'Configuration key "%1$s" expects an array (%2$s found). Admin IP range validation will not be applied.', |
| 43 | + [RemoteIpAddress::SETTING_NAME, gettype($allowedAdminRanges)], |
| 44 | + ) |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + $invalidRanges = array_reduce($allowedAdminRanges, static function (array $carry, mixed $range) { |
| 49 | + if (!is_string($range) || Factory::parseRangeString($range) === null) { |
| 50 | + $carry[] = $range; |
| 51 | + } |
| 52 | + |
| 53 | + return $carry; |
| 54 | + }, []); |
| 55 | + if (count($invalidRanges) > 0) { |
| 56 | + return SetupResult::warning( |
| 57 | + $this->l10n->t( |
| 58 | + 'Configuration key "%1$s" contains invalid IP range(s): "%2$s"', |
| 59 | + [RemoteIpAddress::SETTING_NAME, implode('", "', $invalidRanges)], |
| 60 | + ), |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + return SetupResult::success($this->l10n->t('Admin IP filtering is correctly configured.')); |
| 65 | + } |
| 66 | +} |
0 commit comments