Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Tighten ACL for user routes #2929

Merged
merged 4 commits into from
Nov 6, 2023
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
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