Skip to content

Commit f317e5f

Browse files
committed
disable web updater by default on instances with more than 100 users
Signed-off-by: szaimen <szaimen@e.mail.de>
1 parent a74248b commit f317e5f

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

apps/updatenotification/lib/Controller/AdminController.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
*/
2828
namespace OCA\UpdateNotification\Controller;
2929

30+
use OC\User\Backend;
31+
use OCP\User\Backend\ICountUsersBackend;
3032
use OCA\UpdateNotification\ResetTokenBackgroundJob;
3133
use OCP\AppFramework\Controller;
3234
use OCP\AppFramework\Http;
@@ -38,6 +40,8 @@
3840
use OCP\IRequest;
3941
use OCP\Security\ISecureRandom;
4042
use OCP\Util;
43+
use OCP\IUserManager;
44+
use Psr\Log\LoggerInterface;
4145

4246
class AdminController extends Controller {
4347
/** @var IJobList */
@@ -50,6 +54,10 @@ class AdminController extends Controller {
5054
private $timeFactory;
5155
/** @var IL10N */
5256
private $l10n;
57+
/** @var IUserManager */
58+
private $userManager;
59+
/** @var LoggerInterface */
60+
private $logger;
5361

5462
/**
5563
* @param string $appName
@@ -66,17 +74,24 @@ public function __construct($appName,
6674
ISecureRandom $secureRandom,
6775
IConfig $config,
6876
ITimeFactory $timeFactory,
69-
IL10N $l10n) {
77+
IL10N $l10n,
78+
IUserManager $userManager,
79+
LoggerInterface $logger) {
7080
parent::__construct($appName, $request);
7181
$this->jobList = $jobList;
7282
$this->secureRandom = $secureRandom;
7383
$this->config = $config;
7484
$this->timeFactory = $timeFactory;
7585
$this->l10n = $l10n;
86+
$this->userManager = $userManager;
87+
$this->logger = $logger;
7688
}
7789

7890
private function isUpdaterEnabled() {
79-
return !$this->config->getSystemValue('upgrade.disable-web', false);
91+
if ($this->config->getSystemValue('upgrade.disable-web', false) || $this->getUserCount() > 100) {
92+
return false;
93+
}
94+
return true;
8095
}
8196

8297
/**
@@ -107,4 +122,35 @@ public function createCredentials(): DataResponse {
107122

108123
return new DataResponse($newToken);
109124
}
125+
126+
// Copied from https://github.com/nextcloud/server/blob/a06001e0851abc6073af678b742da3e1aa96eec9/lib/private/Support/Subscription/Registry.php#L187-L214
127+
private function getUserCount(): int {
128+
$userCount = 0;
129+
$backends = $this->userManager->getBackends();
130+
foreach ($backends as $backend) {
131+
if ($backend->implementsActions(Backend::COUNT_USERS)) {
132+
/** @var ICountUsersBackend $backend */
133+
$backendUsers = $backend->countUsers();
134+
if ($backendUsers !== false) {
135+
$userCount += $backendUsers;
136+
} else {
137+
// TODO what if the user count can't be determined?
138+
$this->logger->warning('Can not determine user count for ' . get_class($backend), ['app' => 'updatenotification']);
139+
}
140+
}
141+
}
142+
143+
$disabledUsers = $this->config->getUsersForUserValue('core', 'enabled', 'false');
144+
$disabledUsersCount = count($disabledUsers);
145+
$userCount = $userCount - $disabledUsersCount;
146+
147+
if ($userCount < 0) {
148+
$userCount = 0;
149+
150+
// this should never happen
151+
$this->logger->warning("Total user count was negative (users: $userCount, disabled: $disabledUsersCount)", ['app' => 'updatenotification']);
152+
}
153+
154+
return $userCount;
155+
}
110156
}

apps/updatenotification/tests/Controller/AdminControllerTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
use OCP\IRequest;
3838
use OCP\Security\ISecureRandom;
3939
use Test\TestCase;
40+
use OCP\IUserManager;
41+
use Psr\Log\LoggerInterface;
4042

4143
class AdminControllerTest extends TestCase {
4244
/** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
@@ -53,6 +55,10 @@ class AdminControllerTest extends TestCase {
5355
private $timeFactory;
5456
/** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
5557
private $l10n;
58+
/** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
59+
private $userManager;
60+
/** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
61+
private $logger;
5662

5763
protected function setUp(): void {
5864
parent::setUp();
@@ -63,6 +69,8 @@ protected function setUp(): void {
6369
$this->config = $this->createMock(IConfig::class);
6470
$this->timeFactory = $this->createMock(ITimeFactory::class);
6571
$this->l10n = $this->createMock(IL10N::class);
72+
$this->userManager = $this->createMock(IUserManager::class);
73+
$this->logger = $this->createMock(LoggerInterface::class);
6674

6775
$this->adminController = new AdminController(
6876
'updatenotification',
@@ -71,7 +79,9 @@ protected function setUp(): void {
7179
$this->secureRandom,
7280
$this->config,
7381
$this->timeFactory,
74-
$this->l10n
82+
$this->l10n,
83+
$this->userManager,
84+
$this->logger
7585
);
7686
}
7787

0 commit comments

Comments
 (0)