Files
onyx/backend/ee/danswer/db/document.py
T
hagen-danswer 2274cab554 Added permission syncing (#2340)
* Added permission syncing on the backend

* Rewored to work with celery

alembic fix

fixed test

* frontend changes

* got groups working

* added comments and fixed public docs

* fixed merge issues

* frontend complete!

* frontend cleanup and mypy fixes

* refactored connector access_type selection

* mypy fixes

* minor refactor and frontend improvements

* get to fetch

* renames and comments

* minor change to var names

* got curator stuff working

* addressed pablo's comments

* refactored user_external_group to reference users table

* implemented polling

* small refactor

* fixed a whoopsies on the frontend

* added scripts to seed dummy docs and test query times

* fixed frontend build issue

* alembic fix

* handled is_public overlap

* yuhong feedback

* added more checks for sync

* black

* mypy

* fixed circular import

* todos

* alembic fix

* alembic
2024-09-19 22:07:36 +00:00

48 lines
1.6 KiB
Python

from sqlalchemy import select
from sqlalchemy.orm import Session
from danswer.access.models import ExternalAccess
from danswer.access.utils import prefix_group_w_source
from danswer.configs.constants import DocumentSource
from danswer.db.models import Document as DbDocument
def upsert_document_external_perms__no_commit(
db_session: Session,
doc_id: str,
external_access: ExternalAccess,
source_type: DocumentSource,
) -> None:
"""
This sets the permissions for a document in postgres.
NOTE: this will replace any existing external access, it will not do a union
"""
document = db_session.scalars(
select(DbDocument).where(DbDocument.id == doc_id)
).first()
prefixed_external_groups = [
prefix_group_w_source(
ext_group_name=group_id,
source=source_type,
)
for group_id in external_access.external_user_group_ids
]
if not document:
# If the document does not exist, still store the external access
# So that if the document is added later, the external access is already stored
document = DbDocument(
id=doc_id,
semantic_id="",
external_user_emails=external_access.external_user_emails,
external_user_group_ids=prefixed_external_groups,
is_public=external_access.is_public,
)
db_session.add(document)
return
document.external_user_emails = list(external_access.external_user_emails)
document.external_user_group_ids = prefixed_external_groups
document.is_public = external_access.is_public