Skip to content

Commit be22014

Browse files
committed
RBAC docs general clarity and copy edit improvements
1 parent c90e9b4 commit be22014

File tree

4 files changed

+264
-238
lines changed

4 files changed

+264
-238
lines changed

docs/apps/rbac/for_app_developers.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,36 @@ These instructions are intended for someone applying this system to an existing
44
Start with `docs/Installation.md` for the core ansible_base setup.
55
The app name is dab_rbac, INSTALLED_APPS path is "ansible_base.rbac".
66

7-
You can choose to use this entirely at the Django model level
8-
- you use roles to delegate permissions to users and teams
9-
- this system will efficiently filter querysets to a certain permission level for a certain user
7+
### DAB Models vs Django Models
8+
9+
DAB RBAC uses specialized models that mirror Django's built-in auth models but are purpose-built for the RBAC system:
10+
11+
- **`DABPermission`** - Mirrors Django's `Permission` model but only tracks permissions for RBAC-registered models and supports remote models
12+
- **`DABContentType`** - Mirrors Django's `ContentType` but with enhanced caching and support for remote models
13+
- **User/Team/Organization Models** - Configurable through settings, defaults to Django's built-in models
14+
15+
This parallel structure provides:
16+
- Better performance through aggressive caching
17+
- Support for tracking permissions to remote services
18+
- Clean separation from Django's built-in auth system
19+
20+
### Integration Approach
21+
22+
You can use DAB RBAC entirely at the Django model level:
23+
- Use role definitions to delegate permissions to users and teams
24+
- The system efficiently filters querysets to show only objects a user has permission to access
25+
26+
### Terminology for Developers
27+
28+
When working with DAB RBAC code, use precise terminology:
29+
30+
- **Role Definition** - Always use "role definition", never just "role" (which could refer to object roles or other concepts)
31+
- **Object Role** - The instantiation of a role definition for a specific object
32+
- **Role Assignment** - The record linking a user/team to a role definition for an object or globally
33+
- **Permission** - An action on a model type (e.g., "change_inventory")
34+
- **Access** - Avoid this term in code; use specific permission evaluations instead
35+
36+
User-facing interfaces may simplify "role definition" to "role" for usability, but internal code should maintain precision.
1037

1138
### Migrations
1239

docs/apps/rbac/for_clients.md

Lines changed: 127 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,83 @@
11
## Using DAB RBAC as an API Client
22

3-
This section tells how to use the API endpoints as a client;
4-
what requests to make and how to build request data.
3+
This section explains how to use the RBAC API endpoints to manage permissions. The API follows a clear hierarchy that reflects the system architecture.
54

6-
You need a server running a Django project that uses this system.
7-
Use test_app in this repo for a demo, see `test_app/README.md` for bootstrap.
8-
The server runs at http://127.0.0.1:8000, and links within that will be referenced here.
9-
Default password for admin user is "admin".
5+
### Prerequisites
106

11-
### Create Custom Role (Definition)
7+
You need a server running a Django project that uses DAB RBAC.
8+
Use test_app in this repo for a demo (see `test_app/README.md` for setup).
9+
The server runs at http://127.0.0.1:8000, and examples reference this base URL.
10+
Default admin password is "admin".
1211

13-
After logging in to test_app/, you can visit this endpoint to get the role definition API.
12+
### API Workflow Overview
1413

15-
http://127.0.0.1:8000/api/v1/role_definitions/
14+
The RBAC API follows standard REST practices where models have relational dependencies. The typical order for establishing permissions:
1615

17-
Perform an OPTIONS request, and you will find this gives choices for `content_type`.
18-
Out of the choices a single `content_type` needs to be chosen for a role definition.
16+
1. **Types** (`/api/v1/role_metadata/`) - Read-only, managed by app developers
17+
2. **Permissions** - Read-only, created when models are registered
18+
3. **Role Definitions** (`/api/v1/role_definitions/`) - Managed and custom roles
19+
4. **Role Assignments** - User assignments (`/api/v1/role_user_assignments/`) and team assignments (`/api/v1/role_team_assignments/`)
1920

20-
Multiple permissions can be selected, and are accepted in the form of a list.
21-
To find out what permissions are valid for a role definition for a given `content_type`
22-
make a GET to `/api/v1/role_metadata/` and look up the type under "allowed_permissions".
21+
Users interact primarily with steps 3 and 4, while steps 1 and 2 are controlled by the application configuration.
2322

24-
A POST to this endpoint will create a new role definition, example data:
23+
## Step 1: Understanding Available Types and Permissions
24+
25+
### View Available Content Types
26+
27+
Get the available content types and their permissions:
28+
29+
```
30+
GET /api/v1/role_metadata/
31+
```
32+
33+
This returns read-only information configured by app developers:
34+
- Available content types (e.g., "aap.inventory", "shared.organization")
35+
- Permissions allowed for each content type
36+
- No user interaction required with this data
37+
38+
## Step 2: Creating Role Definitions
39+
40+
### Create a Custom Role Definition
41+
42+
```
43+
POST /api/v1/role_definitions/
44+
```
45+
46+
Example request body:
2547

2648
```json
2749
{
2850
"permissions": ["view_inventory"],
2951
"content_type": "aap.inventory",
3052
"name": "View a single inventory",
31-
"description": "custom role"
53+
"description": "Custom role for inventory viewing"
3254
}
3355
```
3456

35-
Name can be any string that's not blank. Description can be any string.
57+
Required fields:
58+
- `permissions`: List of permission codenames (from step 1)
59+
- `content_type`: Content type string (from step 1)
60+
- `name`: Display name for the role definition
61+
62+
Optional fields:
63+
- `description`: Additional context about the role definition
64+
65+
### Managed vs Custom Role Definitions
66+
67+
- **Managed roles**: Created by app developers, cannot be modified via API
68+
- **Custom roles**: Created by users, fully manageable via API
3669

37-
### Assigning a User a Role to an Object
70+
## Step 3: User Role Assignments
3871

39-
Select a role definition from `/api/v1/role_definitions/`, use the id as `role_definition`
40-
and a user from `/api/v1/users/` and use the id as `user`.
41-
Given the type of the role definition, check the available objects of that type,
42-
in this case `/api/v1/inventories/` and obtain the desired id, this will become `object_id`.
72+
### Assign Role Definition to User for Object
73+
74+
Once you have a role definition, assign it to a user for a specific object:
75+
76+
```
77+
POST /api/v1/role_user_assignments/
78+
```
4379

44-
With all 3 ids ready, POST to http://127.0.0.1:8000/api/v1/role_user_assignments/
80+
Example request body:
4581

4682
```json
4783
{
@@ -51,26 +87,39 @@ With all 3 ids ready, POST to http://127.0.0.1:8000/api/v1/role_user_assignments
5187
}
5288
```
5389

54-
This will give user id=3 view permission to a single inventory id=3, assuming the role definition
55-
referenced is what was created in the last section.
90+
Required fields:
91+
- `role_definition`: ID from `/api/v1/role_definitions/`
92+
- `user`: ID from `/api/v1/users/`
93+
- `object_id`: ID of the target object (e.g., from `/api/v1/inventories/`)
5694

57-
### Assigning a User as a Member of a Team
95+
This grants the user the role definition's permissions for only the specified object.
5896

59-
While this is possible with the RBAC API, it is not covered here,
60-
because it may come from an external source.
97+
### Assign Role Definition to User Globally
6198

62-
The "member_team" permission may later be prohibited from use in custom role definitions.
99+
For system-wide permissions, omit the `object_id`:
63100

64-
### Assigning a Team a Role to an Object
101+
```json
102+
{
103+
"role_definition": 5,
104+
"user": 3
105+
}
106+
```
65107

66-
If you used the test_app bootstrap script, then you will find the user "angry_spud"
67-
is a member of the "awx_devs" team.
108+
The role definition must have `content_type: null` for global assignments.
68109

69-
Assuming the team id is 2, POST to
110+
## Step 4: Team Role Assignments
70111

71-
http://127.0.0.1:8000/api/v1/role_team_assignments/
112+
### Team Membership
113+
114+
First, users must be members of teams. Team membership grants the "member_team" permission and automatically inherits all permissions assigned to the team.
115+
116+
### Assign Role Definition to Team
117+
118+
```
119+
POST /api/v1/role_team_assignments/
120+
```
72121

73-
with data
122+
Example request body:
74123

75124
```json
76125
{
@@ -80,36 +129,58 @@ with data
80129
}
81130
```
82131

83-
This has a similar effect to user assignments, but in this case will give
84-
permissions to all users of a team
132+
This grants the role definition's permissions to all members of the team for the specified object.
133+
134+
## Management Operations
135+
136+
### View Existing Assignments
85137

86-
### Viewing Assignments
138+
List assignments for a specific object:
87139

88-
For the single inventory mentioned above, it is possible to view the existing permission
89-
assignments directly to that object.
140+
```
141+
GET /api/v1/role_user_assignments/?object_id=3&content_type__model=inventory
142+
GET /api/v1/role_team_assignments/?object_id=3&content_type__model=inventory
143+
```
90144

91-
GET
145+
### Revoke an Assignment
146+
147+
Delete a specific assignment by ID:
148+
149+
```
150+
DELETE /api/v1/role_user_assignments/1/
151+
```
92152

93-
http://127.0.0.1:8000/api/v1/role_team_assignments/?object_id=3&content_type__model=inventory
153+
This removes the assignment and all permissions it granted.
94154

95-
http://127.0.0.1:8000/api/v1/role_user_assignments/?object_id=3&content_type__model=inventory
155+
## Organization-Level Permissions
96156

97-
### Revoking an Assignment
157+
### Assigning Organization-Wide Permissions
98158

99-
From any of the assignment lists, the user can select an assignment to revoke.
100-
Follow the "url" in the serializer from either user or team assignment lists.
159+
To grant permissions to all objects within an organization:
101160

102-
DELETE http://127.0.0.1:8000/api/v1/role_user_assignments/1/
161+
1. **Create an organization-level role definition:**
162+
```json
163+
{
164+
"permissions": ["view_inventory", "change_inventory"],
165+
"content_type": "shared.organization",
166+
"name": "Organization Inventory Manager"
167+
}
168+
```
103169

104-
Will undo everything related to that assignment.
105-
The user or team's users will lose permission that was granted by the object-role-assignment.
170+
2. **Assign to user/team using organization ID:**
171+
```json
172+
{
173+
"role_definition": 4,
174+
"object_id": 1, // organization ID
175+
"user": 3
176+
}
177+
```
106178

107-
### Assigning Organization Permission
179+
This grants the specified permissions to all inventories within organization ID 1.
108180

109-
In the case of inventory, its parent object is its organization.
110-
Instead of giving permission to a single inventory, you can use roles to give
111-
permission to all inventories inside of an organization.
181+
## Error Handling
112182

113-
The difference in the above steps are that
114-
- when creating a custom role definition the `content_type` would be "shared.organization"
115-
- when creating the assignment, the `object_id` would be the id of an organization
183+
Common API errors:
184+
- **400 Bad Request**: Invalid role definition/object/user combination
185+
- **403 Forbidden**: Insufficient permissions to create assignment (you need "change" permission to the object to create role assignments)
186+
- **404 Not Found**: Referenced object does not exist

0 commit comments

Comments
 (0)