Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Milan Jaros
nextcloud-forms
Commits
644d92e1
Unverified
Commit
644d92e1
authored
Mar 12, 2021
by
Jonas Rittershofer
Browse files
Delete Forms for deleted User
Signed-off-by:
Jonas Rittershofer
<
jotoeri@users.noreply.github.com
>
parent
82df2730
Changes
5
Hide whitespace changes
Inline
Side-by-side
lib/AppInfo/Application.php
View file @
644d92e1
...
...
@@ -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
);
}
}
lib/BackgroundJob/UserDeletedJob.php
0 → 100644
View file @
644d92e1
<?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
);
}
}
}
lib/Listener/UserDeletedListener.php
0 → 100644
View file @
644d92e1
<?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
()]);
}
}
tests/Unit/BackgroundJob/UserDeletedJobTest.php
0 → 100644
View file @
644d92e1
<?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'
]);
}
}
tests/Unit/Listener/UserDeletedListenerTest.php
0 → 100644
View file @
644d92e1
<?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
);
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment