Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add account warnings to admin settings #40744

Closed
wants to merge 11 commits into from
6 changes: 5 additions & 1 deletion apps/settings/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
['name' => 'AuthorizedGroup#destroy', 'url' => '/settings/authorizedgroups', 'verb' => 'DELETE'],

['name' => 'AuthSettings#wipe', 'url' => '/settings/personal/authtokens/wipe/{id}', 'verb' => 'POST' , 'root' => ''],
['name' => 'AccountWarnings#getAll', 'url' => '/settings/accountwarnings', 'verb' => 'GET' , 'root' => ''],

['name' => 'MailSettings#setMailSettings', 'url' => '/settings/admin/mailsettings', 'verb' => 'POST' , 'root' => ''],
['name' => 'MailSettings#storeCredentials', 'url' => '/settings/admin/mailsettings/credentials', 'verb' => 'POST' , 'root' => ''],
Expand Down Expand Up @@ -84,5 +85,8 @@
['name' => 'WebAuthn#deleteRegistration', 'url' => '/settings/api/personal/webauthn/registration/{id}', 'verb' => 'DELETE' , 'root' => ''],

['name' => 'Reasons#getPdf', 'url' => '/settings/download/reasons', 'verb' => 'GET', 'root' => ''],
]
],
'ocs' => [
['name' => 'ClientDiagnostics#update', 'url' => '/diagnostics', 'verb' => 'PUT' ],
],
];
11 changes: 11 additions & 0 deletions apps/settings/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

return array(
'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php',
'OCA\\Settings\\AccountWarnings\\AccountWarningsManager' => $baseDir . '/../lib/AccountWarnings/AccountWarningsManager.php',
'OCA\\Settings\\AccountWarnings\\ClientDiagnosticWarning' => $baseDir . '/../lib/AccountWarnings/ClientDiagnosticWarning.php',
'OCA\\Settings\\AccountWarnings\\ClientDiagnosticWarningsProvider' => $baseDir . '/../lib/AccountWarnings/ClientDiagnosticWarningsProvider.php',
'OCA\\Settings\\AccountWarnings\\OutdatedClientWarning' => $baseDir . '/../lib/AccountWarnings/OutdatedClientWarning.php',
'OCA\\Settings\\AccountWarnings\\OutdatedClientWarningsProvider' => $baseDir . '/../lib/AccountWarnings/OutdatedClientWarningsProvider.php',
'OCA\\Settings\\AccountWarnings\\QuotaWarning' => $baseDir . '/../lib/AccountWarnings/QuotaWarning.php',
'OCA\\Settings\\AccountWarnings\\QuotaWarningsProvider' => $baseDir . '/../lib/AccountWarnings/QuotaWarningsProvider.php',
'OCA\\Settings\\Activity\\GroupProvider' => $baseDir . '/../lib/Activity/GroupProvider.php',
'OCA\\Settings\\Activity\\GroupSetting' => $baseDir . '/../lib/Activity/GroupSetting.php',
'OCA\\Settings\\Activity\\Provider' => $baseDir . '/../lib/Activity/Provider.php',
Expand All @@ -17,12 +24,14 @@
'OCA\\Settings\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php',
'OCA\\Settings\\BackgroundJobs\\VerifyUserData' => $baseDir . '/../lib/BackgroundJobs/VerifyUserData.php',
'OCA\\Settings\\Controller\\AISettingsController' => $baseDir . '/../lib/Controller/AISettingsController.php',
'OCA\\Settings\\Controller\\AccountWarningsController' => $baseDir . '/../lib/Controller/AccountWarningsController.php',
'OCA\\Settings\\Controller\\AdminSettingsController' => $baseDir . '/../lib/Controller/AdminSettingsController.php',
'OCA\\Settings\\Controller\\AppSettingsController' => $baseDir . '/../lib/Controller/AppSettingsController.php',
'OCA\\Settings\\Controller\\AuthSettingsController' => $baseDir . '/../lib/Controller/AuthSettingsController.php',
'OCA\\Settings\\Controller\\AuthorizedGroupController' => $baseDir . '/../lib/Controller/AuthorizedGroupController.php',
'OCA\\Settings\\Controller\\ChangePasswordController' => $baseDir . '/../lib/Controller/ChangePasswordController.php',
'OCA\\Settings\\Controller\\CheckSetupController' => $baseDir . '/../lib/Controller/CheckSetupController.php',
'OCA\\Settings\\Controller\\ClientDiagnosticsController' => $baseDir . '/../lib/Controller/ClientDiagnosticsController.php',
'OCA\\Settings\\Controller\\CommonSettingsTrait' => $baseDir . '/../lib/Controller/CommonSettingsTrait.php',
'OCA\\Settings\\Controller\\HelpController' => $baseDir . '/../lib/Controller/HelpController.php',
'OCA\\Settings\\Controller\\LogSettingsController' => $baseDir . '/../lib/Controller/LogSettingsController.php',
Expand All @@ -32,6 +41,8 @@
'OCA\\Settings\\Controller\\TwoFactorSettingsController' => $baseDir . '/../lib/Controller/TwoFactorSettingsController.php',
'OCA\\Settings\\Controller\\UsersController' => $baseDir . '/../lib/Controller/UsersController.php',
'OCA\\Settings\\Controller\\WebAuthnController' => $baseDir . '/../lib/Controller/WebAuthnController.php',
'OCA\\Settings\\Db\\ClientDiagnostic' => $baseDir . '/../lib/Db/ClientDiagnostic.php',
'OCA\\Settings\\Db\\ClientDiagnosticMapper' => $baseDir . '/../lib/Db/ClientDiagnosticMapper.php',
'OCA\\Settings\\Events\\BeforeTemplateRenderedEvent' => $baseDir . '/../lib/Events/BeforeTemplateRenderedEvent.php',
'OCA\\Settings\\Hooks' => $baseDir . '/../lib/Hooks.php',
'OCA\\Settings\\Listener\\AppPasswordCreatedActivityListener' => $baseDir . '/../lib/Listener/AppPasswordCreatedActivityListener.php',
Expand Down
11 changes: 11 additions & 0 deletions apps/settings/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ class ComposerStaticInitSettings

public static $classMap = array (
'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php',
'OCA\\Settings\\AccountWarnings\\AccountWarningsManager' => __DIR__ . '/..' . '/../lib/AccountWarnings/AccountWarningsManager.php',
'OCA\\Settings\\AccountWarnings\\ClientDiagnosticWarning' => __DIR__ . '/..' . '/../lib/AccountWarnings/ClientDiagnosticWarning.php',
'OCA\\Settings\\AccountWarnings\\ClientDiagnosticWarningsProvider' => __DIR__ . '/..' . '/../lib/AccountWarnings/ClientDiagnosticWarningsProvider.php',
'OCA\\Settings\\AccountWarnings\\OutdatedClientWarning' => __DIR__ . '/..' . '/../lib/AccountWarnings/OutdatedClientWarning.php',
'OCA\\Settings\\AccountWarnings\\OutdatedClientWarningsProvider' => __DIR__ . '/..' . '/../lib/AccountWarnings/OutdatedClientWarningsProvider.php',
'OCA\\Settings\\AccountWarnings\\QuotaWarning' => __DIR__ . '/..' . '/../lib/AccountWarnings/QuotaWarning.php',
'OCA\\Settings\\AccountWarnings\\QuotaWarningsProvider' => __DIR__ . '/..' . '/../lib/AccountWarnings/QuotaWarningsProvider.php',
'OCA\\Settings\\Activity\\GroupProvider' => __DIR__ . '/..' . '/../lib/Activity/GroupProvider.php',
'OCA\\Settings\\Activity\\GroupSetting' => __DIR__ . '/..' . '/../lib/Activity/GroupSetting.php',
'OCA\\Settings\\Activity\\Provider' => __DIR__ . '/..' . '/../lib/Activity/Provider.php',
Expand All @@ -32,12 +39,14 @@ class ComposerStaticInitSettings
'OCA\\Settings\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php',
'OCA\\Settings\\BackgroundJobs\\VerifyUserData' => __DIR__ . '/..' . '/../lib/BackgroundJobs/VerifyUserData.php',
'OCA\\Settings\\Controller\\AISettingsController' => __DIR__ . '/..' . '/../lib/Controller/AISettingsController.php',
'OCA\\Settings\\Controller\\AccountWarningsController' => __DIR__ . '/..' . '/../lib/Controller/AccountWarningsController.php',
'OCA\\Settings\\Controller\\AdminSettingsController' => __DIR__ . '/..' . '/../lib/Controller/AdminSettingsController.php',
'OCA\\Settings\\Controller\\AppSettingsController' => __DIR__ . '/..' . '/../lib/Controller/AppSettingsController.php',
'OCA\\Settings\\Controller\\AuthSettingsController' => __DIR__ . '/..' . '/../lib/Controller/AuthSettingsController.php',
'OCA\\Settings\\Controller\\AuthorizedGroupController' => __DIR__ . '/..' . '/../lib/Controller/AuthorizedGroupController.php',
'OCA\\Settings\\Controller\\ChangePasswordController' => __DIR__ . '/..' . '/../lib/Controller/ChangePasswordController.php',
'OCA\\Settings\\Controller\\CheckSetupController' => __DIR__ . '/..' . '/../lib/Controller/CheckSetupController.php',
'OCA\\Settings\\Controller\\ClientDiagnosticsController' => __DIR__ . '/..' . '/../lib/Controller/ClientDiagnosticsController.php',
'OCA\\Settings\\Controller\\CommonSettingsTrait' => __DIR__ . '/..' . '/../lib/Controller/CommonSettingsTrait.php',
'OCA\\Settings\\Controller\\HelpController' => __DIR__ . '/..' . '/../lib/Controller/HelpController.php',
'OCA\\Settings\\Controller\\LogSettingsController' => __DIR__ . '/..' . '/../lib/Controller/LogSettingsController.php',
Expand All @@ -47,6 +56,8 @@ class ComposerStaticInitSettings
'OCA\\Settings\\Controller\\TwoFactorSettingsController' => __DIR__ . '/..' . '/../lib/Controller/TwoFactorSettingsController.php',
'OCA\\Settings\\Controller\\UsersController' => __DIR__ . '/..' . '/../lib/Controller/UsersController.php',
'OCA\\Settings\\Controller\\WebAuthnController' => __DIR__ . '/..' . '/../lib/Controller/WebAuthnController.php',
'OCA\\Settings\\Db\\ClientDiagnostic' => __DIR__ . '/..' . '/../lib/Db/ClientDiagnostic.php',
'OCA\\Settings\\Db\\ClientDiagnosticMapper' => __DIR__ . '/..' . '/../lib/Db/ClientDiagnosticMapper.php',
'OCA\\Settings\\Events\\BeforeTemplateRenderedEvent' => __DIR__ . '/..' . '/../lib/Events/BeforeTemplateRenderedEvent.php',
'OCA\\Settings\\Hooks' => __DIR__ . '/..' . '/../lib/Hooks.php',
'OCA\\Settings\\Listener\\AppPasswordCreatedActivityListener' => __DIR__ . '/..' . '/../lib/Listener/AppPasswordCreatedActivityListener.php',
Expand Down
58 changes: 58 additions & 0 deletions apps/settings/lib/AccountWarnings/AccountWarningsManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Côme Chilliet <come.chilliet@nextcloud.com>
*
* @author Côme Chilliet <come.chilliet@nextcloud.com>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCA\Settings\AccountWarnings;

use OCP\Settings\IAccountWarningsProvider;
use OC\AppFramework\Bootstrap\Coordinator;

class AccountWarningsManager {
public function __construct(
private Coordinator $coordinator,
) {
}

/**
* @return array<class-string, array{name:string,warnings:array<string,string>}>

Check failure

Code scanning / Psalm

InvalidReturnType

The declared return type 'array<class-string, array{name: string, warnings: array<string, string>}>' for OCA\Settings\AccountWarnings\AccountWarningsManager::getAll is incorrect, got 'array<class-string<OCP\Settings\IAccountWarningsProvider>, array{name: string, warnings: array<'error'|'info'|'warning', non-empty-list<string>>}>'
*/
public function getAll(): array {
$results = [];
$providerRegistrations = $this->coordinator->getRegistrationContext()->getAccountWarningsProviders();

Check notice

Code scanning / Psalm

PossiblyNullReference

Cannot call method getAccountWarningsProviders on possibly null value
foreach ($providerRegistrations as $providerRegistration) {
/** @var IAccountWarningsProvider $provider */
$provider = \OCP\Server::get($providerRegistration->getService());
$warnings = $provider->getAccountWarnings();
$results[$provider::class] = ['name' => $provider->getName(),'warnings' => []];
foreach ($warnings as $warning) {
$category = $warning->getSeverity();
if (!isset($results[$provider::class]['warnings'][$category])) {
$results[$provider::class]['warnings'][$category] = [];
}
$results[$provider::class]['warnings'][$category][] = $warning->getText();
}
}
return $results;

Check failure

Code scanning / Psalm

InvalidReturnStatement

The inferred type 'array<class-string<OCP\Settings\IAccountWarningsProvider>, array{name: string, warnings: array<'error'|'info'|'warning', non-empty-list<string>>}>' does not match the declared return type 'array<class-string, array{name: string, warnings: array<string, string>}>' for OCA\Settings\AccountWarnings\AccountWarningsManager::getAll
}
}
77 changes: 77 additions & 0 deletions apps/settings/lib/AccountWarnings/ClientDiagnosticWarning.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Côme Chilliet <come.chilliet@nextcloud.com>
*
* @author Côme Chilliet <come.chilliet@nextcloud.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Settings\AccountWarnings;

use OCA\Settings\Db\ClientDiagnostic;
use OCP\Settings\IAccountWarning;
use OCP\IL10N;

class ClientDiagnosticWarning implements IAccountWarning {
/**
* @param string $type one of ClientDiagnostic::TYPE_* constants
*/
public function __construct(
private IL10N $l10n,
private string $type,
private int $count,
private string $uid,
private string $clientName,
private int $oldest,
) {
}

public function getText(): string {
$oldest = new \DateTime();
$oldest->setTimestamp($this->oldest);
// TODO check which format to use
$formattedOldest = $oldest->format('Y-m-d H:i:s');
return match ($this->type) {
ClientDiagnostic::TYPE_CONFLICT =>
$this->l10n->n(
'Account "%s" had %n conflict on client %s on %s',
'Account "%s" had %n conflicts on client %s, oldest one on %s',
$this->count,
[$this->uid, $this->clientName, $formattedOldest]
),
ClientDiagnostic::TYPE_FAILED_UPLOAD =>
$this->l10n->n(
'Account %s had %n failed upload on client %s on %s',
'Account %s had %n failed uploads on client %s, oldest one on %s',
$this->count,
[$this->uid, $this->clientName, $formattedOldest]
),
default => 'Unknown problem',
};
}

public function getSeverity(): string {
return match ($this->type) {
ClientDiagnostic::TYPE_CONFLICT => IAccountWarning::SEVERITY_WARNING,
ClientDiagnostic::TYPE_FAILED_UPLOAD => IAccountWarning::SEVERITY_WARNING,
default => IAccountWarning::SEVERITY_ERROR,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Côme Chilliet <come.chilliet@nextcloud.com>
*
* @author Côme Chilliet <come.chilliet@nextcloud.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Settings\AccountWarnings;

use OCA\Settings\Db\ClientDiagnostic;
use OCA\Settings\Db\ClientDiagnosticMapper;
use OCP\Settings\IAccountWarningsProvider;
use OC\Authentication\Token\IToken;
use OC\Authentication\Token\IProvider;
use OCP\IL10N;
use OCP\IUserManager;

class ClientDiagnosticWarningsProvider implements IAccountWarningsProvider {
/**
* Maximum age for a client diagnostic to be considered still valid, in seconds
* Diagnostic timestamp will be compared to last check of the associated authtoken
*/
public const MAX_AGE = 24 * 60 * 60;

public function __construct(
private IL10N $l10n,
private IUserManager $userManager,
private ClientDiagnosticMapper $diagnosticMapper,
private IProvider $tokenProvider,
) {
}

public function getName(): string {
return $this->l10n->t('Client errors');
}

public function getAccountWarnings(): array {
$diagnostics = $this->diagnosticMapper->getAll();

$warnings = [];
foreach ($diagnostics as $diagnostic) {
$token = $this->getAuthtoken($diagnostic);
// if (!$this->isRecentEnough($diagnostic, $token)) {
// // TODO delete diagnostic?
// continue;
// }
$data = $diagnostic->getDiagnosticAsArray();
foreach ($data['problems'] as $type => $problemDetails) {
$warnings[] = new ClientDiagnosticWarning(
$this->l10n,
$type,
$problemDetails['count'],
$token->getUID(),
$token->getName(),
$problemDetails['oldest']
);
}
}
return $warnings;
}

private function getAuthtoken(ClientDiagnostic $diagnostic): IToken {
return $this->tokenProvider->getTokenById($diagnostic->getAuthtokenid());
}

private function isRecentEnough(ClientDiagnostic $diagnostic, IToken $token): bool {
return ($diagnostic->getTimestamp()->getTimestamp() >= $token->getLastCheck() + self::MAX_AGE);
}
}
46 changes: 46 additions & 0 deletions apps/settings/lib/AccountWarnings/OutdatedClientWarning.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Côme Chilliet <come.chilliet@nextcloud.com>
*
* @author Côme Chilliet <come.chilliet@nextcloud.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Settings\AccountWarnings;

use OCP\Settings\IAccountWarning;
use OCP\IL10N;

class OutdatedClientWarning implements IAccountWarning {
public function __construct(
private IL10N $l10n,
private int $count,
private string $version,
) {
}

public function getText(): string {
return $this->l10n->t('%d users are using outdated client version "%s"', [$this->count, $this->version]);
}

public function getSeverity(): string {
return IAccountWarning::SEVERITY_WARNING;
}
}
Loading