diff --git a/appinfo/app.php b/appinfo/app.php index 9ccd6640..0129e163 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -21,3 +21,4 @@ $app = new \OCA\CustomGroups\Application(); $app->registerGroupBackend(); +$app->registerNotifier(); diff --git a/lib/Application.php b/lib/Application.php index 9520d2d9..c91f21eb 100644 --- a/lib/Application.php +++ b/lib/Application.php @@ -35,4 +35,20 @@ public function registerGroupBackend() { $backend = $this->getContainer()->query('\OCA\CustomGroups\CustomGroupsBackend'); $this->getContainer()->getServer()->getGroupManager()->addBackend($backend); } + + /** + * Registers the notifier + */ + public function registerNotifier() { + $manager = $this->getContainer()->getServer()->getNotificationManager(); + $manager->registerNotifier(function() use ($manager) { + return $this->getContainer()->query('\OCA\CustomGroups\Notifier'); + }, function() { + $l = \OC::$server->getL10N('customgroups'); + return [ + 'id' => 'customgroups', + 'name' => $l->t('Custom groups'), + ]; + }); + } } diff --git a/lib/Dav/GroupMembershipCollection.php b/lib/Dav/GroupMembershipCollection.php index 44f7cd89..d4d56b46 100644 --- a/lib/Dav/GroupMembershipCollection.php +++ b/lib/Dav/GroupMembershipCollection.php @@ -175,6 +175,8 @@ public function createFile($userId, $data = null) { if (!$this->groupsHandler->addToGroup($userId, $groupId, CustomGroupsDatabaseHandler::ROLE_MEMBER)) { throw new PreconditionFailed("The user \"$userId\" is already member of this group"); } + + $this->helper->notifyUser($userId, $this->groupInfo); } /** diff --git a/lib/Dav/UsersCollection.php b/lib/Dav/UsersCollection.php index 3070d78f..57da2689 100644 --- a/lib/Dav/UsersCollection.php +++ b/lib/Dav/UsersCollection.php @@ -79,13 +79,7 @@ public function createFile($name, $data = null) { * @throws MethodNotAllowed if the group already exists */ public function createDirectory($name) { - $groupId = $this->groupsHandler->createGroup($name, $name); - if (is_null($groupId)) { - throw new MethodNotAllowed("Group with uri \"$name\" already exists"); - } - - // add current user as admin - $this->groupsHandler->addToGroup($this->helper->getUserId(), $groupId, true); + throw new MethodNotAllowed('Cannot create user nodes'); } /** diff --git a/lib/Notifier.php b/lib/Notifier.php new file mode 100644 index 00000000..507d39d7 --- /dev/null +++ b/lib/Notifier.php @@ -0,0 +1,80 @@ + + * + * @copyright Copyright (c) 2017, ownCloud GmbH + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\CustomGroups; + +use OCP\L10N\IFactory; +use OCP\Notification\IManager; +use OCP\Notification\INotification; +use OCP\Notification\INotifier; +use OCA\CustomGroups\CustomGroupsDatabaseHandler; + +class Notifier implements INotifier { + + /** + * @var IFactory + */ + protected $l10NFactory; + + /** + * @var CustomGroupsDatabaseHandler + */ + protected $handler; + + /** + * Notifier constructor. + * + * @param IFactory $l10NFactory + * @param CustomGroupsDatabaseHandler $handler + */ + public function __construct(IFactory $l10NFactory, CustomGroupsDatabaseHandler $handler) { + $this->l10NFactory = $l10NFactory; + $this->handler = $handler; + } + + /** + * @param INotification $notification + * @param string $languageCode The code of the language that should be used to prepare the notification + * @return INotification + * @throws \InvalidArgumentException When the notification was not prepared by a notifier + */ + public function prepare(INotification $notification, $languageCode) { + if ($notification->getApp() !== 'customgroups') { + throw new \InvalidArgumentException(); + } + + $l = $this->l10NFactory->get('customgroups', $languageCode); + if ($notification->getObjectType() === 'customgroup') { + if ($notification->getSubject() === 'added_member') { + $notification->setParsedSubject( + $l->t('Added to group "%1$s" by "%2$s".', $notification->getSubjectParameters()) + ); + } + if ($notification->getMessage() === 'added_member') { + $notification->setParsedMessage( + $l->t('You have been added to the group "%1$s" by "%2$s".', $notification->getMessageParameters()) + ); + } + } + + return $notification; + } +} diff --git a/lib/Service/MembershipHelper.php b/lib/Service/MembershipHelper.php index deb06dea..6b9b452a 100644 --- a/lib/Service/MembershipHelper.php +++ b/lib/Service/MembershipHelper.php @@ -27,6 +27,8 @@ use OCP\IGroupManager; use OCA\CustomGroups\Search; use OCP\IUser; +use OCP\Notification\IManager; +use OCP\IURLGenerator; /** * Membership helper @@ -63,6 +65,20 @@ class MembershipHelper { */ private $groupManager; + /** + * Notification manager + * + * @var IManager + */ + private $notificationManager; + + /** + * URL generator + * + * @var IURLGenerator + */ + private $urlGenerator; + /** * Membership info for the currently logged in user * @@ -77,17 +93,23 @@ class MembershipHelper { * @param IUserSession $userSession user session * @param IUserManager $userManager user manager * @param IGroupManager $groupManager group manager + * @param IManager $notificationManager notification manager + * @param IURLGenerator $urlGenerator URL generator */ public function __construct( CustomGroupsDatabaseHandler $groupsHandler, IUserSession $userSession, IUserManager $userManager, - IGroupManager $groupManager + IGroupManager $groupManager, + IManager $notificationManager, + IURLGenerator $urlGenerator ) { $this->groupsHandler = $groupsHandler; $this->userSession = $userSession; $this->userManager = $userManager; $this->groupManager = $groupManager; + $this->notificationManager = $notificationManager; + $this->urlGenerator = $urlGenerator; } /** @@ -233,4 +255,23 @@ public function searchForNewMembers($groupId, $pattern, $limit = 200) { return $totalResults; } + + /** + * Notify the given user about the given group + * + * @param string $targetUserId user to notify + * @param array $groupInfo group info + */ + public function notifyUser($targetUserId, $groupInfo) { + $link = $this->urlGenerator->linkToRouteAbsolute('settings.SettingsPage.getPersonal', ['sectionid' => 'customgroups', 'group' => $groupInfo['uri']]); + $notification = $this->notificationManager->createNotification(); + $notification->setApp('customgroups') + ->setDateTime(new \DateTime()) + ->setObject('customgroup', $groupInfo['group_id']) + ->setSubject('added_member', [$this->getUserId(), $groupInfo['display_name']]) + ->setMessage('added_member', [$this->getUserId(), $groupInfo['display_name']]) + ->setUser($targetUserId) + ->setLink($link); + $this->notificationManager->notify($notification); + } } diff --git a/tests/unit/Dav/GroupMembershipCollectionTest.php b/tests/unit/Dav/GroupMembershipCollectionTest.php index e38836be..79242a36 100644 --- a/tests/unit/Dav/GroupMembershipCollectionTest.php +++ b/tests/unit/Dav/GroupMembershipCollectionTest.php @@ -30,6 +30,8 @@ use OCA\CustomGroups\Service\MembershipHelper; use OCP\IGroupManager; use OCA\CustomGroups\Search; +use OCP\IURLGenerator; +use OCP\Notification\IManager; /** * Class GroupMembershipCollectionTest @@ -91,12 +93,18 @@ public function setUp() { [strtoupper(self::NODE_USER), $nodeUser], ])); - $this->helper = new MembershipHelper( - $this->handler, - $this->userSession, - $this->userManager, - $this->groupManager - ); + $this->helper = $this->getMockBuilder(MembershipHelper::class) + ->setMethods(['notifyUser']) + ->setConstructorArgs([ + $this->handler, + $this->userSession, + $this->userManager, + $this->groupManager, + $this->createMock(IManager::class), + $this->createMock(IURLGenerator::class) + ]) + ->getMock(); + $this->node = new GroupMembershipCollection( ['group_id' => 1, 'uri' => 'group1', 'display_name' => 'Group One', 'role' => CustomGroupsDatabaseHandler::ROLE_ADMIN], $this->handler, diff --git a/tests/unit/Dav/GroupsCollectionTest.php b/tests/unit/Dav/GroupsCollectionTest.php index 592d8dc5..43ad1eed 100644 --- a/tests/unit/Dav/GroupsCollectionTest.php +++ b/tests/unit/Dav/GroupsCollectionTest.php @@ -29,6 +29,8 @@ use OCA\CustomGroups\Service\MembershipHelper; use OCP\IGroupManager; use OCA\CustomGroups\Search; +use OCP\IURLGenerator; +use OCP\Notification\IManager; /** * Class GroupsCollectionTest @@ -78,7 +80,9 @@ public function setUp() { $this->handler, $this->userSession, $this->userManager, - $this->groupManager + $this->groupManager, + $this->createMock(IManager::class), + $this->createMock(IURLGenerator::class) ); $this->collection = new GroupsCollection($this->handler, $this->helper); } diff --git a/tests/unit/Dav/MembershipNodeTest.php b/tests/unit/Dav/MembershipNodeTest.php index 923a9ac2..fad0e2ae 100644 --- a/tests/unit/Dav/MembershipNodeTest.php +++ b/tests/unit/Dav/MembershipNodeTest.php @@ -30,6 +30,8 @@ use OCP\IGroupManager; use OCA\CustomGroups\Dav\Roles; use OCA\CustomGroups\Search; +use OCP\IURLGenerator; +use OCP\Notification\IManager; /** * Class MembershipNodeTest @@ -90,7 +92,9 @@ public function setUp() { $this->handler, $this->userSession, $this->userManager, - $this->groupManager + $this->groupManager, + $this->createMock(IManager::class), + $this->createMock(IURLGenerator::class) ); $this->node = new MembershipNode( ['group_id' => 1, 'user_id' => self::NODE_USER, 'role' => CustomGroupsDatabaseHandler::ROLE_ADMIN], @@ -178,7 +182,9 @@ private function makeSelfNode($role) { $this->handler, $userSession, $this->userManager, - $this->groupManager + $this->groupManager, + $this->createMock(IManager::class), + $this->createMock(IURLGenerator::class) ); $memberInfo = ['group_id' => 1, 'user_id' => self::NODE_USER, 'role' => $role]; diff --git a/tests/unit/Dav/UsersCollectionTest.php b/tests/unit/Dav/UsersCollectionTest.php index 3e34a022..fa357e61 100644 --- a/tests/unit/Dav/UsersCollectionTest.php +++ b/tests/unit/Dav/UsersCollectionTest.php @@ -28,6 +28,8 @@ use OCA\CustomGroups\Service\MembershipHelper; use OCP\IGroupManager; use OCA\CustomGroups\Dav\GroupsCollection; +use OCP\IURLGenerator; +use OCP\Notification\IManager; /** * Class UsersCollectionTest @@ -84,7 +86,9 @@ public function setUp() { $this->handler, $this->userSession, $this->userManager, - $this->groupManager + $this->groupManager, + $this->createMock(IManager::class), + $this->createMock(IURLGenerator::class) ); $this->collection = new UsersCollection($this->handler, $this->helper); diff --git a/tests/unit/NotifierTest.php b/tests/unit/NotifierTest.php new file mode 100644 index 00000000..0a95677e --- /dev/null +++ b/tests/unit/NotifierTest.php @@ -0,0 +1,77 @@ + + * + * @copyright Copyright (c) 2017, ownCloud GmbH + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ +namespace OCA\CustomGroups\Tests\unit; + +use OCP\Notification\IManager; +use OCP\L10N\IFactory; +use OCA\CustomGroups\Notifier; +use OCA\CustomGroups\CustomGroupsDatabaseHandler; +use OCP\Notification\INotification; + +/** + * Class NotifierTest + * + * @package OCA\CustomGroups\Tests\Unit + */ +class NotifierTest extends \Test\TestCase { + + /** + * @var Notifier + */ + private $notifier; + + /** + * @var CustomGroupsDatabaseHandler + */ + private $handler; + + public function setUp() { + parent::setUp(); + + $this->handler = $this->createMock(CustomGroupsDatabaseHandler::class); + + $this->notifier = new Notifier( + \OC::$server->getL10NFactory(), + $this->handler + ); + } + + public function testPrepare() { + $notification = $this->createMock(INotification::class); + + $notification->method('getApp')->willReturn('customgroups'); + $notification->method('getObjectType')->willReturn('customgroup'); + $notification->method('getSubject')->willReturn('added_member'); + $notification->method('getMessage')->willReturn('added_member'); + $notification->method('getSubjectParameters')->willReturn(['group1', 'user1']); + $notification->method('getMessageParameters')->willReturn(['group1', 'user1']); + + $notification->expects($this->once()) + ->method('setParsedSubject') + ->with('Added to group "group1" by "user1".'); + $notification->expects($this->once()) + ->method('setParsedMessage') + ->with('You have been added to the group "group1" by "user1".'); + + $notification = $this->notifier->prepare($notification, 'en_US'); + } + +} diff --git a/tests/unit/Service/MembershipHelperTest.php b/tests/unit/Service/MembershipHelperTest.php index 540eb004..5edb73ef 100644 --- a/tests/unit/Service/MembershipHelperTest.php +++ b/tests/unit/Service/MembershipHelperTest.php @@ -27,6 +27,8 @@ use OCP\IUser; use OCA\CustomGroups\Service\MembershipHelper; use OCA\CustomGroups\Search; +use OCP\Notification\IManager; +use OCP\IURLGenerator; /** * Class MembershipHelperTest @@ -62,12 +64,24 @@ class MembershipHelperTest extends \Test\TestCase { */ private $userSession; + /** + * @var IManager + */ + private $notificationManager; + + /** + * @var IURLGenerator + */ + private $urlGenerator; + public function setUp() { parent::setUp(); $this->handler = $this->createMock(CustomGroupsDatabaseHandler::class); $this->userSession = $this->createMock(IUserSession::class); $this->userManager = $this->createMock(IUserManager::class); $this->groupManager = $this->createMock(IGroupManager::class); + $this->notificationManager = $this->createMock(IManager::class); + $this->urlGenerator = $this->createMock(IURLGenerator::class); // currently logged in user $user = $this->createMock(IUser::class); @@ -80,7 +94,9 @@ public function setUp() { $this->handler, $this->userSession, $this->userManager, - $this->groupManager + $this->groupManager, + $this->notificationManager, + $this->urlGenerator ); }