Skip to content

Commit 4deff37

Browse files
Merge pull request #23278 from nextcloud/enh/noid/user-limits
Allow subscription to indicate that a userlimit is reached
2 parents dc3e05b + c0a05c0 commit 4deff37

File tree

11 files changed

+259
-12
lines changed

11 files changed

+259
-12
lines changed

build/psalm-baseline.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5098,8 +5098,9 @@
50985098
</UndefinedInterfaceMethod>
50995099
</file>
51005100
<file src="lib/private/Support/Subscription/Registry.php">
5101-
<UndefinedInterfaceMethod occurrences="1">
5101+
<UndefinedInterfaceMethod occurrences="2">
51025102
<code>getSupportedApps</code>
5103+
<code>countUsers</code>
51035104
</UndefinedInterfaceMethod>
51045105
</file>
51055106
<file src="lib/private/SystemTag/SystemTagManager.php">

core/Application.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
use OC\Authentication\Listeners\UserDeletedStoreCleanupListener;
4040
use OC\Authentication\Listeners\UserDeletedTokenCleanupListener;
4141
use OC\Authentication\Notifications\Notifier as AuthenticationNotifier;
42-
use OC\Core\Notification\RemoveLinkSharesNotifier;
42+
use OC\Core\Notification\CoreNotifier;
4343
use OC\DB\MissingColumnInformation;
4444
use OC\DB\MissingIndexInformation;
4545
use OC\DB\MissingPrimaryKeyInformation;
@@ -71,7 +71,7 @@ public function __construct() {
7171
$eventDispatcher = $server->query(IEventDispatcher::class);
7272

7373
$notificationManager = $server->getNotificationManager();
74-
$notificationManager->registerNotifierService(RemoveLinkSharesNotifier::class);
74+
$notificationManager->registerNotifierService(CoreNotifier::class);
7575
$notificationManager->registerNotifierService(AuthenticationNotifier::class);
7676

7777
$oldEventDispatcher = $server->getEventDispatcher();

core/Notification/RemoveLinkSharesNotifier.php renamed to core/Notification/CoreNotifier.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*
88
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
99
* @author Joas Schilling <coding@schilljs.com>
10+
* @author Morris Jobke <hey@morrisjobke.de>
1011
* @author Roeland Jago Douma <roeland@famdouma.nl>
1112
*
1213
* @license GNU AGPL version 3 or any later version
@@ -32,7 +33,7 @@
3233
use OCP\Notification\INotification;
3334
use OCP\Notification\INotifier;
3435

35-
class RemoveLinkSharesNotifier implements INotifier {
36+
class CoreNotifier implements INotifier {
3637
/** @var IFactory */
3738
private $l10nFactory;
3839

@@ -73,6 +74,13 @@ public function prepare(INotification $notification, string $languageCode): INot
7374
return $notification;
7475
}
7576

77+
if ($notification->getSubject() === 'user_limit_reached') {
78+
$notification->setParsedSubject($l->t('The user limit of this instance is reached.'));
79+
$notification->setParsedMessage($l->t('Add a subscription key to increase the user limit of this instance. For more information have a look at the Enterprise subscription page.'));
80+
$notification->setLink('https://nextcloud.com/enterprise/order/');
81+
return $notification;
82+
}
83+
7684
throw new \InvalidArgumentException('Invalid subject');
7785
}
7886
}

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@
927927
'OC\\Core\\Migrations\\Version20000Date20201109081918' => $baseDir . '/core/Migrations/Version20000Date20201109081918.php',
928928
'OC\\Core\\Migrations\\Version20000Date20201109081919' => $baseDir . '/core/Migrations/Version20000Date20201109081919.php',
929929
'OC\\Core\\Migrations\\Version20000Date20201111081915' => $baseDir . '/core/Migrations/Version20000Date20201111081915.php',
930-
'OC\\Core\\Notification\\RemoveLinkSharesNotifier' => $baseDir . '/core/Notification/RemoveLinkSharesNotifier.php',
930+
'OC\\Core\\Notification\\CoreNotifier' => $baseDir . '/core/Notification/CoreNotifier.php',
931931
'OC\\Core\\Service\\LoginFlowV2Service' => $baseDir . '/core/Service/LoginFlowV2Service.php',
932932
'OC\\DB\\Adapter' => $baseDir . '/lib/private/DB/Adapter.php',
933933
'OC\\DB\\AdapterMySQL' => $baseDir . '/lib/private/DB/AdapterMySQL.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
956956
'OC\\Core\\Migrations\\Version20000Date20201109081918' => __DIR__ . '/../../..' . '/core/Migrations/Version20000Date20201109081918.php',
957957
'OC\\Core\\Migrations\\Version20000Date20201109081919' => __DIR__ . '/../../..' . '/core/Migrations/Version20000Date20201109081919.php',
958958
'OC\\Core\\Migrations\\Version20000Date20201111081915' => __DIR__ . '/../../..' . '/core/Migrations/Version20000Date20201111081915.php',
959-
'OC\\Core\\Notification\\RemoveLinkSharesNotifier' => __DIR__ . '/../../..' . '/core/Notification/RemoveLinkSharesNotifier.php',
959+
'OC\\Core\\Notification\\CoreNotifier' => __DIR__ . '/../../..' . '/core/Notification/CoreNotifier.php',
960960
'OC\\Core\\Service\\LoginFlowV2Service' => __DIR__ . '/../../..' . '/core/Service/LoginFlowV2Service.php',
961961
'OC\\DB\\Adapter' => __DIR__ . '/../../..' . '/lib/private/DB/Adapter.php',
962962
'OC\\DB\\AdapterMySQL' => __DIR__ . '/../../..' . '/lib/private/DB/AdapterMySQL.php',

lib/private/Support/Subscription/Registry.php

Lines changed: 103 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,18 @@
2828

2929
namespace OC\Support\Subscription;
3030

31+
use OC\User\Backend;
3132
use OCP\AppFramework\QueryException;
3233
use OCP\IConfig;
34+
use OCP\IGroupManager;
3335
use OCP\IServerContainer;
36+
use OCP\IUserManager;
37+
use OCP\Notification\IManager;
3438
use OCP\Support\Subscription\Exception\AlreadyRegisteredException;
3539
use OCP\Support\Subscription\IRegistry;
3640
use OCP\Support\Subscription\ISubscription;
3741
use OCP\Support\Subscription\ISupportedApps;
42+
use Psr\Log\LoggerInterface;
3843

3944
class Registry implements IRegistry {
4045

@@ -49,10 +54,27 @@ class Registry implements IRegistry {
4954

5055
/** @var IServerContainer */
5156
private $container;
52-
53-
public function __construct(IConfig $config, IServerContainer $container) {
57+
/** @var IUserManager */
58+
private $userManager;
59+
/** @var IGroupManager */
60+
private $groupManager;
61+
/** @var LoggerInterface */
62+
private $logger;
63+
/** @var IManager */
64+
private $notificationManager;
65+
66+
public function __construct(IConfig $config,
67+
IServerContainer $container,
68+
IUserManager $userManager,
69+
IGroupManager $groupManager,
70+
LoggerInterface $logger,
71+
IManager $notificationManager) {
5472
$this->config = $config;
5573
$this->container = $container;
74+
$this->userManager = $userManager;
75+
$this->groupManager = $groupManager;
76+
$this->logger = $logger;
77+
$this->notificationManager = $notificationManager;
5678
}
5779

5880
private function getSubscription(): ?ISubscription {
@@ -127,9 +149,87 @@ public function delegateHasValidSubscription(): bool {
127149
* @since 17.0.0
128150
*/
129151
public function delegateHasExtendedSupport(): bool {
130-
if ($this->getSubscription() instanceof ISubscription && method_exists($this->subscription, 'hasExtendedSupport')) {
152+
if ($this->getSubscription() instanceof ISubscription) {
131153
return $this->getSubscription()->hasExtendedSupport();
132154
}
133155
return false;
134156
}
157+
158+
159+
/**
160+
* Indicates if a hard user limit is reached and no new users should be created
161+
*
162+
* @since 21.0.0
163+
*/
164+
public function delegateIsHardUserLimitReached(): bool {
165+
$subscription = $this->getSubscription();
166+
if ($subscription instanceof ISubscription &&
167+
$subscription->hasValidSubscription()) {
168+
$userLimitReached = $subscription->isHardUserLimitReached();
169+
if ($userLimitReached) {
170+
$this->notifyAboutReachedUserLimit();
171+
}
172+
return $userLimitReached;
173+
}
174+
175+
$isOneClickInstance = $this->config->getSystemValueBool('one-click-instance', false);
176+
177+
if (!$isOneClickInstance) {
178+
return false;
179+
}
180+
181+
$userCount = $this->getUserCount();
182+
$hardUserLimit = $this->config->getSystemValue('one-click-instance.user-limit', 50);
183+
184+
$userLimitReached = $userCount >= $hardUserLimit;
185+
if ($userLimitReached) {
186+
$this->notifyAboutReachedUserLimit();
187+
}
188+
return $userLimitReached;
189+
}
190+
191+
private function getUserCount(): int {
192+
$userCount = 0;
193+
$backends = $this->userManager->getBackends();
194+
foreach ($backends as $backend) {
195+
if ($backend->implementsActions(Backend::COUNT_USERS)) {
196+
$backendUsers = $backend->countUsers();
197+
if ($backendUsers !== false) {
198+
$userCount += $backendUsers;
199+
} else {
200+
// TODO what if the user count can't be determined?
201+
$this->logger->warning('Can not determine user count for ' . get_class($backend), ['app' => 'lib']);
202+
}
203+
}
204+
}
205+
206+
$disabledUsers = $this->config->getUsersForUserValue('core', 'enabled', 'false');
207+
$disabledUsersCount = count($disabledUsers);
208+
$userCount = $userCount - $disabledUsersCount;
209+
210+
if ($userCount < 0) {
211+
$userCount = 0;
212+
213+
// this should never happen
214+
$this->logger->warning("Total user count was negative (users: $userCount, disabled: $disabledUsersCount)", ['app' => 'lib']);
215+
}
216+
217+
return $userCount;
218+
}
219+
220+
private function notifyAboutReachedUserLimit() {
221+
$admins = $this->groupManager->get('admin')->getUsers();
222+
foreach ($admins as $admin) {
223+
$notification = $this->notificationManager->createNotification();
224+
225+
$notification->setApp('core')
226+
->setUser($admin->getUID())
227+
->setDateTime(new \DateTime())
228+
->setObject('user_limit_reached', '1')
229+
->setSubject('user_limit_reached');
230+
$this->notificationManager->notify($notification);
231+
}
232+
233+
$this->logger->warning('The user limit was reached and the new user was not created', ['app' => 'lib']);
234+
}
135235
}

lib/private/User/Manager.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
namespace OC\User;
3636

37+
use OC\HintException;
3738
use OC\Hooks\PublicEmitter;
3839
use OCP\DB\QueryBuilder\IQueryBuilder;
3940
use OCP\EventDispatcher\IEventDispatcher;
@@ -42,6 +43,7 @@
4243
use OCP\IUser;
4344
use OCP\IUserBackend;
4445
use OCP\IUserManager;
46+
use OCP\Support\Subscription\IRegistry;
4547
use OCP\User\Backend\IGetRealUIDBackend;
4648
use OCP\User\Events\BeforeUserCreatedEvent;
4749
use OCP\User\Events\UserCreatedEvent;
@@ -297,6 +299,12 @@ public function searchDisplayName($pattern, $limit = null, $offset = null) {
297299
* @return bool|IUser the created user or false
298300
*/
299301
public function createUser($uid, $password) {
302+
// DI injection is not used here as IRegistry needs the user manager itself for user count and thus it would create a cyclic dependency
303+
if (\OC::$server->get(IRegistry::class)->delegateIsHardUserLimitReached()) {
304+
$l = \OC::$server->getL10N('lib');
305+
throw new HintException($l->t('The user limit has been reached and the user was not created.'));
306+
}
307+
300308
$localBackends = [];
301309
foreach ($this->backends as $backend) {
302310
if ($backend instanceof Database) {

lib/public/Support/Subscription/IRegistry.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,11 @@ public function delegateHasValidSubscription(): bool;
7878
* @since 17.0.0
7979
*/
8080
public function delegateHasExtendedSupport(): bool;
81+
82+
/**
83+
* Indicates if a hard user limit is reached and no new users should be created
84+
*
85+
* @since 21.0.0
86+
*/
87+
public function delegateIsHardUserLimitReached(): bool;
8188
}

lib/public/Support/Subscription/ISubscription.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,11 @@ public function hasValidSubscription(): bool;
4545
* @since 17.0.0
4646
*/
4747
public function hasExtendedSupport(): bool;
48+
49+
/**
50+
* Indicates if a hard user limit is reached and no new users should be created
51+
*
52+
* @since 21.0.0
53+
*/
54+
public function isHardUserLimitReached(): bool;
4855
}

tests/lib/Support/Subscription/DummySubscription.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,19 @@ class DummySubscription implements \OCP\Support\Subscription\ISubscription {
3030
private $hasValidSubscription;
3131
/** @var bool */
3232
private $hasExtendedSupport;
33+
/** @var bool */
34+
private $isHardUserLimitReached;
3335

3436
/**
3537
* DummySubscription constructor.
3638
*
3739
* @param bool $hasValidSubscription
3840
* @param bool $hasExtendedSupport
3941
*/
40-
public function __construct(bool $hasValidSubscription, bool $hasExtendedSupport) {
42+
public function __construct(bool $hasValidSubscription, bool $hasExtendedSupport, bool $isHardUserLimitReached) {
4143
$this->hasValidSubscription = $hasValidSubscription;
4244
$this->hasExtendedSupport = $hasExtendedSupport;
45+
$this->isHardUserLimitReached = $isHardUserLimitReached;
4346
}
4447

4548
/**
@@ -55,4 +58,8 @@ public function hasValidSubscription(): bool {
5558
public function hasExtendedSupport(): bool {
5659
return $this->hasExtendedSupport;
5760
}
61+
62+
public function isHardUserLimitReached(): bool {
63+
return $this->isHardUserLimitReached;
64+
}
5865
}

0 commit comments

Comments
 (0)