diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2021-03-02 10:38:24 +0100 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2021-03-03 11:33:33 +0100 |
commit | f492a6aac38828304ecc2ec3a32202aaa36d25d2 (patch) | |
tree | c49e3c7a35da927815825cedcab7621a4a32e9af /apps/settings/lib | |
parent | a1be5f33af7e65a80b2e39d7c28c727c6f3ef6fe (diff) | |
download | nextcloud-server-f492a6aac38828304ecc2ec3a32202aaa36d25d2.tar.gz nextcloud-server-f492a6aac38828304ecc2ec3a32202aaa36d25d2.zip |
Move group activities to events
Use the fancy events.
Nicer code
Nicely isolated code
win-win
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'apps/settings/lib')
-rw-r--r-- | apps/settings/lib/AppInfo/Application.php | 29 | ||||
-rw-r--r-- | apps/settings/lib/Hooks.php | 76 | ||||
-rw-r--r-- | apps/settings/lib/Listener/UserAddedToGroupActivityListener.php | 96 | ||||
-rw-r--r-- | apps/settings/lib/Listener/UserRemovedFromGroupActivityListener.php | 96 |
4 files changed, 198 insertions, 99 deletions
diff --git a/apps/settings/lib/AppInfo/Application.php b/apps/settings/lib/AppInfo/Application.php index 7fa3f8f347c..f089260a7c7 100644 --- a/apps/settings/lib/AppInfo/Application.php +++ b/apps/settings/lib/AppInfo/Application.php @@ -38,10 +38,11 @@ namespace OCA\Settings\AppInfo; use OC\AppFramework\Utility\TimeFactory; use OC\Authentication\Events\AppPasswordCreatedEvent; use OC\Authentication\Token\IProvider; -use OC\Group\Manager; use OC\Server; use OCA\Settings\Hooks; use OCA\Settings\Listener\AppPasswordCreatedActivityListener; +use OCA\Settings\Listener\UserAddedToGroupActivityListener; +use OCA\Settings\Listener\UserRemovedFromGroupActivityListener; use OCA\Settings\Mailer\NewUserMailHelper; use OCA\Settings\Middleware\SubadminMiddleware; use OCA\Settings\Search\AppSearch; @@ -52,10 +53,9 @@ use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\AppFramework\IAppContainer; use OCP\Defaults; -use OCP\IGroup; -use OCP\IGroupManager; +use OCP\Group\Events\UserAddedEvent; +use OCP\Group\Events\UserRemovedEvent; use OCP\IServerContainer; -use OCP\IUser; use OCP\Settings\IManager; use OCP\Util; @@ -78,6 +78,8 @@ class Application extends App implements IBootstrap { // Register listeners $context->registerEventListener(AppPasswordCreatedEvent::class, AppPasswordCreatedActivityListener::class); + $context->registerEventListener(UserAddedEvent::class, UserAddedToGroupActivityListener::class); + $context->registerEventListener(UserRemovedEvent::class, UserRemovedFromGroupActivityListener::class); /** * Core class wrappers @@ -129,27 +131,8 @@ class Application extends App implements IBootstrap { public function boot(IBootContext $context): void { Util::connectHook('OC_User', 'post_setPassword', $this, 'onChangePassword'); Util::connectHook('OC_User', 'changeUser', $this, 'onChangeInfo'); - - $context->injectFn(function (IGroupManager $groupManager) { - /** @var IGroupManager|Manager $groupManager */ - $groupManager->listen('\OC\Group', 'postRemoveUser', [$this, 'removeUserFromGroup']); - $groupManager->listen('\OC\Group', 'postAddUser', [$this, 'addUserToGroup']); - }); - } - - public function addUserToGroup(IGroup $group, IUser $user): void { - /** @var Hooks $hooks */ - $hooks = $this->getContainer()->query(Hooks::class); - $hooks->addUserToGroup($group, $user); } - public function removeUserFromGroup(IGroup $group, IUser $user): void { - /** @var Hooks $hooks */ - $hooks = $this->getContainer()->query(Hooks::class); - $hooks->removeUserFromGroup($group, $user); - } - - /** * @param array $parameters * @throws \InvalidArgumentException diff --git a/apps/settings/lib/Hooks.php b/apps/settings/lib/Hooks.php index 62eddcd1c0b..c8fa5609914 100644 --- a/apps/settings/lib/Hooks.php +++ b/apps/settings/lib/Hooks.php @@ -27,11 +27,9 @@ namespace OCA\Settings; -use OCA\Settings\Activity\GroupProvider; use OCA\Settings\Activity\Provider; use OCP\Activity\IManager as IActivityManager; use OCP\IConfig; -use OCP\IGroup; use OCP\IGroupManager; use OCP\IURLGenerator; use OCP\IUser; @@ -214,78 +212,4 @@ class Hooks { $this->mailer->send($message); } } - - /** - * @param IGroup $group - * @param IUser $user - * @throws \InvalidArgumentException - * @throws \BadMethodCallException - */ - public function addUserToGroup(IGroup $group, IUser $user): void { - $subAdminManager = $this->groupManager->getSubAdmin(); - $usersToNotify = $subAdminManager->getGroupsSubAdmins($group); - $usersToNotify[] = $user; - - - $event = $this->activityManager->generateEvent(); - $event->setApp('settings') - ->setType('group_settings'); - - $actor = $this->userSession->getUser(); - if ($actor instanceof IUser) { - $event->setAuthor($actor->getUID()) - ->setSubject(GroupProvider::ADDED_TO_GROUP, [ - 'user' => $user->getUID(), - 'group' => $group->getGID(), - 'actor' => $actor->getUID(), - ]); - } else { - $event->setSubject(GroupProvider::ADDED_TO_GROUP, [ - 'user' => $user->getUID(), - 'group' => $group->getGID(), - ]); - } - - foreach ($usersToNotify as $userToNotify) { - $event->setAffectedUser($userToNotify->getUID()); - $this->activityManager->publish($event); - } - } - - /** - * @param IGroup $group - * @param IUser $user - * @throws \InvalidArgumentException - * @throws \BadMethodCallException - */ - public function removeUserFromGroup(IGroup $group, IUser $user): void { - $subAdminManager = $this->groupManager->getSubAdmin(); - $usersToNotify = $subAdminManager->getGroupsSubAdmins($group); - $usersToNotify[] = $user; - - - $event = $this->activityManager->generateEvent(); - $event->setApp('settings') - ->setType('group_settings'); - - $actor = $this->userSession->getUser(); - if ($actor instanceof IUser) { - $event->setAuthor($actor->getUID()) - ->setSubject(GroupProvider::REMOVED_FROM_GROUP, [ - 'user' => $user->getUID(), - 'group' => $group->getGID(), - 'actor' => $actor->getUID(), - ]); - } else { - $event->setSubject(GroupProvider::REMOVED_FROM_GROUP, [ - 'user' => $user->getUID(), - 'group' => $group->getGID(), - ]); - } - - foreach ($usersToNotify as $userToNotify) { - $event->setAffectedUser($userToNotify->getUID()); - $this->activityManager->publish($event); - } - } } diff --git a/apps/settings/lib/Listener/UserAddedToGroupActivityListener.php b/apps/settings/lib/Listener/UserAddedToGroupActivityListener.php new file mode 100644 index 00000000000..181b65986a6 --- /dev/null +++ b/apps/settings/lib/Listener/UserAddedToGroupActivityListener.php @@ -0,0 +1,96 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright Copyright (c) 2021 Roeland Jago Douma <roeland@famdouma.nl> + * + * @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 OCA\Settings\Listener; + +use OC\Group\Manager; +use OCA\Settings\Activity\GroupProvider; +use OCP\Activity\IManager; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Group\Events\UserAddedEvent; +use OCP\IUser; +use OCP\IUserSession; + +class UserAddedToGroupActivityListener implements IEventListener { + + /** @var Manager */ + private $groupManager; + + /** @var IManager */ + private $activityManager; + + /** @var IUserSession */ + private $userSession; + + public function __construct( + Manager $groupManager, + IManager $activityManager, + IUserSession $userSession + ) { + $this->groupManager = $groupManager; + $this->activityManager = $activityManager; + $this->userSession = $userSession; + } + + public function handle(Event $event): void { + if (!($event instanceof UserAddedEvent)) { + return; + } + + $user = $event->getUser(); + $group = $event->getGroup(); + + $subAdminManager = $this->groupManager->getSubAdmin(); + $usersToNotify = $subAdminManager->getGroupsSubAdmins($group); + $usersToNotify[] = $user; + + + $event = $this->activityManager->generateEvent(); + $event->setApp('settings') + ->setType('group_settings'); + + $actor = $this->userSession->getUser(); + if ($actor instanceof IUser) { + $event->setAuthor($actor->getUID()) + ->setSubject(GroupProvider::ADDED_TO_GROUP, [ + 'user' => $user->getUID(), + 'group' => $group->getGID(), + 'actor' => $actor->getUID(), + ]); + } else { + $event->setSubject(GroupProvider::ADDED_TO_GROUP, [ + 'user' => $user->getUID(), + 'group' => $group->getGID(), + ]); + } + + foreach ($usersToNotify as $userToNotify) { + $event->setAffectedUser($userToNotify->getUID()); + $this->activityManager->publish($event); + } + } +} diff --git a/apps/settings/lib/Listener/UserRemovedFromGroupActivityListener.php b/apps/settings/lib/Listener/UserRemovedFromGroupActivityListener.php new file mode 100644 index 00000000000..b6e1fbc5188 --- /dev/null +++ b/apps/settings/lib/Listener/UserRemovedFromGroupActivityListener.php @@ -0,0 +1,96 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright Copyright (c) 2021 Roeland Jago Douma <roeland@famdouma.nl> + * + * @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 OCA\Settings\Listener; + +use OC\Group\Manager; +use OCA\Settings\Activity\GroupProvider; +use OCP\Activity\IManager; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Group\Events\UserRemovedEvent; +use OCP\IUser; +use OCP\IUserSession; + +class UserRemovedFromGroupActivityListener implements IEventListener { + + /** @var Manager */ + private $groupManager; + + /** @var IManager */ + private $activityManager; + + /** @var IUserSession */ + private $userSession; + + public function __construct( + Manager $groupManager, + IManager $activityManager, + IUserSession $userSession + ) { + $this->groupManager = $groupManager; + $this->activityManager = $activityManager; + $this->userSession = $userSession; + } + + public function handle(Event $event): void { + if (!($event instanceof UserRemovedEvent)) { + return; + } + + $user = $event->getUser(); + $group = $event->getGroup(); + + $subAdminManager = $this->groupManager->getSubAdmin(); + $usersToNotify = $subAdminManager->getGroupsSubAdmins($group); + $usersToNotify[] = $user; + + + $event = $this->activityManager->generateEvent(); + $event->setApp('settings') + ->setType('group_settings'); + + $actor = $this->userSession->getUser(); + if ($actor instanceof IUser) { + $event->setAuthor($actor->getUID()) + ->setSubject(GroupProvider::REMOVED_FROM_GROUP, [ + 'user' => $user->getUID(), + 'group' => $group->getGID(), + 'actor' => $actor->getUID(), + ]); + } else { + $event->setSubject(GroupProvider::REMOVED_FROM_GROUP, [ + 'user' => $user->getUID(), + 'group' => $group->getGID(), + ]); + } + + foreach ($usersToNotify as $userToNotify) { + $event->setAffectedUser($userToNotify->getUID()); + $this->activityManager->publish($event); + } + } +} |