From a8780debd3d347132065007f558f37c41a898e05 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Wed, 2 Dec 2020 10:07:34 +0100 Subject: [PATCH] Add notification for user limit Signed-off-by: Morris Jobke --- core/Application.php | 4 +-- ...inkSharesNotifier.php => CoreNotifier.php} | 10 +++++- lib/composer/composer/autoload_classmap.php | 2 +- lib/composer/composer/autoload_static.php | 2 +- lib/private/Support/Subscription/Registry.php | 25 +++++++++++++-- .../lib/Support/Subscription/RegistryTest.php | 32 ++++++++++++++++++- 6 files changed, 67 insertions(+), 8 deletions(-) rename core/Notification/{RemoveLinkSharesNotifier.php => CoreNotifier.php} (81%) diff --git a/core/Application.php b/core/Application.php index f105dfd25d1..4a4d2c6ef35 100644 --- a/core/Application.php +++ b/core/Application.php @@ -39,7 +39,7 @@ use OC\Authentication\Listeners\RemoteWipeNotificationsListener; 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; @@ -71,7 +71,7 @@ class Application extends App { $eventDispatcher = $server->query(IEventDispatcher::class); $notificationManager = $server->getNotificationManager(); - $notificationManager->registerNotifierService(RemoveLinkSharesNotifier::class); + $notificationManager->registerNotifierService(CoreNotifier::class); $notificationManager->registerNotifierService(AuthenticationNotifier::class); $oldEventDispatcher = $server->getEventDispatcher(); diff --git a/core/Notification/RemoveLinkSharesNotifier.php b/core/Notification/CoreNotifier.php similarity index 81% rename from core/Notification/RemoveLinkSharesNotifier.php rename to core/Notification/CoreNotifier.php index 52a71fced25..dd362dac8c1 100644 --- a/core/Notification/RemoveLinkSharesNotifier.php +++ b/core/Notification/CoreNotifier.php @@ -7,6 +7,7 @@ declare(strict_types=1); * * @author Christoph Wurst * @author Joas Schilling + * @author Morris Jobke * @author Roeland Jago Douma * * @license GNU AGPL version 3 or any later version @@ -32,7 +33,7 @@ use OCP\L10N\IFactory; use OCP\Notification\INotification; use OCP\Notification\INotifier; -class RemoveLinkSharesNotifier implements INotifier { +class CoreNotifier implements INotifier { /** @var IFactory */ private $l10nFactory; @@ -73,6 +74,13 @@ class RemoveLinkSharesNotifier implements INotifier { 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'); } } diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index bfe7e7b3816..3db497983d0 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -924,7 +924,7 @@ return array( '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', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 10fd332b2b5..0dc5fcebdfb 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -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', diff --git a/lib/private/Support/Subscription/Registry.php b/lib/private/Support/Subscription/Registry.php index ba9c4099b9b..72bae2adc8e 100644 --- a/lib/private/Support/Subscription/Registry.php +++ b/lib/private/Support/Subscription/Registry.php @@ -31,8 +31,10 @@ 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; @@ -54,17 +56,25 @@ class Registry implements IRegistry { private $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, - LoggerInterface $logger) { + 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 { @@ -208,7 +218,18 @@ class Registry implements IRegistry { } private function notifyAboutReachedUserLimit() { - // TODO notify admin about reached user limit + $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']); } } diff --git a/tests/lib/Support/Subscription/RegistryTest.php b/tests/lib/Support/Subscription/RegistryTest.php index 58995afcab8..95759e09547 100644 --- a/tests/lib/Support/Subscription/RegistryTest.php +++ b/tests/lib/Support/Subscription/RegistryTest.php @@ -24,8 +24,11 @@ namespace Test\Support\Subscription; use OC\Support\Subscription\Registry; use OCP\IConfig; +use OCP\IGroup; +use OCP\IGroupManager; use OCP\IServerContainer; use OCP\IUserManager; +use OCP\Notification\IManager; use OCP\Support\Subscription\ISubscription; use OCP\Support\Subscription\ISupportedApps; use PHPUnit\Framework\MockObject\MockObject; @@ -46,21 +49,31 @@ class RegistryTest extends TestCase { /** @var MockObject|IUserManager */ private $userManager; + /** @var MockObject|IGroupManager */ + private $groupManager; + /** @var MockObject|LoggerInterface */ private $logger; + /** @var MockObject|IManager */ + private $notificationManager; + protected function setUp(): void { parent::setUp(); $this->config = $this->createMock(IConfig::class); $this->serverContainer = $this->createMock(IServerContainer::class); $this->userManager = $this->createMock(IUserManager::class); + $this->groupManager = $this->createMock(IGroupManager::class); $this->logger = $this->createMock(LoggerInterface::class); + $this->notificationManager = $this->createMock(IManager::class); $this->registry = new Registry( $this->config, $this->serverContainer, $this->userManager, - $this->logger + $this->groupManager, + $this->logger, + $this->notificationManager ); } @@ -153,6 +166,13 @@ class RegistryTest extends TestCase { ->method('isHardUserLimitReached') ->willReturn(true); $this->registry->register($subscription); + $dummyGroup = $this->createMock(IGroup::class); + $dummyGroup->expects($this->once()) + ->method('getUsers') + ->willReturn([]); + $this->groupManager->expects($this->once()) + ->method('get') + ->willReturn($dummyGroup); $this->assertSame(true, $this->registry->delegateIsHardUserLimitReached()); } @@ -204,6 +224,16 @@ class RegistryTest extends TestCase { ->method('getBackends') ->willReturn([$dummyBackend]); + if ($expectedResult) { + $dummyGroup = $this->createMock(IGroup::class); + $dummyGroup->expects($this->once()) + ->method('getUsers') + ->willReturn([]); + $this->groupManager->expects($this->once()) + ->method('get') + ->willReturn($dummyGroup); + } + $this->assertSame($expectedResult, $this->registry->delegateIsHardUserLimitReached()); } } -- 2.39.5