Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete poll permanently #823

Merged
merged 7 commits into from
Mar 2, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
['name' => 'poll#list', 'url' => '/polls/list/', 'verb' => 'GET'],
['name' => 'poll#get', 'url' => '/polls/get/{pollId}', 'verb' => 'GET'],
['name' => 'poll#delete', 'url' => '/polls/delete/{pollId}', 'verb' => 'GET'],
['name' => 'poll#deletePermanently', 'url' => '/polls/delete/permanent/{pollId}', 'verb' => 'GET'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for not using the DELETE verb?

Suggested change
['name' => 'poll#deletePermanently', 'url' => '/polls/delete/permanent/{pollId}', 'verb' => 'GET'],
['name' => 'poll#deletePermanently', 'url' => '/polls/delete/permanent/{pollId}', 'verb' => 'DELETE'],

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Itried it, but it didn't work. Thus I thought this isn't supported in NC

Copy link
Contributor

@splitt3r splitt3r Feb 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should 😁 a quick research gave me:

https://github.com/nextcloud/spreed/blob/master/appinfo/routes.php#L274-L282

...
[
		'name' => 'Room#leaveRoom',
		'url' => '/api/{apiVersion}/room/{token}/participants/active',
		'verb' => 'DELETE',
		...
]
...

https://github.com/nextcloud/spreed/blob/master/src/services/participantsService.js#L75

...
const leaveConversationSync = function(token) {
	axios.delete(generateOcsUrl('apps/spreed/api/v1/room', 2) + token + '/participants/active')
}
...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some strange reason it worked fine this time 😕 Maybe I did something wrong last time. Will push a fix when at my dev machine.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI. Building works well on windows machines with some tools. Only signing is an issue.

['name' => 'poll#write', 'url' => '/polls/write/', 'verb' => 'POST'],
['name' => 'poll#clone', 'url' => '/polls/clone/{pollId}', 'verb' => 'get'],
['name' => 'poll#getByToken', 'url' => '/polls/get/s/{token}', 'verb' => 'GET'],
Expand Down
32 changes: 32 additions & 0 deletions lib/Controller/PollController.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,38 @@ public function delete($pollId) {
}
}

/**
* deletePermanently
* @NoAdminRequired
* @param Array $poll
* @return DataResponse
*/

public function deletePermanently($pollId) {

try {
// Find existing poll
$this->poll = $this->pollMapper->find($pollId);
$this->acl->setPollId($this->poll->getId());

if (!$this->acl->getAllowEdit()) {
$this->logger->alert('Unauthorized delete attempt from user ' . $this->userId);
return new DataResponse(['message' => 'Unauthorized write attempt.'], Http::STATUS_UNAUTHORIZED);
}

if (!$this->poll->getDeleted()) {
$this->logger->alert('user ' . $this->userId . ' trying to permanently delete active poll');
return new DataResponse(['message' => 'Permanent deletion of active poll.'], Http::STATUS_CONFLICT);
}

$this->pollMapper->delete($this->poll);
return new DataResponse([], Http::STATUS_OK);

} catch (Exception $e) {
return new DataResponse($e, Http::STATUS_NOT_FOUND);
}
}

/**
* write
* @NoAdminRequired
Expand Down
118 changes: 118 additions & 0 deletions lib/Migration/Version0104Date20200205104800.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
/**
* @copyright Copyright (c) 2017 René Gieling <github@dartcafe.de>
*
* @author René Gieling <github@dartcafe.de>
*
* @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\Polls\Migration;

use OCP\DB\ISchemaWrapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;

/**
* Installation class for the polls app.
* Initial db creation
*/
class Version0104Date20200205104800 extends SimpleMigrationStep {

/** @var IDBConnection */
protected $connection;

/** @var IConfig */
protected $config;

/** @var array */
protected $childTables = [
'polls_comments',
'polls_log',
'polls_notif',
'polls_options',
'polls_share',
'polls_votes',
];

/**
* @param IDBConnection $connection
* @param IConfig $config
*/
public function __construct(IDBConnection $connection, IConfig $config) {
$this->connection = $connection;
$this->config = $config;
}


/**
* @param IOutput $output
* @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null
* @since 13.0.0
*/
public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
// delete all orphaned entries by selecting all rows
// those poll_ids are not present in the polls table
//
// we have to use a raw query, because NOT EXISTS is not
// part of doctrine's expression builder
//
// get table prefix, as we are running a raw query
$prefix = $this->config->getSystemValue('dbtableprefix', 'oc_');
// check for orphaned entries in all tables referencing
// the main polls table
foreach($this->childTables as $tbl) {
$child = "$prefix$tbl";
$query = "DELETE
FROM $child
WHERE NOT EXISTS (
SELECT NULL
FROM {$prefix}polls_polls polls
WHERE polls.id = {$child}.poll_id
)";
$stmt = $this->connection->prepare($query);
$stmt->execute();
}
}

/**
* @param IOutput $output
* @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
* @since 13.0.0
*/
public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) {
// add an on delete fk contraint to all tables referencing the main polls table
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

$eventTable = $schema->getTable('polls_polls');
foreach($this->childTables as $tbl) {
$table = $schema->getTable($tbl);

$table->addForeignKeyConstraint($eventTable, ['poll_id'], ['id'], ['onDelete' => 'CASCADE']);
}

return $schema;
}
}
32 changes: 26 additions & 6 deletions src/js/components/Navigation/Navigation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,47 +29,53 @@
icon="icon-folder" :to="{ name: 'list', params: {type: 'all'}}" :open="true">
<ul>
<PollNavigationItems v-for="(poll) in allPolls" :key="poll.id" :poll="poll"
@switchDeleted="switchDeleted(poll.id)" @clonePoll="clonePoll(poll.id)" />
@switchDeleted="switchDeleted(poll.id)" @clonePoll="clonePoll(poll.id)"
@deletePermanently="deletePermanently(poll.id)" />
</ul>
</AppNavigationItem>

<AppNavigationItem :title="t('polls', 'My polls')" :allow-collapse="true"
icon="icon-user" :to="{ name: 'list', params: {type: 'my'}}" :open="false">
<ul>
<PollNavigationItems v-for="(poll) in myPolls" :key="poll.id" :poll="poll"
@switchDeleted="switchDeleted(poll.id)" @clonePoll="clonePoll(poll.id)" />
@switchDeleted="switchDeleted(poll.id)" @clonePoll="clonePoll(poll.id)"
@deletePermanently="deletePermanently(poll.id)" />
</ul>
</AppNavigationItem>

<AppNavigationItem :title="t('polls', 'Participated')" :allow-collapse="true"
icon="icon-user" :to="{ name: 'list', params: {type: 'participated'}}" :open="false">
<ul>
<PollNavigationItems v-for="(poll) in participatedPolls" :key="poll.id" :poll="poll"
@switchDeleted="switchDeleted(poll.id)" @clonePoll="clonePoll(poll.id)" />
@switchDeleted="switchDeleted(poll.id)" @clonePoll="clonePoll(poll.id)"
@deletePermanently="deletePermanently(poll.id)" />
</ul>
</AppNavigationItem>

<AppNavigationItem :title="t('polls', 'Public polls')" :allow-collapse="true"
icon="icon-link" :to="{ name: 'list', params: {type: 'public'}}" :open="false">
<ul>
<PollNavigationItems v-for="(poll) in publicPolls" :key="poll.id" :poll="poll"
@switchDeleted="switchDeleted(poll.id)" @clonePoll="clonePoll(poll.id)" />
@switchDeleted="switchDeleted(poll.id)" @clonePoll="clonePoll(poll.id)"
@deletePermanently="deletePermanently(poll.id)" />
</ul>
</AppNavigationItem>

<AppNavigationItem :title="t('polls', 'Hidden polls')" :allow-collapse="true"
icon="icon-password" :to="{ name: 'list', params: {type: 'hidden'}}" :open="false">
<ul>
<PollNavigationItems v-for="(poll) in hiddenPolls" :key="poll.id" :poll="poll"
@switchDeleted="switchDeleted(poll.id)" @clonePoll="clonePoll(poll.id)" />
@switchDeleted="switchDeleted(poll.id)" @clonePoll="clonePoll(poll.id)"
@deletePermanently="deletePermanently(poll.id)" />
</ul>
</AppNavigationItem>

<AppNavigationItem :title="t('polls', 'Deleted polls')" :allow-collapse="true"
icon="icon-delete" :to="{ name: 'list', params: {type: 'deleted'}}" :open="false">
<ul>
<PollNavigationItems v-for="(poll) in deletedPolls" :key="poll.id" :poll="poll"
@switchDeleted="switchDeleted(poll.id)" @clonePoll="clonePoll(poll.id)" />
@switchDeleted="switchDeleted(poll.id)" @clonePoll="clonePoll(poll.id)"
@deletePermanently="deletePermanently(poll.id)" />
</ul>
</AppNavigationItem>
</ul>
Expand Down Expand Up @@ -164,6 +170,20 @@ export default {

},

deletePermanently(pollId) {
this.$store
.dispatch('deletePermanently', { pollId: pollId })
.then((response) => {
// if we permanently delete current selected poll,
// reload deleted polls route
if(this.$route.params.id && this.$route.params.id == pollId) {
this.$router.push({name: 'list', params: {type: 'deleted'}})
}
this.refreshPolls()
})

},

refreshPolls() {
if (this.$route.name !== 'publicVote') {

Expand Down
4 changes: 4 additions & 0 deletions src/js/components/Navigation/PollNavigationItems.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
<ActionButton v-if="poll.allowEdit && poll.deleted" icon="icon-history" @click="$emit('switchDeleted')">
{{ (poll.isAdmin) ? t('polls', 'Restore poll as admin') : t('polls', 'Restore poll') }}
</ActionButton>

<ActionButton v-if="poll.allowEdit && poll.deleted" icon="icon-delete" class="danger" @click="$emit('deletePermanently')">
{{ (poll.isAdmin) ? t('polls', 'Delete poll permanently as admin') : t('polls', 'Delete poll permanently') }}
</ActionButton>
</template>
</AppNavigationItem>
</template>
Expand Down
12 changes: 12 additions & 0 deletions src/js/components/PollList/PollListItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
<ActionButton v-if="poll.allowEdit && poll.deleted" icon="icon-history" @click="switchDeleted()">
{{ (poll.isAdmin) ? t('polls', 'Restore poll as admin') : t('polls', 'Restore poll') }}
</ActionButton>

<ActionButton v-if="poll.allowEdit && poll.deleted" icon="icon-delete" class="danger" @click="deletePermanently()">
{{ (poll.isAdmin) ? t('polls', 'Delete poll permanently as admin') : t('polls', 'Delete poll permanently') }}
</ActionButton>
</Actions>

<div v-tooltip.auto="accessType" class="thumbnail access" :class="poll.access">
Expand Down Expand Up @@ -183,6 +187,14 @@ export default {
this.hideMenu()
},

deletePermanently() {
this.$store.dispatch('deletePermanently', { pollId: this.poll.id })
.then((response) => {
this.refreshPolls()
})
this.hideMenu()
},

clonePoll() {
this.$store.dispatch('clonePoll', { pollId: this.poll.id })
.then((response) => {
Expand Down
13 changes: 13 additions & 0 deletions src/js/components/SideBar/SideBarTabConfiguration.vue
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@
<span v-if="poll.deleted">{{ t('polls', 'Restore poll') }}</span>
<span v-else>{{ t('polls', 'Delete poll') }}</span>
</button>
<button v-if="poll.deleted" class="button btn error" @click="deletePermanently()">
{{ t('polls', 'Delete poll permanently') }}
</button>
</div>
</template>

Expand Down Expand Up @@ -270,6 +273,16 @@ export default {

},

deletePermanently() {
if(!this.poll.deleted) return;

this.$store
.dispatch('deletePermanently', { pollId: this.poll.id })
.then((response) => {
this.$router.push({name: 'list', params: {type: 'deleted'}})
})
},

writePoll() {
if (this.titleEmpty) {
OC.Notification.showTemporary(t('polls', 'Title must not be empty!'), { type: 'success' })
Expand Down
12 changes: 12 additions & 0 deletions src/js/store/modules/polls.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ const actions = {
})
},

deletePermanently(context, payload) {
const endPoint = 'apps/polls/polls/delete/permanent/'
return axios.get(OC.generateUrl(endPoint + payload.pollId))
.then((response) => {
OC.Notification.showTemporary(t('polls', 'Deleted poll permanently.'), { type: 'success' })
return response
}, (error) => {
OC.Notification.showTemporary(t('polls', 'Error deleting poll.'), { type: 'error' })
console.error('Error deleting poll', { error: error.response }, { payload: payload })
})
},

clonePoll(context, payload) {
const endPoint = 'apps/polls/polls/clone/'
return axios.get(OC.generateUrl(endPoint + payload.pollId))
Expand Down