Handle circle deletion event and add cleanup job

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl
2022-02-24 14:07:51 +01:00
parent b42076ccb8
commit 76fd901062
9 changed files with 150 additions and 11 deletions

View File

@@ -41,6 +41,11 @@
<job>OCA\Deck\Cron\ScheduledNotifications</job>
<job>OCA\Deck\Cron\CardDescriptionActivity</job>
</background-jobs>
<repair-steps>
<live-migration>
<step>OCA\Deck\Migration\DeletedCircleCleanup</step>
</live-migration>
</repair-steps>
<commands>
<command>OCA\Deck\Command\UserExport</command>
<command>OCA\Deck\Command\BoardImport</command>

View File

@@ -26,6 +26,7 @@ namespace OCA\Deck\AppInfo;
use Closure;
use Exception;
use OC\EventDispatcher\SymfonyAdapter;
use OCA\Circles\Events\CircleDestroyedEvent;
use OCA\Deck\Activity\CommentEventHandler;
use OCA\Deck\Capabilities;
use OCA\Deck\Collaboration\Resources\ResourceProvider;
@@ -43,7 +44,9 @@ use OCA\Deck\Event\CardCreatedEvent;
use OCA\Deck\Event\CardDeletedEvent;
use OCA\Deck\Event\CardUpdatedEvent;
use OCA\Deck\Listeners\BeforeTemplateRenderedListener;
use OCA\Deck\Listeners\CircleEventListener;
use OCA\Deck\Listeners\FullTextSearchEventListener;
use OCA\Deck\Listeners\ResourceListener;
use OCA\Deck\Middleware\DefaultBoardMiddleware;
use OCA\Deck\Middleware\ExceptionMiddleware;
use OCA\Deck\Notification\Notifier;
@@ -143,6 +146,12 @@ class Application extends App implements IBootstrap {
$context->registerEventListener(AclCreatedEvent::class, FullTextSearchEventListener::class);
$context->registerEventListener(AclUpdatedEvent::class, FullTextSearchEventListener::class);
$context->registerEventListener(AclDeletedEvent::class, FullTextSearchEventListener::class);
// Handling cache invalidation for collections
$context->registerEventListener(AclCreatedEvent::class, ResourceListener::class);
$context->registerEventListener(AclDeletedEvent::class, ResourceListener::class);
$context->registerEventListener(CircleDestroyedEvent::class, CircleEventListener::class);
}
public function registerNotifications(NotificationManager $notificationManager): void {

View File

@@ -110,4 +110,12 @@ class AclMapper extends DeckMapper implements IPermissionMapper {
->andWhere($qb->expr()->eq('board_id', $qb->createNamedParameter($boardId, IQueryBuilder::PARAM_INT)));
$qb->executeStatement();
}
public function findByType(int $type): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from('deck_board_acl')
->where($qb->expr()->eq('type', $qb->createNamedParameter($type, IQueryBuilder::PARAM_INT)));
return $this->findEntities($qb);
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace OCA\Deck\Listeners;
use OCA\Circles\Events\CircleDestroyedEvent;
use OCA\Deck\Db\Acl;
use OCA\Deck\Db\AclMapper;
use OCA\Deck\Event\AclDeletedEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\EventDispatcher\IEventListener;
class CircleEventListener implements IEventListener {
/** @var AclMapper */
private $aclMapper;
/** @var IEventDispatcher */
private $eventDispatcher;
public function __construct(AclMapper $aclMapper, IEventDispatcher $eventDispatcher) {
$this->aclMapper = $aclMapper;
$this->eventDispatcher = $eventDispatcher;
}
public function handle(Event $event): void {
if ($event instanceof CircleDestroyedEvent) {
$circleId = $event->getCircle()->getSingleId();
$acls = $this->aclMapper->findByParticipant(Acl::PERMISSION_TYPE_CIRCLE, $circleId);
foreach ($acls as $acl) {
$this->aclMapper->delete($acl);
$this->eventDispatcher->dispatchTyped(new AclDeletedEvent($acl));
}
}
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace OCA\Deck\Listeners;
use OCA\Deck\Collaboration\Resources\ResourceProvider;
use OCA\Deck\Collaboration\Resources\ResourceProviderCard;
use OCA\Deck\Event\AclCreatedEvent;
use OCA\Deck\Event\AclDeletedEvent;
use OCP\Collaboration\Resources\IManager;
use OCP\Collaboration\Resources\ResourceException;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
class ResourceListener implements IEventListener {
/** @var IManager */
private $resourceManager;
/** @var ResourceProviderCard */
private $resourceProviderCard;
public function __construct(IManager $resourceManager, ResourceProviderCard $resourceProviderCard) {
$this->resourceManager = $resourceManager;
$this->resourceProviderCard = $resourceProviderCard;
}
public function handle(Event $event): void {
if (!$event instanceof AclDeletedEvent && !$event instanceof AclCreatedEvent) {
return;
}
$boardId = $event->getAcl()->getBoardId();
$this->resourceManager->invalidateAccessCacheForProvider($this->resourceProviderCard);
try {
$resource = $this->resourceManager->getResourceForUser(ResourceProvider::RESOURCE_TYPE, $boardId, null);
$this->resourceManager->invalidateAccessCacheForResource($resource);
} catch (ResourceException $e) {
// If there is no resource we don't need to invalidate anything, but this should not happen anyways
}
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace OCA\Deck\Migration;
use OCA\Deck\Db\Acl;
use OCA\Deck\Db\AclMapper;
use OCA\Deck\Service\CirclesService;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
class DeletedCircleCleanup implements IRepairStep {
private AclMapper $aclMapper;
private CirclesService $circleService;
public function __construct(AclMapper $aclMapper, CirclesService $circlesService) {
$this->aclMapper = $aclMapper;
$this->circleService = $circlesService;
}
public function getName() {
return 'Cleanup Deck ACL entries for circles which have been already deleted';
}
public function run(IOutput $output) {
if (!$this->circleService->isCirclesEnabled()) {
return;
}
foreach ($this->aclMapper->findByType(Acl::PERMISSION_TYPE_CIRCLE) as $acl) {
if ($this->circleService->getCircle($acl->getParticipant()) === null) {
$this->aclMapper->delete($acl);
$output->info('Removed circle with id ' . $acl->getParticipant());
}
}
}
}

View File

@@ -116,12 +116,6 @@
<code>$schemaClosure</code>
</MoreSpecificImplementedParamType>
</file>
<file src="lib/Service/AssignmentService.php">
<InvalidScalarArgument occurrences="2">
<code>$cardId</code>
<code>$cardId</code>
</InvalidScalarArgument>
</file>
<file src="lib/Service/AttachmentService.php">
<InvalidCatch occurrences="1"/>
</file>
@@ -142,11 +136,6 @@
<code>is_resource($content)</code>
</RedundantCondition>
</file>
<file src="lib/Service/StackService.php">
<UndefinedClass occurrences="1">
<code>BadRquestException</code>
</UndefinedClass>
</file>
<file src="lib/Sharing/Listener.php">
<InvalidArgument occurrences="1">
<code>[self::class, 'listenPreShare']</code>

View File

@@ -59,6 +59,12 @@ namespace OCA\Circles\Model {
}
}
namespace OCA\Circles\Events {
class CircleDestroyedEvent extends \OCP\EventDispatcher\Event {
public function __construct(FederatedEvent $federatedEvent, array $results) {}
abstract public function getCircle(): \OCA\Circles\Model\Circle {}
}
}
namespace OCA\Circles\Model\Probes {
class CircleProbe {
public function __construct() {}

View File

@@ -35,6 +35,8 @@ use OCA\Deck\Db\CardMapper;
use OCA\Deck\Db\ChangeHelper;
use OCA\Deck\Db\LabelMapper;
use OCA\Deck\Db\StackMapper;
use OCA\Deck\Event\AclCreatedEvent;
use OCA\Deck\Event\AclDeletedEvent;
use OCA\Deck\NoPermissionException;
use OCA\Deck\Notification\NotificationHelper;
use OCP\EventDispatcher\IEventDispatcher;
@@ -375,6 +377,9 @@ class BoardServiceTest extends TestCase {
->method('insert')
->with($acl)
->willReturn($acl);
$this->eventDispatcher->expects(self::once())
->method('dispatchTyped')
->with(new AclCreatedEvent($acl));
$this->assertEquals($expected, $this->service->addAcl(
123, 'user', 'admin', $providedAcl[0], $providedAcl[1], $providedAcl[2]
));
@@ -432,6 +437,9 @@ class BoardServiceTest extends TestCase {
->method('delete')
->with($acl)
->willReturn($acl);
$this->eventDispatcher->expects(self::once())
->method('dispatchTyped')
->with(new AclDeletedEvent($acl));
$this->assertEquals($acl, $this->service->deleteAcl(123));
}
}