Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add listener for GroupDeletedEvent to drop this group as collaborator
Signed-off-by: Simon Spannagel <[email protected]>
  • Loading branch information
Simon Spannagel committed Jan 24, 2023
commit 302bb3f0065597aa6d6f5ea8454d776b8308fce9
5 changes: 5 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@
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';
Expand Down Expand Up @@ -71,6 +74,8 @@ public function register(IRegistrationContext $context): void {

$context->registerEventListener(UserRemovedEvent::class, GroupUserRemovedListener::class);

$context->registerEventListener(GroupDeletedEvent::class, GroupDeletedListener::class);

$context->registerEventListener(SabrePluginAuthInitEvent::class, SabrePluginAuthInitListener::class);
}

Expand Down
48 changes: 48 additions & 0 deletions lib/Listener/GroupDeletedListener.php
Original file line number Diff line number Diff line change
@@ -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());
}
}
}