]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add notification for user limit 24511/head
authorMorris Jobke <hey@morrisjobke.de>
Wed, 2 Dec 2020 09:07:34 +0000 (10:07 +0100)
committerbackportbot[bot] <backportbot[bot]@users.noreply.github.com>
Wed, 2 Dec 2020 17:24:44 +0000 (17:24 +0000)
Signed-off-by: Morris Jobke <hey@morrisjobke.de>
core/Application.php
core/Notification/CoreNotifier.php [new file with mode: 0644]
core/Notification/RemoveLinkSharesNotifier.php [deleted file]
lib/composer/composer/autoload_classmap.php
lib/composer/composer/autoload_static.php
lib/private/Support/Subscription/Registry.php
tests/lib/Support/Subscription/RegistryTest.php

index f105dfd25d11bd64657b3e39007e8a2d7b5fe997..4a4d2c6ef35c0fdcfd85b84d37ea958e922e45c0 100644 (file)
@@ -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/CoreNotifier.php b/core/Notification/CoreNotifier.php
new file mode 100644 (file)
index 0000000..dd362da
--- /dev/null
@@ -0,0 +1,86 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @author Christoph Wurst <christoph@winzerhof-wurst.at>
+ * @author Joas Schilling <coding@schilljs.com>
+ * @author Morris Jobke <hey@morrisjobke.de>
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OC\Core\Notification;
+
+use OCP\L10N\IFactory;
+use OCP\Notification\INotification;
+use OCP\Notification\INotifier;
+
+class CoreNotifier implements INotifier {
+       /** @var IFactory */
+       private $l10nFactory;
+
+       public function __construct(IFactory $factory) {
+               $this->l10nFactory = $factory;
+       }
+
+       /**
+        * Identifier of the notifier, only use [a-z0-9_]
+        *
+        * @return string
+        * @since 17.0.0
+        */
+       public function getID(): string {
+               return 'core';
+       }
+
+       /**
+        * Human readable name describing the notifier
+        *
+        * @return string
+        * @since 17.0.0
+        */
+       public function getName(): string {
+               return $this->l10nFactory->get('core')->t('Nextcloud Server');
+       }
+
+       public function prepare(INotification $notification, string $languageCode): INotification {
+               if ($notification->getApp() !== 'core') {
+                       throw new \InvalidArgumentException();
+               }
+               $l = $this->l10nFactory->get('core', $languageCode);
+
+               if ($notification->getSubject() === 'repair_exposing_links') {
+                       $notification->setParsedSubject($l->t('Some of your link shares have been removed'));
+                       $notification->setParsedMessage($l->t('Due to a security bug we had to remove some of your link shares. Please see the link for more information.'));
+                       $notification->setLink('https://nextcloud.com/security/advisory/?id=NC-SA-2019-003');
+                       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/core/Notification/RemoveLinkSharesNotifier.php b/core/Notification/RemoveLinkSharesNotifier.php
deleted file mode 100644 (file)
index 52a71fc..0000000
+++ /dev/null
@@ -1,78 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-/**
- * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-namespace OC\Core\Notification;
-
-use OCP\L10N\IFactory;
-use OCP\Notification\INotification;
-use OCP\Notification\INotifier;
-
-class RemoveLinkSharesNotifier implements INotifier {
-       /** @var IFactory */
-       private $l10nFactory;
-
-       public function __construct(IFactory $factory) {
-               $this->l10nFactory = $factory;
-       }
-
-       /**
-        * Identifier of the notifier, only use [a-z0-9_]
-        *
-        * @return string
-        * @since 17.0.0
-        */
-       public function getID(): string {
-               return 'core';
-       }
-
-       /**
-        * Human readable name describing the notifier
-        *
-        * @return string
-        * @since 17.0.0
-        */
-       public function getName(): string {
-               return $this->l10nFactory->get('core')->t('Nextcloud Server');
-       }
-
-       public function prepare(INotification $notification, string $languageCode): INotification {
-               if ($notification->getApp() !== 'core') {
-                       throw new \InvalidArgumentException();
-               }
-               $l = $this->l10nFactory->get('core', $languageCode);
-
-               if ($notification->getSubject() === 'repair_exposing_links') {
-                       $notification->setParsedSubject($l->t('Some of your link shares have been removed'));
-                       $notification->setParsedMessage($l->t('Due to a security bug we had to remove some of your link shares. Please see the link for more information.'));
-                       $notification->setLink('https://nextcloud.com/security/advisory/?id=NC-SA-2019-003');
-                       return $notification;
-               }
-
-               throw new \InvalidArgumentException('Invalid subject');
-       }
-}
index bfe7e7b38161848a3b76e98e9395c1b08c09bb3b..3db497983d0ffd7acd988f4b9e1e17c6c3134ea8 100644 (file)
@@ -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',
index 10fd332b2b5ddc74ea263ee3748c0e16d1edaf7a..0dc5fcebdfb4f0512ea739b7c54aa3a0f2206181 100644 (file)
@@ -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',
index ba9c4099b9bdecc240046b33785178306f5fab11..72bae2adc8ecb6c8162bbc6449c6686518cf06b3 100644 (file)
@@ -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']);
        }
 }
index 58995afcab81011c7016a059f3041358f4a0c940..95759e09547a03868af9fa51f423ffe0c3830dfc 100644 (file)
@@ -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());
        }
 }