Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add QuotaWarningsProvider to warn about account close to full quota
Signed-off-by: Côme Chilliet <[email protected]>
  • Loading branch information
come-nc committed Oct 3, 2023
commit d01ad74b0b22276149e7acdf34060a48617d3278
2 changes: 2 additions & 0 deletions apps/settings/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
'OCA\\Settings\\AccountWarnings\\AccountWarningsManager' => $baseDir . '/../lib/AccountWarnings/AccountWarningsManager.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 Down
2 changes: 2 additions & 0 deletions apps/settings/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class ComposerStaticInitSettings
'OCA\\Settings\\AccountWarnings\\AccountWarningsManager' => __DIR__ . '/..' . '/../lib/AccountWarnings/AccountWarningsManager.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 Down
57 changes: 57 additions & 0 deletions apps/settings/lib/AccountWarnings/QuotaWarning.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Côme Chilliet <[email protected]>
*
* @author Côme Chilliet <[email protected]>
*
* @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 QuotaWarning implements IAccountWarning {
public const THRESHOLDS = [
98 => IAccountWarning::SEVERITY_ERROR,
90 => IAccountWarning::SEVERITY_WARNING,
80 => IAccountWarning::SEVERITY_INFO,
];

public function __construct(
private IL10N $l10n,
private int $count,
private int $threshold,
) {
}

public function getText(): string {
return $this->l10n->n(
'%n account is using more than %d%% of their quota',
'%n accounts are using more than %d%% of their quota',
$this->count,
[$this->threshold]
);
}

public function getSeverity(): string {
return self::THRESHOLDS[$this->threshold];
}
}
75 changes: 75 additions & 0 deletions apps/settings/lib/AccountWarnings/QuotaWarningsProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Côme Chilliet <[email protected]>
*
* @author Côme Chilliet <[email protected]>
*
* @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\IAccountWarningsProvider;
use OCP\IL10N;
use OCP\IUserManager;
use OCP\IUser;
use OCP\Files\IRootFolder;

class QuotaWarningsProvider implements IAccountWarningsProvider {
public function __construct(
private IL10N $l10n,
private IUserManager $userManager,
private IRootFolder $rootFolder,
) {
}

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

public function getAccountWarnings(): array {
$users = [];
foreach (QuotaWarning::THRESHOLDS as $threshold) {
$users[$threshold] = 0;
}
$this->userManager->callForSeenUsers(function (IUser $user) use (&$users) {
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
$size = $userFolder->getSize();
$quota = \OCP\Util::computerFileSize($user->getQuota());
if ($quota === false) {
return;
}
$usage = 100 * ($size / $quota);
foreach (QuotaWarning::THRESHOLDS as $threshold => $level) {
if ($usage >= $threshold) {
$users[$threshold]++;
break;
}
}
});
$warnings = [];
foreach ($users as $threshold => $count) {
if ($count > 0) {
$warnings[] = new QuotaWarning($this->l10n, $count, $threshold);
}
}
return $warnings;
}
}
2 changes: 2 additions & 0 deletions apps/settings/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use OC\Authentication\Token\IProvider;
use OC\Server;
use OCA\Settings\AccountWarnings\OutdatedClientWarningsProvider;
use OCA\Settings\AccountWarnings\QuotaWarningsProvider;
use OCA\Settings\Hooks;
use OCA\Settings\Listener\AppPasswordCreatedActivityListener;
use OCA\Settings\Listener\GroupRemovedListener;
Expand Down Expand Up @@ -139,6 +140,7 @@ public function register(IRegistrationContext $context): void {
$context->registerUserMigrator(AccountMigrator::class);

$context->registerAccountWarningsProvider(OutdatedClientWarningsProvider::class);
$context->registerAccountWarningsProvider(QuotaWarningsProvider::class);
}

public function boot(IBootContext $context): void {
Expand Down