-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* anonymous users for multi tenant setting * nit * k
- Loading branch information
Showing
26 changed files
with
538 additions
and
38 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
backend/alembic_tenants/versions/a4f6ee863c47_mapping_for_anonymous_user_path.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""mapping for anonymous user path | ||
Revision ID: a4f6ee863c47 | ||
Revises: 14a83a331951 | ||
Create Date: 2025-01-04 14:16:58.697451 | ||
""" | ||
import sqlalchemy as sa | ||
|
||
from alembic import op | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = "a4f6ee863c47" | ||
down_revision = "14a83a331951" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
op.create_table( | ||
"tenant_anonymous_user_path", | ||
sa.Column("tenant_id", sa.String(), primary_key=True, nullable=False), | ||
sa.Column("anonymous_user_path", sa.String(), nullable=False), | ||
sa.PrimaryKeyConstraint("tenant_id"), | ||
sa.UniqueConstraint("anonymous_user_path"), | ||
) | ||
|
||
|
||
def downgrade() -> None: | ||
op.drop_table("tenant_anonymous_user_path") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from sqlalchemy import select | ||
from sqlalchemy.orm import Session | ||
|
||
from onyx.db.models import TenantAnonymousUserPath | ||
|
||
|
||
def get_anonymous_user_path(tenant_id: str, db_session: Session) -> str | None: | ||
result = db_session.execute( | ||
select(TenantAnonymousUserPath).where( | ||
TenantAnonymousUserPath.tenant_id == tenant_id | ||
) | ||
) | ||
result_scalar = result.scalar_one_or_none() | ||
if result_scalar: | ||
return result_scalar.anonymous_user_path | ||
else: | ||
return None | ||
|
||
|
||
def modify_anonymous_user_path( | ||
tenant_id: str, anonymous_user_path: str, db_session: Session | ||
) -> None: | ||
# Enforce lowercase path at DB operation level | ||
anonymous_user_path = anonymous_user_path.lower() | ||
|
||
existing_entry = ( | ||
db_session.query(TenantAnonymousUserPath).filter_by(tenant_id=tenant_id).first() | ||
) | ||
|
||
if existing_entry: | ||
existing_entry.anonymous_user_path = anonymous_user_path | ||
|
||
else: | ||
new_entry = TenantAnonymousUserPath( | ||
tenant_id=tenant_id, anonymous_user_path=anonymous_user_path | ||
) | ||
db_session.add(new_entry) | ||
|
||
db_session.commit() | ||
|
||
|
||
def get_tenant_id_for_anonymous_user_path( | ||
anonymous_user_path: str, db_session: Session | ||
) -> str | None: | ||
result = db_session.execute( | ||
select(TenantAnonymousUserPath).where( | ||
TenantAnonymousUserPath.anonymous_user_path == anonymous_user_path | ||
) | ||
) | ||
result_scalar = result.scalar_one_or_none() | ||
if result_scalar: | ||
return result_scalar.tenant_id | ||
else: | ||
return None | ||
|
||
|
||
def validate_anonymous_user_path(path: str) -> None: | ||
if not path or "/" in path or not path.replace("-", "").isalnum(): | ||
raise ValueError("Invalid path. Use only letters, numbers, and hyphens.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.