Skip to content
This repository was archived by the owner on Sep 3, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/dispatch/auth/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ def __init__(self, request: Request):
if not organization:
raise HTTPException(status_code=self.org_error_code, detail=self.org_error_msg)

org_check = organization_service.get_by_slug(
db_session=request.state.db, slug=organization.slug
)

if not org_check or org_check.id != organization.id:
raise HTTPException(status_code=self.org_error_code, detail=self.org_error_msg)

user = get_current_user(request=request)
if not user:
raise HTTPException(status_code=self.user_error_code, detail=self.user_error_msg)
Expand Down
15 changes: 13 additions & 2 deletions src/dispatch/organization/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from fastapi import APIRouter, Depends, HTTPException, status
from slugify import slugify
from pydantic.error_wrappers import ErrorWrapper, ValidationError

from sqlalchemy.exc import IntegrityError
Expand All @@ -23,7 +24,7 @@
OrganizationUpdate,
OrganizationPagination,
)
from .service import create, get, get_by_name, update, add_user
from .service import create, get, get_by_name, get_by_slug, update, add_user


router = APIRouter()
Expand All @@ -45,6 +46,11 @@ def create_organization(
current_user: CurrentUser,
):
"""Create a new organization."""
if not organization_in.name:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=[{"msg": "An organization name is required."}],
)
organization = get_by_name(db_session=db_session, name=organization_in.name)
if organization:
raise HTTPException(
Expand All @@ -56,7 +62,12 @@ def create_organization(
status_code=status.HTTP_409_CONFLICT,
detail=[{"msg": "An organization with this id already exists."}],
)

slug = slugify(organization_in.name, separator="_")
if get_by_slug(db_session=db_session, slug=slug):
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail=[{"msg": "An organization with this slug already exists."}],
)
# we create the organization
organization = create(db_session=db_session, organization_in=organization_in)

Expand Down
Loading