Skip to content

Commit 2b14470

Browse files
committed
Add RoleData to resource registry
1 parent d1433df commit 2b14470

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

ansible_base/resource_registry/registry.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class ServiceAPIConfig:
2727
"shared.team": ResourceTypeProcessor,
2828
"shared.organization": ResourceTypeProcessor,
2929
"shared.user": ResourceTypeProcessor,
30+
"shared.roledefinition": ResourceTypeProcessor,
3031
}
3132

3233
custom_resource_processors = {}

ansible_base/resource_registry/shared_types.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from rest_framework import serializers
22

3+
from ansible_base.rbac.models import DABContentType
34
from ansible_base.resource_registry.utils.resource_type_serializers import AnsibleResourceForeignKeyField, SharedResourceTypeSerializer
45
from ansible_base.resource_registry.utils.sso_provider import get_sso_provider_server
56

@@ -75,3 +76,27 @@ class TeamType(SharedResourceTypeSerializer):
7576
default="",
7677
allow_blank=True,
7778
)
79+
80+
81+
class RoleDefinitionPermissionsSerializer(serializers.Serializer):
82+
permissions = serializers.SlugRelatedField(
83+
slug_field='api_slug',
84+
read_only=True,
85+
many=True,
86+
)
87+
88+
89+
class RoleDefinitionType(SharedResourceTypeSerializer):
90+
RESOURCE_TYPE = "roledefinition"
91+
ADDITIONAL_DATA_SERIALIZER = RoleDefinitionPermissionsSerializer
92+
UNIQUE_FIELDS = ("name",)
93+
94+
name = serializers.CharField()
95+
description = serializers.CharField(default="", allow_blank=True)
96+
managed = serializers.BooleanField()
97+
content_type = serializers.SlugRelatedField(
98+
slug_field='api_slug',
99+
queryset=DABContentType.objects.all(),
100+
allow_null=True,
101+
default=None,
102+
)

docs/apps/rbac/for_app_developers.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,31 @@ role definition with the name "team-member".
350350
Apps that utilize django-ansible-base may wish to add extra validation when assigning roles to actors (users or teams).
351351

352352
see [Validation callback for role assignment](../../lib/validation.md)
353+
354+
### Remote Permissions
355+
356+
There is an API under `/service-index/` designed for the purpose (primarily) for use in
357+
coordination of permissions between multiple servers or services.
358+
Actually doing the coordination of permissions in a cluster is an exercise left up to the reader.
359+
360+
Types and permissions are shown in:
361+
- `/service-index/role-types/`
362+
- `/service-index/role-permissions/`
363+
364+
To get everything you need, you must add an entry to your `RESOURCE_LIST` setting.
365+
366+
```
367+
from ansible_base.rbac.models import RoleDefinition
368+
from ansible_base.resource_registry.shared_types import RoleDefinitionType
369+
370+
RESOURCE_LIST = (
371+
...
372+
ResourceConfig(
373+
RoleDefinition,
374+
shared_resource=SharedResource(serializer=RoleDefinitionType, is_provider=False),
375+
),
376+
)
377+
```
378+
379+
With this, role definitions should appear in the endpoint
380+
- `/service-index/resources/?`

test_app/resource_api.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from django.contrib.auth import get_user_model
22

33
from ansible_base.authentication.models import Authenticator
4+
from ansible_base.rbac.models import RoleDefinition
45
from ansible_base.resource_registry.registry import ResourceConfig, ServiceAPIConfig, SharedResource
5-
from ansible_base.resource_registry.shared_types import OrganizationType, TeamType, UserType
6+
from ansible_base.resource_registry.shared_types import OrganizationType, RoleDefinitionType, TeamType, UserType
67
from ansible_base.resource_registry.utils.resource_type_processor import ResourceTypeProcessor
78
from test_app.models import Organization, Original1, Proxy2, ResourceMigrationTestModel, Team
89

@@ -37,6 +38,10 @@ class APIConfig(ServiceAPIConfig):
3738
Organization,
3839
shared_resource=SharedResource(serializer=OrganizationType, is_provider=False),
3940
),
41+
ResourceConfig(
42+
RoleDefinition,
43+
shared_resource=SharedResource(serializer=RoleDefinitionType, is_provider=False),
44+
),
4045
# Authenticators won't be a shared resource in production, but it's a convenient model to use for testing.
4146
ResourceConfig(Authenticator),
4247
ResourceConfig(ResourceMigrationTestModel),

test_app/tests/rbac/remote/test_service_api.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,22 @@ def test_get_permission_list(admin_api_client):
3232
change_org_data = type_data['shared.change_organization']
3333
assert change_org_data['content_type'] == 'shared.organization'
3434
assert change_org_data['codename'] == 'change_organization'
35+
36+
37+
@pytest.mark.django_db
38+
def test_role_definition_listed_as_resource(admin_api_client, org_admin_rd):
39+
url = get_relative_url('resource-list')
40+
url += '?page_size=200&content_type__resource_type__name=shared.roledefinition'
41+
response = admin_api_client.get(url, format="json")
42+
assert response.status_code == 200, response.data
43+
rd_data = {rd['name']: rd for rd in response.data['results']}
44+
45+
assert 'Organization Admin' in rd_data
46+
org_admin_data = rd_data['Organization Admin']
47+
48+
detail = admin_api_client.get(org_admin_data['url'], format="json")
49+
resource_data = detail.data['resource_data']
50+
assert resource_data['managed'] is True
51+
assert resource_data['content_type'] == 'shared.organization'
52+
assert 'permissions' in detail.data['additional_data']
53+
assert 'aap.add_inventory' in detail.data['additional_data']['permissions']

0 commit comments

Comments
 (0)