Skip to content

Commit

Permalink
disable web updater by default on instances with more than 100 users
Browse files Browse the repository at this point in the history
Signed-off-by: szaimen <szaimen@e.mail.de>
  • Loading branch information
szaimen committed Mar 14, 2022
1 parent a74248b commit f317e5f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
50 changes: 48 additions & 2 deletions apps/updatenotification/lib/Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
*/
namespace OCA\UpdateNotification\Controller;

use OC\User\Backend;
use OCP\User\Backend\ICountUsersBackend;
use OCA\UpdateNotification\ResetTokenBackgroundJob;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
Expand All @@ -38,6 +40,8 @@
use OCP\IRequest;
use OCP\Security\ISecureRandom;
use OCP\Util;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;

class AdminController extends Controller {
/** @var IJobList */
Expand All @@ -50,6 +54,10 @@ class AdminController extends Controller {
private $timeFactory;
/** @var IL10N */
private $l10n;
/** @var IUserManager */
private $userManager;
/** @var LoggerInterface */
private $logger;

/**
* @param string $appName
Expand All @@ -66,17 +74,24 @@ public function __construct($appName,
ISecureRandom $secureRandom,
IConfig $config,
ITimeFactory $timeFactory,
IL10N $l10n) {
IL10N $l10n,
IUserManager $userManager,
LoggerInterface $logger) {
parent::__construct($appName, $request);
$this->jobList = $jobList;
$this->secureRandom = $secureRandom;
$this->config = $config;
$this->timeFactory = $timeFactory;
$this->l10n = $l10n;
$this->userManager = $userManager;
$this->logger = $logger;
}

private function isUpdaterEnabled() {
return !$this->config->getSystemValue('upgrade.disable-web', false);
if ($this->config->getSystemValue('upgrade.disable-web', false) || $this->getUserCount() > 100) {
return false;
}
return true;
}

/**
Expand Down Expand Up @@ -107,4 +122,35 @@ public function createCredentials(): DataResponse {

return new DataResponse($newToken);
}

// Copied from https://github.com/nextcloud/server/blob/a06001e0851abc6073af678b742da3e1aa96eec9/lib/private/Support/Subscription/Registry.php#L187-L214
private function getUserCount(): int {
$userCount = 0;
$backends = $this->userManager->getBackends();
foreach ($backends as $backend) {
if ($backend->implementsActions(Backend::COUNT_USERS)) {
/** @var ICountUsersBackend $backend */
$backendUsers = $backend->countUsers();
if ($backendUsers !== false) {
$userCount += $backendUsers;
} else {
// TODO what if the user count can't be determined?
$this->logger->warning('Can not determine user count for ' . get_class($backend), ['app' => 'updatenotification']);
}
}
}

$disabledUsers = $this->config->getUsersForUserValue('core', 'enabled', 'false');
$disabledUsersCount = count($disabledUsers);
$userCount = $userCount - $disabledUsersCount;

if ($userCount < 0) {
$userCount = 0;

// this should never happen
$this->logger->warning("Total user count was negative (users: $userCount, disabled: $disabledUsersCount)", ['app' => 'updatenotification']);
}

return $userCount;
}
}
12 changes: 11 additions & 1 deletion apps/updatenotification/tests/Controller/AdminControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
use OCP\IRequest;
use OCP\Security\ISecureRandom;
use Test\TestCase;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;

class AdminControllerTest extends TestCase {
/** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
Expand All @@ -53,6 +55,10 @@ class AdminControllerTest extends TestCase {
private $timeFactory;
/** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
private $l10n;
/** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
private $userManager;
/** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
private $logger;

protected function setUp(): void {
parent::setUp();
Expand All @@ -63,6 +69,8 @@ protected function setUp(): void {
$this->config = $this->createMock(IConfig::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->l10n = $this->createMock(IL10N::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->logger = $this->createMock(LoggerInterface::class);

$this->adminController = new AdminController(
'updatenotification',
Expand All @@ -71,7 +79,9 @@ protected function setUp(): void {
$this->secureRandom,
$this->config,
$this->timeFactory,
$this->l10n
$this->l10n,
$this->userManager,
$this->logger
);
}

Expand Down

0 comments on commit f317e5f

Please sign in to comment.