AllowedAdminRanges.php 1.7 KB

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