-
Notifications
You must be signed in to change notification settings - Fork 89
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
User moderation: Delete a user's records when blocking them #1450
Merged
zzacharo
merged 1 commit into
inveniosoftware:master
from
max-moser:mm/delete-records-when-blocking-users
Sep 11, 2023
+114
−5
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,101 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2023 CERN. | ||
# Copyright (C) 2023 TU Wien. | ||
# | ||
# Invenio-RDM-Records is free software; you can redistribute it and/or modify | ||
# it under the terms of the MIT License; see LICENSE file for more details. | ||
"""RDM user moderation action.""" | ||
|
||
from invenio_access.permissions import system_identity | ||
from invenio_pidstore.errors import PIDDoesNotExistError | ||
from invenio_vocabularies.proxies import current_service | ||
|
||
from invenio_rdm_records.proxies import current_rdm_records_service | ||
from ...proxies import current_rdm_records_service | ||
from ...records.systemfields.deletion_status import RecordDeletionStatusEnum | ||
|
||
|
||
def _get_records_for_user(user_id): | ||
"""Helper function for getting all the records of the user. | ||
|
||
Note: This function performs DB queries yielding all records for a given | ||
user (which is not hard-limited in the system) and performs service calls | ||
on each of them. Thus, this function has the potential of being a very | ||
heavy operation, and should not be called as part of the handling of an | ||
HTTP request! | ||
""" | ||
record_cls = current_rdm_records_service.record_cls | ||
model_cls = record_cls.model_cls | ||
parent_cls = record_cls.parent_record_cls | ||
parent_model_cls = parent_cls.model_cls | ||
|
||
# get all the parent records owned by the blocked user | ||
parent_recs = [ | ||
parent_cls(m.data, model=m) | ||
for m in parent_model_cls.query.filter( | ||
parent_model_cls.json["access"]["owned_by"]["user"].as_string() == user_id | ||
).all() | ||
] | ||
|
||
# get all child records of the chosen parent records | ||
recs = [ | ||
record_cls(m.data, model=m) | ||
for m in model_cls.query.filter( | ||
model_cls.parent_id.in_([p.id for p in parent_recs]) | ||
).all() | ||
] | ||
|
||
return recs | ||
|
||
|
||
def on_block(user_id, uow=None, **kwargs): | ||
"""Removes records that belong to a user.""" | ||
pass | ||
"""Removes records that belong to a user. | ||
|
||
Note: This function operates on all records of a user and thus has the potential | ||
to be a very heavy operation! Thus it should not be called as part of the handling | ||
of an HTTP request! | ||
""" | ||
user_id = str(user_id) | ||
tombstone_data = {"note": "User was blocked"} | ||
|
||
# set the removal reason if the vocabulary item exists | ||
try: | ||
removal_reason_id = kwargs.get("removal_reason_id", "misconduct") | ||
vocab = current_service.read( | ||
identity=system_identity, id_=("removalreasons", removal_reason_id) | ||
) | ||
tombstone_data["removal_reason"] = {"id": vocab.id} | ||
except PIDDoesNotExistError: | ||
pass | ||
|
||
# soft-delete all the published records of that user | ||
for rec in _get_records_for_user(user_id): | ||
if not rec.deletion_status.is_deleted: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated the docstring |
||
current_rdm_records_service.delete_record( | ||
system_identity, | ||
rec.pid.pid_value, | ||
tombstone_data, | ||
uow=uow, | ||
) | ||
|
||
|
||
def on_restore(user_id, uow=None, **kwargs): | ||
"""Restores records that belong to a user.""" | ||
pass | ||
"""Restores records that belong to a user. | ||
|
||
Note: This function operates on all records of a user and thus has the potential | ||
to be a very heavy operation! Thus it should not be called as part of the handling | ||
of an HTTP request! | ||
""" | ||
user_id = str(user_id) | ||
|
||
# restore all the deleted records of that user | ||
for rec in _get_records_for_user(user_id): | ||
if rec.deletion_status == RecordDeletionStatusEnum.DELETED: | ||
current_rdm_records_service.restore_record( | ||
system_identity, | ||
rec.pid.pid_value, | ||
uow=uow, | ||
) | ||
|
||
|
||
def on_approve(user_id, uow=None, **kwargs): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these can be potentially costly queries to run, is this action done in celery task ? ping @slint @zzacharo @alejandromumo - not sure if that was discussed during design phase
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, in the user's service, we kick off a celery task and execute all the actions inside it.
We can leave a comment here (e.g.
this is meant to be used ...
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the docstring to reflect this!