Skip to content

Commit

Permalink
Fixes bugs in filtering users by organization and getting users organ…
Browse files Browse the repository at this point in the history
…ization role (#2336)
  • Loading branch information
mvilanova authored Jul 7, 2022
1 parent 36efab7 commit 49c2f54
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 8 deletions.
9 changes: 4 additions & 5 deletions src/dispatch/auth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion src/dispatch/auth/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/dispatch/auth/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion src/dispatch/auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 49c2f54

Please sign in to comment.