|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Settings\SetupChecks; |
| 11 | + |
| 12 | +use OC\Authentication\TwoFactorAuth\MandatoryTwoFactor; |
| 13 | +use OC\Authentication\TwoFactorAuth\ProviderLoader; |
| 14 | +use OC\Authentication\TwoFactorAuth\ProviderSet; |
| 15 | +use OCP\IL10N; |
| 16 | +use OCP\SetupCheck\ISetupCheck; |
| 17 | +use OCP\SetupCheck\SetupResult; |
| 18 | + |
| 19 | +class TwoFactorConfiguration implements ISetupCheck { |
| 20 | + public function __construct( |
| 21 | + private IL10N $l10n, |
| 22 | + private ProviderLoader $providerLoader, |
| 23 | + private MandatoryTwoFactor $mandatoryTwoFactor, |
| 24 | + ) { |
| 25 | + } |
| 26 | + |
| 27 | + public function getName(): string { |
| 28 | + return $this->l10n->t('Second factor configuration'); |
| 29 | + } |
| 30 | + |
| 31 | + public function getCategory(): string { |
| 32 | + return 'security'; |
| 33 | + } |
| 34 | + |
| 35 | + public function run(): SetupResult { |
| 36 | + $providers = $this->providerLoader->getProviders(); |
| 37 | + $providerSet = new ProviderSet($providers, false); |
| 38 | + $primaryProviders = $providerSet->getPrimaryProviders(); |
| 39 | + if (count($primaryProviders) === 0) { |
| 40 | + return SetupResult::warning($this->l10n->t('This instance has no second factor provider available.')); |
| 41 | + } |
| 42 | + |
| 43 | + $state = $this->mandatoryTwoFactor->getState(); |
| 44 | + |
| 45 | + if (!$state->isEnforced()) { |
| 46 | + return SetupResult::info( |
| 47 | + $this->l10n->t( |
| 48 | + 'Second factor providers are available but two-factor authentication is not enforced.' |
| 49 | + ) |
| 50 | + ); |
| 51 | + } else { |
| 52 | + return SetupResult::success( |
| 53 | + $this->l10n->t( |
| 54 | + 'Second factor providers are available and enforced: %s.', |
| 55 | + [ |
| 56 | + implode(', ', array_map( |
| 57 | + fn ($p) => '"' . $p->getDisplayName() . '"', |
| 58 | + $primaryProviders) |
| 59 | + ) |
| 60 | + ] |
| 61 | + ) |
| 62 | + ); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments