Skip to content
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

[stable20] Allow subscription to indicate that a userlimit is reached #24511

Merged
merged 2 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion build/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5608,8 +5608,9 @@
</UndefinedInterfaceMethod>
</file>
<file src="lib/private/Support/Subscription/Registry.php">
<UndefinedInterfaceMethod occurrences="1">
<UndefinedInterfaceMethod occurrences="2">
<code>getSupportedApps</code>
<code>countUsers</code>
</UndefinedInterfaceMethod>
</file>
<file src="lib/private/SystemTag/SystemTagManager.php">
Expand Down
4 changes: 2 additions & 2 deletions core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
use OC\Authentication\Listeners\UserDeletedStoreCleanupListener;
use OC\Authentication\Listeners\UserDeletedTokenCleanupListener;
use OC\Authentication\Notifications\Notifier as AuthenticationNotifier;
use OC\Core\Notification\RemoveLinkSharesNotifier;
use OC\Core\Notification\CoreNotifier;
use OC\DB\MissingColumnInformation;
use OC\DB\MissingIndexInformation;
use OC\DB\MissingPrimaryKeyInformation;
Expand Down Expand Up @@ -71,7 +71,7 @@ public function __construct() {
$eventDispatcher = $server->query(IEventDispatcher::class);

$notificationManager = $server->getNotificationManager();
$notificationManager->registerNotifierService(RemoveLinkSharesNotifier::class);
$notificationManager->registerNotifierService(CoreNotifier::class);
$notificationManager->registerNotifierService(AuthenticationNotifier::class);

$oldEventDispatcher = $server->getEventDispatcher();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*
* @author Christoph Wurst <[email protected]>
* @author Joas Schilling <[email protected]>
* @author Morris Jobke <[email protected]>
* @author Roeland Jago Douma <[email protected]>
*
* @license GNU AGPL version 3 or any later version
Expand All @@ -32,7 +33,7 @@
use OCP\Notification\INotification;
use OCP\Notification\INotifier;

class RemoveLinkSharesNotifier implements INotifier {
class CoreNotifier implements INotifier {
/** @var IFactory */
private $l10nFactory;

Expand Down Expand Up @@ -73,6 +74,13 @@ public function prepare(INotification $notification, string $languageCode): INot
return $notification;
}

if ($notification->getSubject() === 'user_limit_reached') {
$notification->setParsedSubject($l->t('The user limit of this instance is reached.'));
$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.'));
$notification->setLink('https://nextcloud.com/enterprise/order/');
return $notification;
}

throw new \InvalidArgumentException('Invalid subject');
}
}
2 changes: 1 addition & 1 deletion lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@
'OC\\Core\\Migrations\\Version20000Date20201109081918' => $baseDir . '/core/Migrations/Version20000Date20201109081918.php',
'OC\\Core\\Migrations\\Version20000Date20201109081919' => $baseDir . '/core/Migrations/Version20000Date20201109081919.php',
'OC\\Core\\Migrations\\Version20000Date20201111081915' => $baseDir . '/core/Migrations/Version20000Date20201111081915.php',
'OC\\Core\\Notification\\RemoveLinkSharesNotifier' => $baseDir . '/core/Notification/RemoveLinkSharesNotifier.php',
'OC\\Core\\Notification\\CoreNotifier' => $baseDir . '/core/Notification/CoreNotifier.php',
'OC\\Core\\Service\\LoginFlowV2Service' => $baseDir . '/core/Service/LoginFlowV2Service.php',
'OC\\DB\\Adapter' => $baseDir . '/lib/private/DB/Adapter.php',
'OC\\DB\\AdapterMySQL' => $baseDir . '/lib/private/DB/AdapterMySQL.php',
Expand Down
2 changes: 1 addition & 1 deletion lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Core\\Migrations\\Version20000Date20201109081918' => __DIR__ . '/../../..' . '/core/Migrations/Version20000Date20201109081918.php',
'OC\\Core\\Migrations\\Version20000Date20201109081919' => __DIR__ . '/../../..' . '/core/Migrations/Version20000Date20201109081919.php',
'OC\\Core\\Migrations\\Version20000Date20201111081915' => __DIR__ . '/../../..' . '/core/Migrations/Version20000Date20201111081915.php',
'OC\\Core\\Notification\\RemoveLinkSharesNotifier' => __DIR__ . '/../../..' . '/core/Notification/RemoveLinkSharesNotifier.php',
'OC\\Core\\Notification\\CoreNotifier' => __DIR__ . '/../../..' . '/core/Notification/CoreNotifier.php',
'OC\\Core\\Service\\LoginFlowV2Service' => __DIR__ . '/../../..' . '/core/Service/LoginFlowV2Service.php',
'OC\\DB\\Adapter' => __DIR__ . '/../../..' . '/lib/private/DB/Adapter.php',
'OC\\DB\\AdapterMySQL' => __DIR__ . '/../../..' . '/lib/private/DB/AdapterMySQL.php',
Expand Down
106 changes: 103 additions & 3 deletions lib/private/Support/Subscription/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,18 @@

namespace OC\Support\Subscription;

use OC\User\Backend;
use OCP\AppFramework\QueryException;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IServerContainer;
use OCP\IUserManager;
use OCP\Notification\IManager;
use OCP\Support\Subscription\Exception\AlreadyRegisteredException;
use OCP\Support\Subscription\IRegistry;
use OCP\Support\Subscription\ISubscription;
use OCP\Support\Subscription\ISupportedApps;
use Psr\Log\LoggerInterface;

class Registry implements IRegistry {

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

/** @var IServerContainer */
private $container;

public function __construct(IConfig $config, IServerContainer $container) {
/** @var IUserManager */
private $userManager;
/** @var IGroupManager */
private $groupManager;
/** @var LoggerInterface */
private $logger;
/** @var IManager */
private $notificationManager;

public function __construct(IConfig $config,
IServerContainer $container,
IUserManager $userManager,
IGroupManager $groupManager,
LoggerInterface $logger,
IManager $notificationManager) {
$this->config = $config;
$this->container = $container;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->logger = $logger;
$this->notificationManager = $notificationManager;
}

private function getSubscription(): ?ISubscription {
Expand Down Expand Up @@ -127,9 +149,87 @@ public function delegateHasValidSubscription(): bool {
* @since 17.0.0
*/
public function delegateHasExtendedSupport(): bool {
if ($this->getSubscription() instanceof ISubscription && method_exists($this->subscription, 'hasExtendedSupport')) {
if ($this->getSubscription() instanceof ISubscription) {
return $this->getSubscription()->hasExtendedSupport();
}
return false;
}


/**
* Indicates if a hard user limit is reached and no new users should be created
*
* @since 21.0.0
*/
public function delegateIsHardUserLimitReached(): bool {
$subscription = $this->getSubscription();
if ($subscription instanceof ISubscription &&
$subscription->hasValidSubscription()) {
$userLimitReached = $subscription->isHardUserLimitReached();
if ($userLimitReached) {
$this->notifyAboutReachedUserLimit();
}
return $userLimitReached;
}

$isOneClickInstance = $this->config->getSystemValueBool('one-click-instance', false);

if (!$isOneClickInstance) {
return false;
}

$userCount = $this->getUserCount();
$hardUserLimit = $this->config->getSystemValue('one-click-instance.user-limit', 50);

$userLimitReached = $userCount >= $hardUserLimit;
if ($userLimitReached) {
$this->notifyAboutReachedUserLimit();
}
return $userLimitReached;
}

private function getUserCount(): int {
$userCount = 0;
$backends = $this->userManager->getBackends();
foreach ($backends as $backend) {
if ($backend->implementsActions(Backend::COUNT_USERS)) {
$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' => 'lib']);
}
}
}

$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' => 'lib']);
}

return $userCount;
}

private function notifyAboutReachedUserLimit() {
$admins = $this->groupManager->get('admin')->getUsers();
foreach ($admins as $admin) {
$notification = $this->notificationManager->createNotification();

$notification->setApp('core')
->setUser($admin->getUID())
->setDateTime(new \DateTime())
->setObject('user_limit_reached', '1')
->setSubject('user_limit_reached');
$this->notificationManager->notify($notification);
}

$this->logger->warning('The user limit was reached and the new user was not created', ['app' => 'lib']);
}
}
8 changes: 8 additions & 0 deletions lib/private/User/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

namespace OC\User;

use OC\HintException;
use OC\Hooks\PublicEmitter;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\EventDispatcher\IEventDispatcher;
Expand All @@ -42,6 +43,7 @@
use OCP\IUser;
use OCP\IUserBackend;
use OCP\IUserManager;
use OCP\Support\Subscription\IRegistry;
use OCP\User\Backend\IGetRealUIDBackend;
use OCP\User\Events\CreateUserEvent;
use OCP\User\Events\UserCreatedEvent;
Expand Down Expand Up @@ -297,6 +299,12 @@ public function searchDisplayName($pattern, $limit = null, $offset = null) {
* @return bool|IUser the created user or false
*/
public function createUser($uid, $password) {
// DI injection is not used here as IRegistry needs the user manager itself for user count and thus it would create a cyclic dependency
if (\OC::$server->get(IRegistry::class)->delegateIsHardUserLimitReached()) {
$l = \OC::$server->getL10N('lib');
throw new HintException($l->t('The user limit has been reached and the user was not created.'));
}

$localBackends = [];
foreach ($this->backends as $backend) {
if ($backend instanceof Database) {
Expand Down
7 changes: 7 additions & 0 deletions lib/public/Support/Subscription/IRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,11 @@ public function delegateHasValidSubscription(): bool;
* @since 17.0.0
*/
public function delegateHasExtendedSupport(): bool;

/**
* Indicates if a hard user limit is reached and no new users should be created
*
* @since 21.0.0
*/
public function delegateIsHardUserLimitReached(): bool;
}
7 changes: 7 additions & 0 deletions lib/public/Support/Subscription/ISubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,11 @@ public function hasValidSubscription(): bool;
* @since 17.0.0
*/
public function hasExtendedSupport(): bool;

/**
* Indicates if a hard user limit is reached and no new users should be created
*
* @since 21.0.0
*/
public function isHardUserLimitReached(): bool;
}
9 changes: 8 additions & 1 deletion tests/lib/Support/Subscription/DummySubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,19 @@ class DummySubscription implements \OCP\Support\Subscription\ISubscription {
private $hasValidSubscription;
/** @var bool */
private $hasExtendedSupport;
/** @var bool */
private $isHardUserLimitReached;

/**
* DummySubscription constructor.
*
* @param bool $hasValidSubscription
* @param bool $hasExtendedSupport
*/
public function __construct(bool $hasValidSubscription, bool $hasExtendedSupport) {
public function __construct(bool $hasValidSubscription, bool $hasExtendedSupport, bool $isHardUserLimitReached) {
$this->hasValidSubscription = $hasValidSubscription;
$this->hasExtendedSupport = $hasExtendedSupport;
$this->isHardUserLimitReached = $isHardUserLimitReached;
}

/**
Expand All @@ -55,4 +58,8 @@ public function hasValidSubscription(): bool {
public function hasExtendedSupport(): bool {
return $this->hasExtendedSupport;
}

public function isHardUserLimitReached(): bool {
return $this->isHardUserLimitReached;
}
}
Loading