Signed-off-by: Jonas Rittershofer <jotoeri@users.noreply.github.com>
This commit is contained in:
Jonas Rittershofer 2021-03-12 01:52:16 +01:00
Родитель 82df27304d
Коммит 644d92e149
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: A865740F334316E0
5 изменённых файлов: 262 добавлений и 0 удалений

Просмотреть файл

@ -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);
}
}

Просмотреть файл

@ -0,0 +1,59 @@
<?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);
}
}
}

Просмотреть файл

@ -0,0 +1,55 @@
<?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()]);
}
}

Просмотреть файл

@ -0,0 +1,64 @@
<?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']);
}
}

Просмотреть файл

@ -0,0 +1,78 @@
<?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);
}
}