Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
18 changes: 16 additions & 2 deletions api/controllers/service_api/dataset/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
from libs.helper import dump_response
from libs.login import current_user
from libs.pagination import clamp_pagination, paginate_query
from models.account import Account
from models.dataset import Dataset, Document
from models.enums import DocumentCreatedFrom
from repositories.knowledge.dataset_read_repository import (
Expand Down Expand Up @@ -534,6 +535,19 @@ def _update_document_by_text(
return document, batch


def _file_document_account(dataset: Dataset, *, session: Session) -> Account:
"""Keep the dataset creator as the document author when they still belong to its tenant."""
creator = get_dataset_creator(dataset, session=session)
if creator is not None:
creator.set_tenant_id_with_session(dataset.tenant_id, session=session)
if creator.current_tenant_id == dataset.tenant_id:
return creator

# The dataset creator may have left the tenant. The authenticated Service API
# account is the tenant owner and already has the required tenant context.
return current_user


@service_api_ns.route("/datasets/<uuid:dataset_id>/document/create-by-text")
class DocumentAddByTextApi(DatasetApiResource):
"""Resource for the canonical text document creation route."""
Expand Down Expand Up @@ -851,7 +865,7 @@ def post(self, session: Session, tenant_id, dataset_id: UUID):
documents, batch = DocumentService.save_document_with_dataset_id(
dataset=dataset,
knowledge_config=knowledge_config,
account=get_dataset_creator(dataset, session=session),
account=_file_document_account(dataset, session=session),
dataset_process_rule=dataset_process_rule,
created_from=DocumentCreatedFrom.API,
session=session,
Expand Down Expand Up @@ -930,7 +944,7 @@ def _update_document_by_file(
documents, _ = DocumentService.save_document_with_dataset_id(
dataset=dataset,
knowledge_config=knowledge_config,
account=get_dataset_creator(dataset, session=session),
account=_file_document_account(dataset, session=session),
dataset_process_rule=get_latest_dataset_process_rule(dataset, session=session)
if "process_rule" not in args
else None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
from core.rag.index_processor.constant.index_type import IndexStructureType
from enums import DeploymentEdition
from libs.pagination import PaginatedResult, clamp_pagination
from models.account import Account
from models.account import Account, TenantAccountJoin, TenantAccountRole
from models.dataset import Dataset, Document, DocumentSegment
from models.enums import (
ApiTokenType,
Expand Down Expand Up @@ -1032,6 +1032,32 @@ def _persist_dataset(self, dataset: Dataset) -> None:
self.session.commit()


class TestFileDocumentAccount(SQLiteControllerTest):
@pytest.mark.parametrize("creator_is_member", [True, False])
def test_creator_attribution_requires_tenant_membership(self, service_api_identity, creator_is_member: bool):
creator = make_account(
account_id=str(uuid.uuid4()),
email=f"dataset-creator-{uuid.uuid4()}@example.com",
)
self.session.add(creator)
if creator_is_member:
self.session.add(
TenantAccountJoin(
tenant_id=service_api_identity.tenant.id,
account_id=creator.id,
role=TenantAccountRole.NORMAL,
)
)
self.session.commit()
dataset = make_dataset(tenant_id=service_api_identity.tenant.id, created_by=creator.id)

with patch.object(document_module, "current_user", service_api_identity.account):
account = document_module._file_document_account(dataset, session=self.session)

assert account.id == (creator.id if creator_is_member else service_api_identity.account.id)
assert account.current_tenant_id == dataset.tenant_id


class TestDocumentApiDelete(SQLiteControllerTest):
"""Test suite for DocumentApi.delete() endpoint.

Expand Down Expand Up @@ -1923,6 +1949,9 @@ def test_add_by_file_success_serializes_document_and_batch_shape(
{"document": _expected_document_response(mock_document), "batch": "batch-file"},
200,
)
account = mock_doc_svc.save_document_with_dataset_id.call_args.kwargs["account"]
assert isinstance(account, Account)
assert account.id == "user-1"

@patch(
"controllers.service_api.dataset.document.FeatureService.get_knowledge_file_size_limit",
Expand Down Expand Up @@ -2269,3 +2298,6 @@ def test_update_by_file_success(
{"document": _expected_document_response(mock_document), "batch": "batch-1"},
200,
)
account = mock_doc_svc.save_document_with_dataset_id.call_args.kwargs["account"]
assert isinstance(account, Account)
assert account.id == "user-1"