-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
88d6c8e
Add ClientDiagnostic mapper and controller
come-nc 86fd999
Add migration to create DB table for client diagnostics
come-nc 4687603
json_encode client diagnostic
come-nc e67969e
Add OCP API to register account warnings providers
come-nc f398b31
Add a provider of account warnings for outdated clients
come-nc a24a3d8
Implement account provider registration in RegistrationContext
come-nc b0528d3
Add route to get account warnings from server
come-nc d01ad74
Add QuotaWarningsProvider to warn about account close to full quota
come-nc 2f96c57
Add user id information to quota warning
come-nc 3298503
Add ClientDiagnosticWarningsProvider and fix diagnostic PUT route
come-nc 0cde8b9
Move the route for pushing diagnostics to ocs endpoint
come-nc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
apps/settings/lib/AccountWarnings/AccountWarningsManager.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>}> | ||
*/ | ||
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
77
apps/settings/lib/AccountWarnings/ClientDiagnosticWarning.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
apps/settings/lib/AccountWarnings/ClientDiagnosticWarningsProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
46
apps/settings/lib/AccountWarnings/OutdatedClientWarning.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / Psalm
InvalidReturnType