mirror of
https://github.com/danswer-ai/danswer.git
synced 2025-09-19 12:03:54 +02:00
Proper anonymous user restricting (#3645)
This commit is contained in:
@@ -345,7 +345,8 @@ def fetch_assistant_unique_users_total(
|
|||||||
def user_can_view_assistant_stats(
|
def user_can_view_assistant_stats(
|
||||||
db_session: Session, user: User | None, assistant_id: int
|
db_session: Session, user: User | None, assistant_id: int
|
||||||
) -> bool:
|
) -> bool:
|
||||||
# If user is None, assume the user is an admin or auth is disabled
|
# If user is None and auth is disabled, assume the user is an admin
|
||||||
|
|
||||||
if user is None or user.role == UserRole.ADMIN:
|
if user is None or user.role == UserRole.ADMIN:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@@ -7,6 +7,7 @@ from sqlalchemy import select
|
|||||||
from sqlalchemy.orm import aliased
|
from sqlalchemy.orm import aliased
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
from onyx.configs.app_configs import DISABLE_AUTH
|
||||||
from onyx.configs.constants import TokenRateLimitScope
|
from onyx.configs.constants import TokenRateLimitScope
|
||||||
from onyx.db.models import TokenRateLimit
|
from onyx.db.models import TokenRateLimit
|
||||||
from onyx.db.models import TokenRateLimit__UserGroup
|
from onyx.db.models import TokenRateLimit__UserGroup
|
||||||
@@ -20,8 +21,8 @@ from onyx.server.token_rate_limits.models import TokenRateLimitArgs
|
|||||||
def _add_user_filters(
|
def _add_user_filters(
|
||||||
stmt: Select, user: User | None, get_editable: bool = True
|
stmt: Select, user: User | None, get_editable: bool = True
|
||||||
) -> Select:
|
) -> Select:
|
||||||
# If user is None, assume the user is an admin or auth is disabled
|
# If user is None and auth is disabled, assume the user is an admin
|
||||||
if user is None or user.role == UserRole.ADMIN:
|
if (user is None and DISABLE_AUTH) or (user and user.role == UserRole.ADMIN):
|
||||||
return stmt
|
return stmt
|
||||||
|
|
||||||
stmt = stmt.distinct()
|
stmt = stmt.distinct()
|
||||||
@@ -47,6 +48,12 @@ def _add_user_filters(
|
|||||||
that the user isn't a curator for
|
that the user isn't a curator for
|
||||||
- if we are not editing, we show all token_rate_limits in the groups the user curates
|
- if we are not editing, we show all token_rate_limits in the groups the user curates
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# If user is None, this is an anonymous user and we should only show public token_rate_limits
|
||||||
|
if user is None:
|
||||||
|
where_clause = TokenRateLimit.scope == TokenRateLimitScope.GLOBAL
|
||||||
|
return stmt.where(where_clause)
|
||||||
|
|
||||||
where_clause = User__UG.user_id == user.id
|
where_clause = User__UG.user_id == user.id
|
||||||
if user.role == UserRole.CURATOR and get_editable:
|
if user.role == UserRole.CURATOR and get_editable:
|
||||||
where_clause &= User__UG.is_curator == True # noqa: E712
|
where_clause &= User__UG.is_curator == True # noqa: E712
|
||||||
|
@@ -10,6 +10,7 @@ from sqlalchemy.orm import aliased
|
|||||||
from sqlalchemy.orm import joinedload
|
from sqlalchemy.orm import joinedload
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
from onyx.configs.app_configs import DISABLE_AUTH
|
||||||
from onyx.configs.constants import DocumentSource
|
from onyx.configs.constants import DocumentSource
|
||||||
from onyx.db.connector import fetch_connector_by_id
|
from onyx.db.connector import fetch_connector_by_id
|
||||||
from onyx.db.credentials import fetch_credential_by_id
|
from onyx.db.credentials import fetch_credential_by_id
|
||||||
@@ -28,15 +29,14 @@ from onyx.server.models import StatusResponse
|
|||||||
from onyx.utils.logger import setup_logger
|
from onyx.utils.logger import setup_logger
|
||||||
from onyx.utils.variable_functionality import fetch_ee_implementation_or_noop
|
from onyx.utils.variable_functionality import fetch_ee_implementation_or_noop
|
||||||
|
|
||||||
|
|
||||||
logger = setup_logger()
|
logger = setup_logger()
|
||||||
|
|
||||||
|
|
||||||
def _add_user_filters(
|
def _add_user_filters(
|
||||||
stmt: Select, user: User | None, get_editable: bool = True
|
stmt: Select, user: User | None, get_editable: bool = True
|
||||||
) -> Select:
|
) -> Select:
|
||||||
# If user is None, assume the user is an admin or auth is disabled
|
# If user is None and auth is disabled, assume the user is an admin
|
||||||
if user is None or user.role == UserRole.ADMIN:
|
if (user is None and DISABLE_AUTH) or (user and user.role == UserRole.ADMIN):
|
||||||
return stmt
|
return stmt
|
||||||
|
|
||||||
stmt = stmt.distinct()
|
stmt = stmt.distinct()
|
||||||
@@ -63,6 +63,12 @@ def _add_user_filters(
|
|||||||
- if we are not editing, we show all cc_pairs in the groups the user is a curator
|
- if we are not editing, we show all cc_pairs in the groups the user is a curator
|
||||||
for (as well as public cc_pairs)
|
for (as well as public cc_pairs)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# If user is None, this is an anonymous user and we should only show public cc_pairs
|
||||||
|
if user is None:
|
||||||
|
where_clause = ConnectorCredentialPair.access_type == AccessType.PUBLIC
|
||||||
|
return stmt.where(where_clause)
|
||||||
|
|
||||||
where_clause = User__UG.user_id == user.id
|
where_clause = User__UG.user_id == user.id
|
||||||
if user.role == UserRole.CURATOR and get_editable:
|
if user.role == UserRole.CURATOR and get_editable:
|
||||||
where_clause &= User__UG.is_curator == True # noqa: E712
|
where_clause &= User__UG.is_curator == True # noqa: E712
|
||||||
|
@@ -12,6 +12,7 @@ from sqlalchemy import select
|
|||||||
from sqlalchemy.orm import aliased
|
from sqlalchemy.orm import aliased
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
from onyx.configs.app_configs import DISABLE_AUTH
|
||||||
from onyx.db.connector_credential_pair import get_cc_pair_groups_for_ids
|
from onyx.db.connector_credential_pair import get_cc_pair_groups_for_ids
|
||||||
from onyx.db.connector_credential_pair import get_connector_credential_pairs
|
from onyx.db.connector_credential_pair import get_connector_credential_pairs
|
||||||
from onyx.db.enums import AccessType
|
from onyx.db.enums import AccessType
|
||||||
@@ -36,8 +37,8 @@ logger = setup_logger()
|
|||||||
def _add_user_filters(
|
def _add_user_filters(
|
||||||
stmt: Select, user: User | None, get_editable: bool = True
|
stmt: Select, user: User | None, get_editable: bool = True
|
||||||
) -> Select:
|
) -> Select:
|
||||||
# If user is None, assume the user is an admin or auth is disabled
|
# If user is None and auth is disabled, assume the user is an admin
|
||||||
if user is None or user.role == UserRole.ADMIN:
|
if (user is None and DISABLE_AUTH) or (user and user.role == UserRole.ADMIN):
|
||||||
return stmt
|
return stmt
|
||||||
|
|
||||||
stmt = stmt.distinct()
|
stmt = stmt.distinct()
|
||||||
@@ -61,6 +62,12 @@ def _add_user_filters(
|
|||||||
- if we are not editing, we show all DocumentSets in the groups the user is a curator
|
- if we are not editing, we show all DocumentSets in the groups the user is a curator
|
||||||
for (as well as public DocumentSets)
|
for (as well as public DocumentSets)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# If user is None, this is an anonymous user and we should only show public DocumentSets
|
||||||
|
if user is None:
|
||||||
|
where_clause = DocumentSetDBModel.is_public == True # noqa: E712
|
||||||
|
return stmt.where(where_clause)
|
||||||
|
|
||||||
where_clause = User__UserGroup.user_id == user.id
|
where_clause = User__UserGroup.user_id == user.id
|
||||||
if user.role == UserRole.CURATOR and get_editable:
|
if user.role == UserRole.CURATOR and get_editable:
|
||||||
where_clause &= User__UserGroup.is_curator == True # noqa: E712
|
where_clause &= User__UserGroup.is_curator == True # noqa: E712
|
||||||
|
@@ -13,6 +13,7 @@ from sqlalchemy import select
|
|||||||
from sqlalchemy.orm import aliased
|
from sqlalchemy.orm import aliased
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
from onyx.configs.app_configs import DISABLE_AUTH
|
||||||
from onyx.configs.constants import MessageType
|
from onyx.configs.constants import MessageType
|
||||||
from onyx.configs.constants import SearchFeedbackType
|
from onyx.configs.constants import SearchFeedbackType
|
||||||
from onyx.db.chat import get_chat_message
|
from onyx.db.chat import get_chat_message
|
||||||
@@ -46,8 +47,8 @@ def _fetch_db_doc_by_id(doc_id: str, db_session: Session) -> DbDocument:
|
|||||||
def _add_user_filters(
|
def _add_user_filters(
|
||||||
stmt: Select, user: User | None, get_editable: bool = True
|
stmt: Select, user: User | None, get_editable: bool = True
|
||||||
) -> Select:
|
) -> Select:
|
||||||
# If user is None, assume the user is an admin or auth is disabled
|
# If user is None and auth is disabled, assume the user is an admin
|
||||||
if user is None or user.role == UserRole.ADMIN:
|
if (user is None and DISABLE_AUTH) or (user and user.role == UserRole.ADMIN):
|
||||||
return stmt
|
return stmt
|
||||||
|
|
||||||
stmt = stmt.distinct()
|
stmt = stmt.distinct()
|
||||||
@@ -84,6 +85,12 @@ def _add_user_filters(
|
|||||||
- if we are not editing, we show all objects in the groups the user is a curator
|
- if we are not editing, we show all objects in the groups the user is a curator
|
||||||
for (as well as public objects as well)
|
for (as well as public objects as well)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# If user is None, this is an anonymous user and we should only show public documents
|
||||||
|
if user is None:
|
||||||
|
where_clause = CCPair.access_type == AccessType.PUBLIC
|
||||||
|
return stmt.where(where_clause)
|
||||||
|
|
||||||
where_clause = User__UG.user_id == user.id
|
where_clause = User__UG.user_id == user.id
|
||||||
if user.role == UserRole.CURATOR and get_editable:
|
if user.role == UserRole.CURATOR and get_editable:
|
||||||
where_clause &= User__UG.is_curator == True # noqa: E712
|
where_clause &= User__UG.is_curator == True # noqa: E712
|
||||||
|
@@ -17,6 +17,7 @@ from sqlalchemy.orm import joinedload
|
|||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from onyx.auth.schemas import UserRole
|
from onyx.auth.schemas import UserRole
|
||||||
|
from onyx.configs.app_configs import DISABLE_AUTH
|
||||||
from onyx.configs.chat_configs import BING_API_KEY
|
from onyx.configs.chat_configs import BING_API_KEY
|
||||||
from onyx.configs.chat_configs import CONTEXT_CHUNKS_ABOVE
|
from onyx.configs.chat_configs import CONTEXT_CHUNKS_ABOVE
|
||||||
from onyx.configs.chat_configs import CONTEXT_CHUNKS_BELOW
|
from onyx.configs.chat_configs import CONTEXT_CHUNKS_BELOW
|
||||||
@@ -45,8 +46,8 @@ logger = setup_logger()
|
|||||||
def _add_user_filters(
|
def _add_user_filters(
|
||||||
stmt: Select, user: User | None, get_editable: bool = True
|
stmt: Select, user: User | None, get_editable: bool = True
|
||||||
) -> Select:
|
) -> Select:
|
||||||
# If user is None, assume the user is an admin or auth is disabled
|
# If user is None and auth is disabled, assume the user is an admin
|
||||||
if user is None or user.role == UserRole.ADMIN:
|
if (user is None and DISABLE_AUTH) or (user and user.role == UserRole.ADMIN):
|
||||||
return stmt
|
return stmt
|
||||||
|
|
||||||
stmt = stmt.distinct()
|
stmt = stmt.distinct()
|
||||||
@@ -78,6 +79,12 @@ def _add_user_filters(
|
|||||||
for (as well as public Personas)
|
for (as well as public Personas)
|
||||||
- if we are not editing, we return all Personas directly connected to the user
|
- if we are not editing, we return all Personas directly connected to the user
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# If user is None, this is an anonymous user and we should only show public Personas
|
||||||
|
if user is None:
|
||||||
|
where_clause = Persona.is_public == True # noqa: E712
|
||||||
|
return stmt.where(where_clause)
|
||||||
|
|
||||||
where_clause = User__UserGroup.user_id == user.id
|
where_clause = User__UserGroup.user_id == user.id
|
||||||
if user.role == UserRole.CURATOR and get_editable:
|
if user.role == UserRole.CURATOR and get_editable:
|
||||||
where_clause &= User__UserGroup.is_curator == True # noqa: E712
|
where_clause &= User__UserGroup.is_curator == True # noqa: E712
|
||||||
|
Reference in New Issue
Block a user