fix: clean up after user was deleted

fix #80
pull/91/head
sualko 2020-09-24 11:05:51 +02:00
parent a81d97a6e5
commit dd99275f16
4 changed files with 98 additions and 10 deletions

View File

@ -12,12 +12,14 @@ use \OCA\BigBlueButton\Event\RoomCreatedEvent;
use \OCA\BigBlueButton\Event\RoomDeletedEvent; use \OCA\BigBlueButton\Event\RoomDeletedEvent;
use \OCA\BigBlueButton\Event\RoomShareCreatedEvent; use \OCA\BigBlueButton\Event\RoomShareCreatedEvent;
use \OCA\BigBlueButton\Event\RoomShareDeletedEvent; use \OCA\BigBlueButton\Event\RoomShareDeletedEvent;
use \OCA\BigBlueButton\Listener\UserDeletedListener;
use \OCA\BigBlueButton\Middleware\HookMiddleware; use \OCA\BigBlueButton\Middleware\HookMiddleware;
use \OCA\BigBlueButton\Middleware\JoinMiddleware; use \OCA\BigBlueButton\Middleware\JoinMiddleware;
use \OCP\AppFramework\App; use \OCP\AppFramework\App;
use \OCP\EventDispatcher\IEventDispatcher; use \OCP\EventDispatcher\IEventDispatcher;
use \OCP\IConfig; use \OCP\IConfig;
use \OCP\Settings\IManager as ISettingsManager; use \OCP\Settings\IManager as ISettingsManager;
use \OCP\User\Events\UserDeletedEvent;
if ((@include_once __DIR__ . '/../../vendor/autoload.php') === false) { if ((@include_once __DIR__ . '/../../vendor/autoload.php') === false) {
throw new \Exception('Cannot include autoload. Did you run install dependencies using composer?'); throw new \Exception('Cannot include autoload. Did you run install dependencies using composer?');
@ -31,17 +33,9 @@ class Application extends App {
$container = $this->getContainer(); $container = $this->getContainer();
/* @var IEventDispatcher $eventDispatcher */ /* @var IEventDispatcher $dispatcher */
$dispatcher = $container->query(IEventDispatcher::class); $dispatcher = $container->query(IEventDispatcher::class);
$dispatcher->addServiceListener(RoomCreatedEvent::class, RoomListener::class); $this->registerServiceListener($dispatcher);
$dispatcher->addServiceListener(RoomDeletedEvent::class, RoomListener::class);
$dispatcher->addServiceListener(RoomShareCreatedEvent::class, RoomShareListener::class);
$dispatcher->addServiceListener(RoomShareDeletedEvent::class, RoomShareListener::class);
$dispatcher->addServiceListener(MeetingStartedEvent::class, MeetingListener::class);
$dispatcher->addServiceListener(MeetingEndedEvent::class, MeetingListener::class);
$dispatcher->addServiceListener(RecordingReadyEvent::class, MeetingListener::class);
$container->registerMiddleWare(JoinMiddleware::class); $container->registerMiddleWare(JoinMiddleware::class);
$container->registerMiddleWare(HookMiddleware::class); $container->registerMiddleWare(HookMiddleware::class);
@ -74,4 +68,18 @@ class Application extends App {
]; ];
}); });
} }
private function registerServiceListener(IEventDispatcher $dispatcher) {
$dispatcher->addServiceListener(RoomCreatedEvent::class, RoomListener::class);
$dispatcher->addServiceListener(RoomDeletedEvent::class, RoomListener::class);
$dispatcher->addServiceListener(RoomShareCreatedEvent::class, RoomShareListener::class);
$dispatcher->addServiceListener(RoomShareDeletedEvent::class, RoomShareListener::class);
$dispatcher->addServiceListener(MeetingStartedEvent::class, MeetingListener::class);
$dispatcher->addServiceListener(MeetingEndedEvent::class, MeetingListener::class);
$dispatcher->addServiceListener(RecordingReadyEvent::class, MeetingListener::class);
$dispatcher->addServiceListener(UserDeletedEvent::class, UserDeletedListener::class);
}
} }

View File

@ -44,6 +44,18 @@ class RoomShareMapper extends QBMapper {
return $this->findEntity($qb); return $this->findEntity($qb);
} }
public function findByUserId(string $userId): array {
/* @var $qb IQueryBuilder */
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from('bbb_room_shares')
->where($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)))
->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(RoomShare::SHARE_TYPE_USER, IQueryBuilder::PARAM_INT)));
/** @var array<RoomShare> */
return $this->findEntities($qb);
}
/** /**
* @return array<RoomShare> * @return array<RoomShare>
*/ */

View File

@ -0,0 +1,64 @@
<?php
namespace OCA\BigBlueButton\Listener;
use OCA\BigBlueButton\Service\RoomService;
use OCA\BigBlueButton\Service\RoomShareService;
use OCP\Activity\IManager as IActivityManager;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\User\Events\UserDeletedEvent;
class UserDeletedListener implements IEventListener {
/** @var IActivityManager */
private $activityManager;
/** @var RoomService */
private $roomService;
/** @var RoomShareService */
private $shareService;
public function __construct(
IActivityManager $activityManager,
RoomService $roomService,
RoomShareService $shareService) {
$this->activityManager = $activityManager;
$this->roomService = $roomService;
$this->shareService = $shareService;
}
public function handle(Event $event): void {
if (!($event instanceof UserDeletedEvent)) {
return;
}
$userId = $event->getUser()->getUID();
$rooms = $this->roomService->findAll($userId, [], []);
$this->deleteSharesByUserId($userId);
foreach ($rooms as $room) {
$this->deleteSharesByRoomId($room->getId());
$this->roomService->delete($room->getId());
}
}
private function deleteSharesByRoomId(string $roomId) {
$shares = $this->shareService->findAll($roomId);
foreach ($shares as $share) {
$this->shareService->delete($share->getId());
}
}
private function deleteSharesByUserId(string $userId) {
$shares = $this->shareService->findByUserId($userId);
foreach ($shares as $share) {
$this->shareService->delete($share->getId());
}
}
}

View File

@ -32,6 +32,10 @@ class RoomShareService {
return $this->mapper->findAll($roomId); return $this->mapper->findAll($roomId);
} }
public function findByUserId(string $userId): array {
return $this->mapper->findByUserId($userId);
}
private function handleException(Exception $e): void { private function handleException(Exception $e): void {
if ($e instanceof DoesNotExistException || if ($e instanceof DoesNotExistException ||
$e instanceof MultipleObjectsReturnedException) { $e instanceof MultipleObjectsReturnedException) {