Skip to content

Commit fe0b1d9

Browse files
arrestleAlanCoding
authored andcommitted
[AAP-52187] Allow authenticated reads on RBAC UserAccessViewSet; keep writes restricted
1 parent d429f08 commit fe0b1d9

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

ansible_base/rbac/api/views.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ class UserAccessViewSet(
296296
"""
297297

298298
serializer_mixin = UserAccessListMixin
299+
permission_classes = try_add_oauth2_scope_permission([permissions.IsAuthenticated])
299300

300301
def get_data_from_url(self):
301302
if not hasattr(self, 'related_object'):
@@ -378,6 +379,7 @@ class UserAccessAssignmentViewSet(
378379
"""
379380

380381
serializer_class = UserAccessAssignmentSerializer
382+
permission_classes = try_add_oauth2_scope_permission([permissions.IsAuthenticated])
381383

382384
def get_url_actor(self):
383385
actor_pk = self.kwargs.get("actor_pk")

test_app/tests/rbac/api/test_access_lists.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,43 @@ def test_no_duplicates_team(team, inv_rd, inventory, org_inv_rd, admin_api_clien
138138
response = admin_api_client.get(url)
139139
assert response.status_code == 200, response.data
140140
assert response.data['count'] == 1, response.data
141+
142+
143+
@pytest.mark.django_db
144+
def test_org_admin_role_user_access_bug(organization, org_admin_rd):
145+
"""
146+
Test for AAP-52187: Org admin gets 403 on role_user_access despite having proper permissions.
147+
148+
This test demonstrates the RBAC evaluation bug where:
149+
- Org admin can GET /organizations/X/ (works correctly)
150+
- Same org admin gets 403 on /role_user_access/shared.organization/X/ (bug)
151+
- Both should work since the user has shared.view_organization permission
152+
"""
153+
from rest_framework.test import APIClient
154+
155+
# Create org admin user for AAP-52187 reproduction
156+
org_admin_user = User.objects.create(username='aap52187-org-admin-test-user')
157+
158+
# Give user Organization Admin role on the organization
159+
org_admin_rd.give_permission(org_admin_user, organization)
160+
161+
# Create API client for the org admin user
162+
client = APIClient()
163+
client.force_authenticate(user=org_admin_user)
164+
165+
# Test 1: Org admin should be able to view the organization directly
166+
org_detail_url = get_relative_url('organization-detail', kwargs={'pk': organization.pk})
167+
response = client.get(org_detail_url)
168+
assert response.status_code == 200, f"Org admin should be able to view organization directly: {response.data}"
169+
170+
# Test 2: Org admin should be able to view role user access for the same organization
171+
# This is currently broken due to has_obj_perm evaluation bug in UserAccessViewSet
172+
role_access_url = get_relative_url('role-user-access', kwargs={'pk': organization.pk, 'model_name': 'shared.organization'})
173+
response = client.get(role_access_url)
174+
175+
# This assertion will fail with current bug, demonstrating the issue
176+
assert response.status_code == 200, (
177+
f"AAP-52187 BUG: Org admin should be able to view role access for organization they manage. "
178+
f"User has shared.view_organization permission and can access org detail endpoint, "
179+
f"but role_user_access fails with: {response.status_code} {response.data}"
180+
)

0 commit comments

Comments
 (0)