perf: resolve permitted_document_ids once before email/share loops

Replaces per-document has_perms_owner_aware calls in the email-document
action and bulk share-link-bundle creation with a single
permitted_document_ids(request.user) resolution before the loop,
reducing DB round-trips while preserving identical permission
semantics (including the per-document error message on the bundle
endpoint).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GRp4kf1mdn9ruv81zWAmh2
This commit is contained in:
stumpylog
2026-07-27 23:41:22 -07:00
co-authored by Claude Sonnet 5
parent 09c60ea667
commit 6794d6e835
3 changed files with 30 additions and 8 deletions
+5 -7
View File
@@ -1925,12 +1925,9 @@ class DocumentViewSet(
use_archive_version = validated_data.get("use_archive_version", True)
documents = Document.objects.select_related("owner").filter(pk__in=document_ids)
for document in documents:
if request.user is not None and not has_perms_owner_aware(
request.user,
"view_document",
document,
):
if request.user is not None:
permitted_ids = set(permitted_document_ids(request.user))
if not all(document.pk in permitted_ids for document in documents):
return HttpResponseForbidden("Insufficient permissions")
try:
@@ -4505,8 +4502,9 @@ class ShareLinkBundleViewSet(PassUserMixin, ModelViewSet[ShareLinkBundle]):
)
documents = list(documents_qs)
permitted_ids = set(permitted_document_ids(request.user))
for document in documents:
if not has_perms_owner_aware(request.user, "view_document", document):
if document.pk not in permitted_ids:
raise ValidationError(
{
"document_ids": _(