-
Notifications
You must be signed in to change notification settings - Fork 116
feat(auditlog): Add audit logs for file create/delete #2303
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,3 +38,12 @@ def __call__(self, data, **kwargs): | |
| "type": "draft" if isinstance(resource, RDMDraft) else "record", | ||
| } | ||
| dict_set(data, "metadata.triggered_by", triggered_by) | ||
|
|
||
|
|
||
| class FileContext(object): | ||
| """Payload generator for setting file data.""" | ||
|
|
||
| def __call__(self, data, **kwargs): | ||
| """Update data with file data.""" | ||
| file_key = kwargs.get("file_key", None) | ||
|
Contributor
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. nit; the marshmallow schema is required, but here it can possibly be None |
||
| dict_set(data, "metadata.file_key", file_key) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,8 +7,14 @@ | |
|
|
||
| """File Service API.""" | ||
|
|
||
| from invenio_audit_logs.services.uow import AuditLogOp | ||
| from invenio_records_resources.services import FileService | ||
| from invenio_records_resources.services.uow import unit_of_work | ||
|
|
||
| from invenio_rdm_records.auditlog.actions import ( | ||
| FileCreateAuditLog, | ||
| FileDeleteAuditLog, | ||
| ) | ||
| from invenio_rdm_records.services.errors import RecordDeletedException | ||
|
|
||
|
|
||
|
|
@@ -32,3 +38,23 @@ def _get_record(self, id_, identity, action, file_key=None): | |
| self._check_record_deleted_permissions(record, identity) | ||
|
|
||
| return record | ||
|
|
||
| @unit_of_work() | ||
| def commit_file(self, identity, id_, file_key, uow=None): | ||
| """Commit a file upload.""" | ||
| result = super().commit_file(identity, id_, file_key, uow=uow) | ||
|
|
||
| uow.register( | ||
| AuditLogOp(FileCreateAuditLog.build(identity, id_, file_key=file_key)) | ||
|
Contributor
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. Why did we choose to use the drafts id for file creation logs and not the parent.pid.pid_value as we do In the other logs?
Contributor
Author
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. For the share access logs, the metadata of the parent is updated. Here, the metadata of the files attached to the record are updated. And through the UI, we can only create or delete a file. |
||
| ) # Added here as audit logs can't be added to invenio-records-resources | ||
| return result | ||
|
|
||
| @unit_of_work() | ||
| def delete_file(self, identity, id_, file_key, uow=None): | ||
| """Delete a file.""" | ||
| result = super().delete_file(identity, id_, file_key, uow=uow) | ||
|
|
||
| uow.register( | ||
| AuditLogOp(FileDeleteAuditLog.build(identity, id_, file_key=file_key)) | ||
| ) # Added here as audit logs can't be added to invenio-records-resources | ||
| return result | ||
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.
Is this log only for the initial file creation? What if a record is modified and files are added?
Uh oh!
There was an error while loading. Please reload this page.
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.
Then it's a new file and a new audit log entry, through the FileService
commit_filemethod