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>
This commit is contained in:
Roeland Jago Douma 2021-03-02 10:38:24 +01:00
parent a1be5f33af
commit f492a6aac3
6 changed files with 202 additions and 99 deletions

View File

@ -33,6 +33,8 @@ return array(
'OCA\\Settings\\Events\\BeforeTemplateRenderedEvent' => $baseDir . '/../lib/Events/BeforeTemplateRenderedEvent.php', 'OCA\\Settings\\Events\\BeforeTemplateRenderedEvent' => $baseDir . '/../lib/Events/BeforeTemplateRenderedEvent.php',
'OCA\\Settings\\Hooks' => $baseDir . '/../lib/Hooks.php', 'OCA\\Settings\\Hooks' => $baseDir . '/../lib/Hooks.php',
'OCA\\Settings\\Listener\\AppPasswordCreatedActivityListener' => $baseDir . '/../lib/Listener/AppPasswordCreatedActivityListener.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\\Mailer\\NewUserMailHelper' => $baseDir . '/../lib/Mailer/NewUserMailHelper.php',
'OCA\\Settings\\Middleware\\SubadminMiddleware' => $baseDir . '/../lib/Middleware/SubadminMiddleware.php', 'OCA\\Settings\\Middleware\\SubadminMiddleware' => $baseDir . '/../lib/Middleware/SubadminMiddleware.php',
'OCA\\Settings\\Search\\AppSearch' => $baseDir . '/../lib/Search/AppSearch.php', 'OCA\\Settings\\Search\\AppSearch' => $baseDir . '/../lib/Search/AppSearch.php',

View File

@ -48,6 +48,8 @@ class ComposerStaticInitSettings
'OCA\\Settings\\Events\\BeforeTemplateRenderedEvent' => __DIR__ . '/..' . '/../lib/Events/BeforeTemplateRenderedEvent.php', 'OCA\\Settings\\Events\\BeforeTemplateRenderedEvent' => __DIR__ . '/..' . '/../lib/Events/BeforeTemplateRenderedEvent.php',
'OCA\\Settings\\Hooks' => __DIR__ . '/..' . '/../lib/Hooks.php', 'OCA\\Settings\\Hooks' => __DIR__ . '/..' . '/../lib/Hooks.php',
'OCA\\Settings\\Listener\\AppPasswordCreatedActivityListener' => __DIR__ . '/..' . '/../lib/Listener/AppPasswordCreatedActivityListener.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\\Mailer\\NewUserMailHelper' => __DIR__ . '/..' . '/../lib/Mailer/NewUserMailHelper.php',
'OCA\\Settings\\Middleware\\SubadminMiddleware' => __DIR__ . '/..' . '/../lib/Middleware/SubadminMiddleware.php', 'OCA\\Settings\\Middleware\\SubadminMiddleware' => __DIR__ . '/..' . '/../lib/Middleware/SubadminMiddleware.php',
'OCA\\Settings\\Search\\AppSearch' => __DIR__ . '/..' . '/../lib/Search/AppSearch.php', 'OCA\\Settings\\Search\\AppSearch' => __DIR__ . '/..' . '/../lib/Search/AppSearch.php',

View File

@ -38,10 +38,11 @@ namespace OCA\Settings\AppInfo;
use OC\AppFramework\Utility\TimeFactory; use OC\AppFramework\Utility\TimeFactory;
use OC\Authentication\Events\AppPasswordCreatedEvent; use OC\Authentication\Events\AppPasswordCreatedEvent;
use OC\Authentication\Token\IProvider; use OC\Authentication\Token\IProvider;
use OC\Group\Manager;
use OC\Server; use OC\Server;
use OCA\Settings\Hooks; use OCA\Settings\Hooks;
use OCA\Settings\Listener\AppPasswordCreatedActivityListener; use OCA\Settings\Listener\AppPasswordCreatedActivityListener;
use OCA\Settings\Listener\UserAddedToGroupActivityListener;
use OCA\Settings\Listener\UserRemovedFromGroupActivityListener;
use OCA\Settings\Mailer\NewUserMailHelper; use OCA\Settings\Mailer\NewUserMailHelper;
use OCA\Settings\Middleware\SubadminMiddleware; use OCA\Settings\Middleware\SubadminMiddleware;
use OCA\Settings\Search\AppSearch; use OCA\Settings\Search\AppSearch;
@ -52,10 +53,9 @@ use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\IAppContainer; use OCP\AppFramework\IAppContainer;
use OCP\Defaults; use OCP\Defaults;
use OCP\IGroup; use OCP\Group\Events\UserAddedEvent;
use OCP\IGroupManager; use OCP\Group\Events\UserRemovedEvent;
use OCP\IServerContainer; use OCP\IServerContainer;
use OCP\IUser;
use OCP\Settings\IManager; use OCP\Settings\IManager;
use OCP\Util; use OCP\Util;
@ -78,6 +78,8 @@ class Application extends App implements IBootstrap {
// Register listeners // Register listeners
$context->registerEventListener(AppPasswordCreatedEvent::class, AppPasswordCreatedActivityListener::class); $context->registerEventListener(AppPasswordCreatedEvent::class, AppPasswordCreatedActivityListener::class);
$context->registerEventListener(UserAddedEvent::class, UserAddedToGroupActivityListener::class);
$context->registerEventListener(UserRemovedEvent::class, UserRemovedFromGroupActivityListener::class);
/** /**
* Core class wrappers * Core class wrappers
@ -129,27 +131,8 @@ class Application extends App implements IBootstrap {
public function boot(IBootContext $context): void { public function boot(IBootContext $context): void {
Util::connectHook('OC_User', 'post_setPassword', $this, 'onChangePassword'); Util::connectHook('OC_User', 'post_setPassword', $this, 'onChangePassword');
Util::connectHook('OC_User', 'changeUser', $this, 'onChangeInfo'); 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 * @param array $parameters
* @throws \InvalidArgumentException * @throws \InvalidArgumentException

View File

@ -27,11 +27,9 @@
namespace OCA\Settings; namespace OCA\Settings;
use OCA\Settings\Activity\GroupProvider;
use OCA\Settings\Activity\Provider; use OCA\Settings\Activity\Provider;
use OCP\Activity\IManager as IActivityManager; use OCP\Activity\IManager as IActivityManager;
use OCP\IConfig; use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use OCP\IUser; use OCP\IUser;
@ -214,78 +212,4 @@ class Hooks {
$this->mailer->send($message); $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);
}
}
} }

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}