diff --git a/src/dispatch/auth/models.py b/src/dispatch/auth/models.py index d36987d9e656..33a12a29b9a4 100644 --- a/src/dispatch/auth/models.py +++ b/src/dispatch/auth/models.py @@ -21,8 +21,7 @@ ) from dispatch.database.core import Base from dispatch.enums import UserRoles -from dispatch.models import PrimaryKey -from dispatch.models import TimeStampMixin, DispatchBase +from dispatch.models import OrganizationSlug, PrimaryKey, TimeStampMixin, DispatchBase from dispatch.organization.models import Organization, OrganizationRead from dispatch.project.models import Project, ProjectRead @@ -70,10 +69,10 @@ def token(self): } return jwt.encode(data, DISPATCH_JWT_SECRET, algorithm=DISPATCH_JWT_ALG) - def get_organization_role(self, organization_name): - """Gets the users role for a given organization.""" + def get_organization_role(self, organization_slug: OrganizationSlug): + """Gets the user's role for a given organization slug.""" for o in self.organizations: - if o.organization.name == organization_name: + if o.organization.slug == organization_slug: return o.role diff --git a/src/dispatch/auth/permissions.py b/src/dispatch/auth/permissions.py index 8a48a2bdbf72..91fc6b5a690d 100644 --- a/src/dispatch/auth/permissions.py +++ b/src/dispatch/auth/permissions.py @@ -76,7 +76,7 @@ def __init__(self, request: Request): if not user: raise HTTPException(status_code=self.status_code, detail=self.error_msg) - self.role = user.get_organization_role(organization.name) + self.role = user.get_organization_role(organization.slug) if not self.has_required_permissions(request): raise HTTPException(status_code=self.status_code, detail=self.error_msg) diff --git a/src/dispatch/auth/service.py b/src/dispatch/auth/service.py index dedfea32385b..2b723dfb5a34 100644 --- a/src/dispatch/auth/service.py +++ b/src/dispatch/auth/service.py @@ -253,4 +253,4 @@ def get_current_role( request: Request, current_user: DispatchUser = Depends(get_current_user) ) -> UserRoles: """Attempts to get the current user depending on the configured authentication provider.""" - return current_user.get_organization_role(organization_name=request.state.organization) + return current_user.get_organization_role(organization_slug=request.state.organization) diff --git a/src/dispatch/auth/views.py b/src/dispatch/auth/views.py index 9b43166bda7c..5be6e6898175 100644 --- a/src/dispatch/auth/views.py +++ b/src/dispatch/auth/views.py @@ -51,7 +51,7 @@ def get_users(*, organization: OrganizationSlug, common: dict = Depends(common_parameters)): """Get all users.""" common["filter_spec"] = { - "and": [{"model": "Organization", "op": "==", "field": "name", "value": organization}] + "and": [{"model": "Organization", "op": "==", "field": "slug", "value": organization}] } items = search_filter_sort_paginate(model="DispatchUser", **common)