Skip to content
Snippets Groups Projects
Unverified Commit f5faf3f2 authored by Jonas's avatar Jonas Committed by GitHub
Browse files

Merge pull request #856 from nextcloud/enh/user_deleted_listener

Delete a deleted Users Forms
parents 600ce239 644d92e1
No related branches found
No related tags found
No related merge requests found
Pipeline #18711 failed
...@@ -28,7 +28,10 @@ declare(strict_types=1); ...@@ -28,7 +28,10 @@ declare(strict_types=1);
namespace OCA\Forms\AppInfo; namespace OCA\Forms\AppInfo;
use OCA\Forms\Listener\UserDeletedListener;
use OCP\AppFramework\App; use OCP\AppFramework\App;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\User\Events\UserDeletedEvent;
include_once __DIR__ . '/../../vendor/autoload.php'; include_once __DIR__ . '/../../vendor/autoload.php';
...@@ -41,5 +44,8 @@ class Application extends App { ...@@ -41,5 +44,8 @@ class Application extends App {
*/ */
public function __construct(array $urlParams = []) { public function __construct(array $urlParams = []) {
parent::__construct(self::APP_ID, $urlParams); parent::__construct(self::APP_ID, $urlParams);
$eventDispatcher = $this->getContainer()->query(IEventDispatcher::class);
$eventDispatcher->addServiceListener(UserDeletedEvent::class, UserDeletedListener::class);
} }
} }
<?php
/**
* @copyright Copyright (c) 2021 Jonas Rittershofer <jotoeri@users.noreply.github.com>
*
* @author Jonas Rittershofer <jotoeri@users.noreply.github.com>
*
* @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\Forms\BackgroundJob;
use OCA\Forms\Db\FormMapper;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\QueuedJob;
use OCP\ILogger;
class UserDeletedJob extends QueuedJob {
/** @var FormMapper */
private $formMapper;
/** @var ILogger */
private $logger;
public function __construct(FormMapper $formMapper,
ITimeFactory $time,
ILogger $logger) {
parent::__construct($time);
$this->formMapper = $formMapper;
$this->logger = $logger;
}
public function run($arguments) {
$ownerId = $arguments['owner_id'];
$this->logger->info('Deleting forms for deleted user {user}', [
'user' => $ownerId
]);
$forms = $this->formMapper->findAllByOwnerId($ownerId);
foreach ($forms as $form) {
$this->formMapper->deleteForm($form);
}
}
}
...@@ -430,10 +430,7 @@ class ApiController extends OCSController { ...@@ -430,10 +430,7 @@ class ApiController extends OCSController {
throw new OCSForbiddenException(); throw new OCSForbiddenException();
} }
// Delete Submissions(incl. Answers), Questions(incl. Options) and Form. $this->formMapper->deleteForm($form);
$this->submissionMapper->deleteByForm($id);
$this->questionMapper->deleteByForm($id);
$this->formMapper->delete($form);
return new DataResponse($id); return new DataResponse($id);
} }
......
...@@ -31,14 +31,23 @@ use OCP\IDBConnection; ...@@ -31,14 +31,23 @@ use OCP\IDBConnection;
use OCP\AppFramework\Db\QBMapper; use OCP\AppFramework\Db\QBMapper;
class FormMapper extends QBMapper { class FormMapper extends QBMapper {
/** @var QuestionMapper */
private $questionMapper;
/** @var SubmissionMapper */
private $submissionMapper;
/** /**
* FormMapper constructor. * FormMapper constructor.
* *
* @param IDBConnection $db * @param IDBConnection $db
*/ */
public function __construct(IDBConnection $db) { public function __construct(QuestionMapper $questionMapper,
SubmissionMapper $submissionMapper,
IDBConnection $db) {
parent::__construct($db, 'forms_v2_forms', Form::class); parent::__construct($db, 'forms_v2_forms', Form::class);
$this->questionMapper = $questionMapper;
$this->submissionMapper = $submissionMapper;
} }
/** /**
...@@ -107,4 +116,15 @@ class FormMapper extends QBMapper { ...@@ -107,4 +116,15 @@ class FormMapper extends QBMapper {
return $this->findEntities($qb); return $this->findEntities($qb);
} }
/**
* Delete a Form including connected Questions and Submissions
* @param Form $form The form instance to delete
*/
public function deleteForm(Form $form): void {
// Delete Submissions(incl. Answers), Questions(incl. Options) and Form.
$this->submissionMapper->deleteByForm($form->getId());
$this->questionMapper->deleteByForm($form->getId());
$this->delete($form);
}
} }
<?php
/**
* @copyright Copyright (c) 2021 Jonas Rittershofer <jotoeri@users.noreply.github.com>
*
* @author Jonas Rittershofer <jotoeri@users.noreply.github.com>
*
* @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\Forms\Listener;
use OCA\Forms\BackgroundJob\UserDeletedJob;
use OCP\BackgroundJob\IJobList;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\ILogger;
use OCP\User\Events\UserDeletedEvent;
class UserDeletedListener implements IEventListener {
/** @var IJobList */
private $jobList;
/** @var ILogger */
private $logger;
public function __construct(IJobList $jobList,
ILogger $logger) {
$this->jobList = $jobList;
$this->logger = $logger;
}
public function handle(Event $event): void {
if (!($event instanceof UserDeletedEvent)) {
return;
}
// Set a Cron-Job to delete the Users Forms.
$this->jobList->add(UserDeletedJob::class, ['owner_id' => $event->getUser()->getUID()]);
}
}
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2021 Jonas Rittershofer <jotoeri@users.noreply.github.com>
*
* @author Jonas Rittershofer <jotoeri@users.noreply.github.com>
*
* @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\Forms\Tests\Unit\BackgroundJob;
use OCA\Forms\BackgroundJob\UserDeletedJob;
use OCA\Forms\Db\Form;
use OCA\Forms\Db\FormMapper;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\ILogger;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class UserDeletedJobTest extends TestCase {
/** @var UserDeletedJob */
private $userDeletedJob;
/** @var FormMapper|MockObject */
private $formMapper;
/** @var ILogger|MockObject */
private $logger;
public function setUp(): void {
$this->formMapper = $this->createMock(FormMapper::class);
$time = $this->createMock(ITimeFactory::class);
$this->logger = $this->createMock(ILogger::class);
$this->userDeletedJob = new UserDeletedJob($this->formMapper, $time, $this->logger);
}
public function testHandle() {
$form = $this->createMock(Form::class);
$this->formMapper->expects($this->once())
->method('findAllByOwnerId')
->willReturn([$form, $form, $form]);
$this->formMapper->expects($this->exactly(3))
->method('deleteForm')
->with($form);
$this->userDeletedJob->run(['owner_id' => 'someUser']);
}
}
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2021 Jonas Rittershofer <jotoeri@users.noreply.github.com>
*
* @author Jonas Rittershofer <jotoeri@users.noreply.github.com>
*
* @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\Forms\Tests\Unit\Listener;
use OCA\Forms\Listener\UserDeletedListener;
use OCA\Forms\BackgroundJob\UserDeletedJob;
use OCP\BackgroundJob\IJobList;
use OCP\ILogger;
use OCP\IUser;
use OCP\User\Events\UserCreatedEvent;
use OCP\User\Events\UserDeletedEvent;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class UserDeletedListenerTest extends TestCase {
/** @var UserDeletedListener */
private $userDeletedListener;
/** @var IJobList|MockObject */
private $jobList;
/** @var ILogger|MockObject */
private $logger;
public function setUp(): void {
$this->jobList = $this->createMock(IJobList::class);
$this->logger = $this->createMock(ILogger::class);
$this->userDeletedListener = new UserDeletedListener($this->jobList, $this->logger);
}
public function testHandle() {
$user = $this->createMock(IUser::class);
$user->expects($this->once())
->method('getUID')
->willReturn('someUser');
$event = $this->createMock(UserDeletedEvent::class);
$event->expects($this->once())
->method('getUser')
->willReturn($user);
$this->jobList->expects($this->once())
->method('add')
->with(UserDeletedJob::class, ['owner_id' => 'someUser']);
$this->userDeletedListener->handle($event);
}
public function testWrongEvent() {
$event = $this->createMock(UserCreatedEvent::class);
$this->jobList->expects($this->never())
->method('add');
$this->userDeletedListener->handle($event);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment