diff options
author | Joas Schilling <213943+nickvergessen@users.noreply.github.com> | 2021-03-05 15:05:06 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-05 15:05:06 +0300 |
commit | f92d21e80eef48a81decd2c3d09496b100294803 (patch) | |
tree | 8b6b2c0830abf24d6c9aee6b84218606ee79c7f6 /apps | |
parent | e47bd8324d6f36f39c360064d0dad10fa7949519 (diff) | |
parent | f492a6aac38828304ecc2ec3a32202aaa36d25d2 (diff) |
Merge pull request #25870 from nextcloud/techdept/settings/move_group_activity_events
Move group activities to events
Diffstat (limited to 'apps')
6 files changed, 202 insertions, 99 deletions
diff --git a/apps/settings/composer/composer/autoload_classmap.php b/apps/settings/composer/composer/autoload_classmap.php index a6f2d2e5685..c4a49aaca07 100644 --- a/apps/settings/composer/composer/autoload_classmap.php +++ b/apps/settings/composer/composer/autoload_classmap.php @@ -33,6 +33,8 @@ return array( 'OCA\\Settings\\Events\\BeforeTemplateRenderedEvent' => $baseDir . '/../lib/Events/BeforeTemplateRenderedEvent.php', 'OCA\\Settings\\Hooks' => $baseDir . '/../lib/Hooks.php', 'OCA\\Settings\\Listener\\AppPasswordCreatedActivityListener' => $baseDir . '/../lib/Listener/AppPasswordCreatedActivityListener.php', + 'OCA\\Settings\\Listener\\UserAddedToGroupActivityListener' => $baseDir . '/../lib/Listener/UserAddedToGroupActivityListener.php', + 'OCA\\Settings\\Listener\\UserRemovedFromGroupActivityListener' => $baseDir . '/../lib/Listener/UserRemovedFromGroupActivityListener.php', 'OCA\\Settings\\Mailer\\NewUserMailHelper' => $baseDir . '/../lib/Mailer/NewUserMailHelper.php', 'OCA\\Settings\\Middleware\\SubadminMiddleware' => $baseDir . '/../lib/Middleware/SubadminMiddleware.php', 'OCA\\Settings\\Search\\AppSearch' => $baseDir . '/../lib/Search/AppSearch.php', diff --git a/apps/settings/composer/composer/autoload_static.php b/apps/settings/composer/composer/autoload_static.php index 096e6a2d5b4..0417683ebc6 100644 --- a/apps/settings/composer/composer/autoload_static.php +++ b/apps/settings/composer/composer/autoload_static.php @@ -48,6 +48,8 @@ class ComposerStaticInitSettings 'OCA\\Settings\\Events\\BeforeTemplateRenderedEvent' => __DIR__ . '/..' . '/../lib/Events/BeforeTemplateRenderedEvent.php', 'OCA\\Settings\\Hooks' => __DIR__ . '/..' . '/../lib/Hooks.php', 'OCA\\Settings\\Listener\\AppPasswordCreatedActivityListener' => __DIR__ . '/..' . '/../lib/Listener/AppPasswordCreatedActivityListener.php', + 'OCA\\Settings\\Listener\\UserAddedToGroupActivityListener' => __DIR__ . '/..' . '/../lib/Listener/UserAddedToGroupActivityListener.php', + 'OCA\\Settings\\Listener\\UserRemovedFromGroupActivityListener' => __DIR__ . '/..' . '/../lib/Listener/UserRemovedFromGroupActivityListener.php', 'OCA\\Settings\\Mailer\\NewUserMailHelper' => __DIR__ . '/..' . '/../lib/Mailer/NewUserMailHelper.php', 'OCA\\Settings\\Middleware\\SubadminMiddleware' => __DIR__ . '/..' . '/../lib/Middleware/SubadminMiddleware.php', 'OCA\\Settings\\Search\\AppSearch' => __DIR__ . '/..' . '/../lib/Search/AppSearch.php', 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); + } + } +} |