Skip to content

Commit

Permalink
k
Browse files Browse the repository at this point in the history
  • Loading branch information
pablonyx committed Dec 31, 2024
1 parent bdd5b4d commit 9bf7661
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions backend/ee/onyx/db/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from collections.abc import Sequence
from uuid import UUID

from sqlalchemy import and_
from sqlalchemy import case
from sqlalchemy import cast
from sqlalchemy import Date
Expand All @@ -16,7 +17,7 @@
from onyx.db.models import ChatSession
from onyx.db.models import Persona
from onyx.db.models import User
from onyx.db.persona import _add_user_filters as _add_persona_user_filters
from onyx.db.models import UserRole


def fetch_query_analytics(
Expand Down Expand Up @@ -339,11 +340,23 @@ def fetch_assistant_unique_users_total(
return result if result else 0


# Users can view assistant stats if they created the persona,
# or if they are an admin
def user_can_view_assistant_stats(
db_session: Session, user: User | None, assistant_id: int
) -> bool:
# Using the same logic as other user filters
stmt = select(Persona).where(Persona.id == assistant_id)
stmt = _add_persona_user_filters(stmt, user)
# If user is None, we assume they are an admin
if user is None:
return True

# Admins can always view stats
if user.role == UserRole.ADMIN:
return True

# Check if the user created the persona
stmt = select(Persona).where(
and_(Persona.id == assistant_id, Persona.user_id == user.id)
)

persona = db_session.execute(stmt).scalar_one_or_none()
return persona is not None

0 comments on commit 9bf7661

Please sign in to comment.