diff options
author | Simon Spannagel <simonspa@kth.se> | 2023-01-22 16:38:36 +0100 |
---|---|---|
committer | Simon Spannagel <simonspa@kth.se> | 2023-01-24 13:59:21 +0100 |
commit | 302bb3f0065597aa6d6f5ea8454d776b8308fce9 (patch) | |
tree | b4a1ec8d3a21bd08608f3101cb02f068b50017f2 /lib | |
parent | 7e711aa1023726cfa660e9d4d9a65b7519f0b631 (diff) |
Add listener for GroupDeletedEvent to drop this group as collaborator
Signed-off-by: Simon Spannagel <simonspa@kth.se>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/AppInfo/Application.php | 5 | ||||
-rw-r--r-- | lib/Listener/GroupDeletedListener.php | 48 |
2 files changed, 53 insertions, 0 deletions
diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 64e34826..11507922 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -29,12 +29,15 @@ use OCA\DAV\Events\SabrePluginAuthInitEvent; use OCA\Photos\Listener\SabrePluginAuthInitListener; use OCA\DAV\Connector\Sabre\Principal; use OCA\Photos\Listener\NodeDeletedListener; +use OCA\Photos\Listener\GroupUserRemovedListener; +use OCA\Photos\Listener\GroupDeletedListener; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\Files\Events\Node\NodeDeletedEvent; use OCP\Group\Events\UserRemovedEvent; +use OCP\Group\Events\GroupDeletedEvent; class Application extends App implements IBootstrap { public const APP_ID = 'photos'; @@ -71,6 +74,8 @@ class Application extends App implements IBootstrap { $context->registerEventListener(UserRemovedEvent::class, GroupUserRemovedListener::class); + $context->registerEventListener(GroupDeletedEvent::class, GroupDeletedListener::class); + $context->registerEventListener(SabrePluginAuthInitEvent::class, SabrePluginAuthInitListener::class); } diff --git a/lib/Listener/GroupDeletedListener.php b/lib/Listener/GroupDeletedListener.php new file mode 100644 index 00000000..34d4bb3c --- /dev/null +++ b/lib/Listener/GroupDeletedListener.php @@ -0,0 +1,48 @@ +<?php + +namespace OCA\Photos\Listener; + +use OCA\Photos\Album\AlbumMapper; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Group\Events\GroupDeletedEvent; + +class GroupDeletedListener implements IEventListener { + private AlbumMapper $albumMapper; + + public function __construct(AlbumMapper $albumMapper) { + $this->albumMapper = $albumMapper; + } + + public function handle(Event $event): void { + if (!($event instanceof GroupDeletedEvent)) { + return; + } + + // Get all shared albums for this group: + $albums_group = $this->albumMapper->getSharedAlbumsForCollaboratorWithFiles($event->getGroup()->getGID(), AlbumMapper::TYPE_GROUP); + + // Get all users of this group: + $users = $event->getGroup()->getUsers(); + + foreach ($users as $user) { + $uid = $user->getUID(); + + // Get all albums shared with this specific user: + $albums_user = $this->albumMapper->getSharedAlbumsForCollaboratorWithFiles($user->getUID(), AlbumMapper::TYPE_USER); + + // Get all group-shared albums that are not directly shared with the removed user in addition + $albums = array_udiff($albums_group, $albums_user, fn ($a, $b) => ($a->getAlbum()->getId() - $b->getAlbum()->getId())); + + + // Remove their photos from theses albums: + foreach ($albums as $album) { + $this->albumMapper->removeFilesForUser($album->getAlbum()->getId(), $user->getUID()); + } + } + + foreach ($albums_group as $album) { + $this->albumMapper->deleteGroupFromAlbumCollaboratorsList($event->getGroup()->getGID(), $album->getAlbum()->getId()); + } + } +} |