✨(backend) extract attachment keys from updated content for access
We can't prevent document editors from copy/pasting content to from one document to another. The problem is that copying content, will copy the urls pointing to attachments but if we don't do anything, the reader of the document to which the content is being pasted, may not be allowed to access the attachment files from the original document. Using the work from the previous commit, we can grant access to the readers of the target document by extracting the attachment keys from the content and adding themto the target document's "attachments" field. Before doing this, we check that the current user can indeed access the attachment files extracted from the content and that they are allowed to edit the current document.
This commit is contained in:
committed by
Manuel Raynaud
parent
34a208a80d
commit
c02f19a2cd
@@ -10,7 +10,7 @@ from django.utils.translation import gettext_lazy as _
|
||||
import magic
|
||||
from rest_framework import exceptions, serializers
|
||||
|
||||
from core import enums, models
|
||||
from core import enums, models, utils
|
||||
from core.services.ai_services import AI_ACTIONS
|
||||
from core.services.converter_services import (
|
||||
ConversionError,
|
||||
@@ -268,6 +268,53 @@ class DocumentSerializer(ListDocumentSerializer):
|
||||
|
||||
return value
|
||||
|
||||
def save(self, **kwargs):
|
||||
"""
|
||||
Process the content field to extract attachment keys and update the document's
|
||||
"attachments" field for access control.
|
||||
"""
|
||||
content = self.validated_data.get("content", "")
|
||||
extracted_attachments = set(utils.extract_attachments(content))
|
||||
|
||||
existing_attachments = (
|
||||
set(self.instance.attachments or []) if self.instance else set()
|
||||
)
|
||||
new_attachments = extracted_attachments - existing_attachments
|
||||
|
||||
if new_attachments:
|
||||
attachments_documents = (
|
||||
models.Document.objects.filter(
|
||||
attachments__overlap=list(new_attachments)
|
||||
)
|
||||
.only("path", "attachments")
|
||||
.order_by("path")
|
||||
)
|
||||
|
||||
user = self.context["request"].user
|
||||
readable_per_se_paths = (
|
||||
models.Document.objects.readable_per_se(user)
|
||||
.order_by("path")
|
||||
.values_list("path", flat=True)
|
||||
)
|
||||
readable_attachments_paths = utils.filter_descendants(
|
||||
[doc.path for doc in attachments_documents],
|
||||
readable_per_se_paths,
|
||||
skip_sorting=True,
|
||||
)
|
||||
|
||||
readable_attachments = set()
|
||||
for document in attachments_documents:
|
||||
if document.path not in readable_attachments_paths:
|
||||
continue
|
||||
readable_attachments.update(set(document.attachments) & new_attachments)
|
||||
|
||||
# Update attachments with readable keys
|
||||
self.validated_data["attachments"] = list(
|
||||
existing_attachments | readable_attachments
|
||||
)
|
||||
|
||||
return super().save(**kwargs)
|
||||
|
||||
|
||||
class ServerCreateDocumentSerializer(serializers.Serializer):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user