Skip to content

Commit

Permalink
fix: Tighten ACL for user routes (#2929)
Browse files Browse the repository at this point in the history
  • Loading branch information
zachaysan authored Nov 6, 2023
1 parent 493f0e5 commit 3732e67
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
42 changes: 42 additions & 0 deletions api/organisations/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,27 @@ def test_make_user_group_admin_success(
)


def test_make_user_group_admin_forbidden(
staff_client: FFAdminUser,
organisation: Organisation,
user_permission_group: UserPermissionGroup,
):
# Given
another_user = FFAdminUser.objects.create(email="[email protected]")
another_user.add_organisation(organisation)
another_user.permission_groups.add(user_permission_group)
url = reverse(
"api-v1:organisations:make-user-group-admin",
args=[organisation.id, user_permission_group.id, another_user.id],
)

# When
response = staff_client.post(url)

# Then
assert response.status_code == status.HTTP_403_FORBIDDEN


def test_remove_user_as_group_admin_user_does_not_belong_to_group(
admin_client, admin_user, organisation, user_permission_group
):
Expand Down Expand Up @@ -1202,6 +1223,27 @@ def test_remove_user_as_group_admin_success(
)


def test_remove_user_as_group_admin_forbidden(
staff_client: FFAdminUser,
organisation: Organisation,
user_permission_group: UserPermissionGroup,
):
# Given
another_user = FFAdminUser.objects.create(email="[email protected]")
another_user.add_organisation(organisation)
another_user.permission_groups.add(user_permission_group)
another_user.make_group_admin(user_permission_group.id)
url = reverse(
"api-v1:organisations:remove-user-group-admin",
args=[organisation.id, user_permission_group.id, another_user.id],
)

# When
response = staff_client.post(url)
# Then
assert response.status_code == status.HTTP_403_FORBIDDEN


def test_list_user_groups_as_group_admin(organisation, api_client):
# Given
user1 = FFAdminUser.objects.create(email="[email protected]")
Expand Down
8 changes: 4 additions & 4 deletions api/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,11 @@ def my_groups(self, request: Request, organisation_pk: int) -> Response:
return self.list(request, organisation_pk)


@permission_classes([IsAuthenticated(), NestedIsOrganisationAdminPermission()])
@api_view(["POST"])
@permission_classes([IsAuthenticated, NestedIsOrganisationAdminPermission])
def make_user_group_admin(
request: Request, organisation_pk: int, group_pk: int, user_pk: int
):
) -> Response:
user = get_object_or_404(
FFAdminUser,
userorganisation__organisation_id=organisation_pk,
Expand All @@ -260,11 +260,11 @@ def make_user_group_admin(
return Response()


@permission_classes([IsAuthenticated(), NestedIsOrganisationAdminPermission()])
@api_view(["POST"])
@permission_classes([IsAuthenticated, NestedIsOrganisationAdminPermission])
def remove_user_as_group_admin(
request: Request, organisation_pk: int, group_pk: int, user_pk: int
):
) -> Response:
user = get_object_or_404(
FFAdminUser,
userorganisation__organisation_id=organisation_pk,
Expand Down

3 comments on commit 3732e67

@vercel
Copy link

@vercel vercel bot commented on 3732e67 Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./docs

docs-flagsmith.vercel.app
docs-git-main-flagsmith.vercel.app
docs.flagsmith.com
docs.bullet-train.io

@vercel
Copy link

@vercel vercel bot commented on 3732e67 Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 3732e67 Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.