Skip to content
Snippets Groups Projects
Unverified Commit 644d92e1 authored by Jonas Rittershofer's avatar Jonas Rittershofer
Browse files

Delete Forms for deleted User

parent 82df2730
No related branches found
No related tags found
No related merge requests found
......@@ -28,7 +28,10 @@ declare(strict_types=1);
namespace OCA\Forms\AppInfo;
use OCA\Forms\Listener\UserDeletedListener;
use OCP\AppFramework\App;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\User\Events\UserDeletedEvent;
include_once __DIR__ . '/../../vendor/autoload.php';
......@@ -41,5 +44,8 @@ class Application extends App {
*/
public function __construct(array $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);
}
}
}
<?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