Support owner and object permissions for advanced queries

This commit is contained in:
Michael Shamoon
2022-12-12 22:34:38 -08:00
parent f8b77d7ef7
commit 4cf9ed9d26
3 changed files with 32 additions and 5 deletions

View File

@@ -6,6 +6,7 @@ from contextlib import contextmanager
from dateutil.parser import isoparse
from django.conf import settings
from documents.models import Document
from guardian.shortcuts import get_users_with_perms
from whoosh import classify
from whoosh import highlight
from whoosh import query
@@ -49,6 +50,10 @@ def get_schema():
path=TEXT(sortable=True),
path_id=NUMERIC(),
has_path=BOOLEAN(),
owner=TEXT(),
owner_id=NUMERIC(),
has_owner=BOOLEAN(),
viewer_id=KEYWORD(commas=True),
)
@@ -90,6 +95,11 @@ def open_index_searcher():
def update_document(writer, doc):
tags = ",".join([t.name for t in doc.tags.all()])
tags_ids = ",".join([str(t.id) for t in doc.tags.all()])
users_with_perms = get_users_with_perms(
doc,
only_with_perms_in=["view_document"],
)
viewer_ids = ",".join([str(u.id) for u in users_with_perms])
writer.update_document(
id=doc.pk,
title=doc.title,
@@ -110,6 +120,10 @@ def update_document(writer, doc):
path=doc.storage_path.name if doc.storage_path else None,
path_id=doc.storage_path.id if doc.storage_path else None,
has_path=doc.storage_path is not None,
owner=doc.owner.username if doc.owner else None,
owner_id=doc.owner.id if doc.owner else None,
has_owner=doc.owner is not None,
viewer_id=viewer_ids if viewer_ids else None,
)
@@ -168,10 +182,17 @@ class DelayedQuery:
elif k == "storage_path__isnull":
criterias.append(query.Term("has_path", v == "false"))
user_criterias = [query.Term("has_owner", False)]
if "user" in self.query_params:
user_criterias.append(query.Term("owner_id", self.query_params["user"]))
user_criterias.append(
query.Term("viewer_id", str(self.query_params["user"])),
)
if len(criterias) > 0:
criterias.append(query.Or(user_criterias))
return query.And(criterias)
else:
return None
return query.Or(user_criterias)
def _get_query_sortedby(self):
if "ordering" not in self.query_params: