Skip to content

Commit 3bad870

Browse files
committed
Review comments
1 parent 8dabe43 commit 3bad870

File tree

1 file changed

+34
-58
lines changed

1 file changed

+34
-58
lines changed

ansible_base/rbac/api/views.py

Lines changed: 34 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -237,17 +237,34 @@ class RoleTeamAssignmentViewSet(BaseAssignmentViewSet):
237237
]
238238

239239

240-
class RoleUserAssignmentViewSet(BaseAssignmentViewSet):
241-
"""
242-
Use this endpoint to give a user permission to a resource or an organization.
243-
The needed data is the user, the role definition, and the object id.
244-
The object must be of the type specified in the role definition.
245-
The type given in the role definition and the provided object_id are used
246-
to look up the resource.
240+
# Schema fragments for RoleUserAssignmentViewSet OpenAPI spec
241+
_USER_ACTOR_ONEOF = {
242+
'oneOf': [
243+
{'required': ['user'], 'not': {'required': ['user_ansible_id']}},
244+
{'required': ['user_ansible_id'], 'not': {'required': ['user']}},
245+
]
246+
}
247247

248-
After creation, the assignment cannot be edited, but can be deleted to
249-
remove those permissions.
250-
"""
248+
_OBJECT_ID_ONEOF = {
249+
'oneOf': [
250+
{'properties': {'object_id': {'oneOf': [{'type': 'integer'}, {'type': 'string', 'format': 'uuid'}]}, 'object_ansible_id': False}},
251+
{'properties': {'object_ansible_id': {'type': 'string', 'format': 'uuid'}, 'object_id': False}},
252+
{'not': {'anyOf': [{'required': ['object_id']}, {'required': ['object_ansible_id']}]}},
253+
]
254+
}
255+
256+
_ROLE_USER_ASSIGNMENT_REQUEST_SCHEMA = {
257+
'schema': {
258+
'allOf': [
259+
{'$ref': '#/components/schemas/RoleUserAssignment'},
260+
_USER_ACTOR_ONEOF,
261+
_OBJECT_ID_ONEOF,
262+
]
263+
}
264+
}
265+
266+
267+
class RoleUserAssignmentViewSet(BaseAssignmentViewSet):
251268

252269
resource_purpose = "RBAC role grants assigning permissions to users for specific resources"
253270

@@ -260,55 +277,14 @@ class RoleUserAssignmentViewSet(BaseAssignmentViewSet):
260277

261278
@extend_schema_if_available(
262279
request={
263-
'application/json': {
264-
'schema': {
265-
'allOf': [
266-
{'$ref': '#/components/schemas/RoleUserAssignment'}, # Keep auto-generated schema
267-
{
268-
'oneOf': [
269-
{'required': ['user'], 'not': {'required': ['user_ansible_id']}},
270-
{'required': ['user_ansible_id'], 'not': {'required': ['user']}},
271-
]
272-
},
273-
{
274-
'oneOf': [
275-
{'properties': {'object_id': {'type': 'integer'}, 'object_ansible_id': False}},
276-
{'properties': {'object_ansible_id': {'type': 'string', 'format': 'uuid'}, 'object_id': False}},
277-
{'not': {'anyOf': [{'required': ['object_id']}, {'required': ['object_ansible_id']}]}},
278-
]
279-
},
280-
]
281-
}
282-
},
283-
'application/x-www-form-urlencoded': {
284-
'schema': {
285-
'allOf': [
286-
{'$ref': '#/components/schemas/RoleUserAssignment'},
287-
{
288-
'oneOf': [
289-
{'required': ['user'], 'not': {'required': ['user_ansible_id']}},
290-
{'required': ['user_ansible_id'], 'not': {'required': ['user']}},
291-
]
292-
},
293-
]
294-
}
295-
},
296-
'multipart/form-data': {
297-
'schema': {
298-
'allOf': [
299-
{'$ref': '#/components/schemas/RoleUserAssignment'},
300-
{
301-
'oneOf': [
302-
{'required': ['user'], 'not': {'required': ['user_ansible_id']}},
303-
{'required': ['user_ansible_id'], 'not': {'required': ['user']}},
304-
]
305-
},
306-
]
307-
}
308-
},
280+
'application/json': _ROLE_USER_ASSIGNMENT_REQUEST_SCHEMA,
281+
'application/x-www-form-urlencoded': _ROLE_USER_ASSIGNMENT_REQUEST_SCHEMA,
282+
'multipart/form-data': _ROLE_USER_ASSIGNMENT_REQUEST_SCHEMA,
309283
},
310-
description="Create role assignment. Must specify 'role_definition' and exactly one of 'user' or 'user_ansible_id'. "
311-
"Can specify at most one of 'object_id' or 'object_ansible_id' (omit both for global roles).",
284+
description="Give a user permission to a resource, an organization, or globally (when allowed). Must specify 'role_definition' and exactly one of 'user' or 'user_ansible_id'. "
285+
"Can specify at most one of 'object_id' or 'object_ansible_id' (omit both for global roles)."
286+
"The content_type of the role definition and the provided object_id are used to look up the resource."
287+
"After creation, the assignment cannot be edited, but can be deleted to remove those permissions.",
312288
)
313289
def create(self, request, *args, **kwargs):
314290
return super().create(request, *args, **kwargs)

0 commit comments

Comments
 (0)