forked from openedx/openedx-authz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
467 lines (374 loc) · 18.3 KB
/
views.py
File metadata and controls
467 lines (374 loc) · 18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
"""
REST API views for Open edX Authorization (AuthZ) system.
This module provides Django REST Framework views for managing authorization
permissions, roles, and user assignments within Open edX platform.
"""
import logging
import edx_api_doc_tools as apidocs
from django.contrib.auth import get_user_model
from django.http import HttpRequest
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from openedx_authz import api
from openedx_authz.rest_api.data import RoleOperationError, RoleOperationStatus
from openedx_authz.rest_api.decorators import authz_permissions, view_auth_classes
from openedx_authz.rest_api.utils import (
filter_users,
get_generic_scope,
get_user_by_username_or_email,
get_user_map,
sort_users,
)
from openedx_authz.rest_api.v1.paginators import AuthZAPIViewPagination
from openedx_authz.rest_api.v1.permissions import DynamicScopePermission
from openedx_authz.rest_api.v1.serializers import (
AddUsersToRoleWithScopeSerializer,
ListRolesWithScopeResponseSerializer,
ListRolesWithScopeSerializer,
ListUsersInRoleWithScopeSerializer,
PermissionValidationResponseSerializer,
PermissionValidationSerializer,
RemoveUsersFromRoleWithScopeSerializer,
UserRoleAssignmentSerializer,
)
logger = logging.getLogger(__name__)
User = get_user_model()
@view_auth_classes()
class PermissionValidationMeView(APIView):
"""
API view for validating user permissions against authorization policies.
This view enables authenticated users to verify their permissions for specific
actions and scopes within the Open edX authorization system. It supports batch
validation of multiple permissions in a single request.
**Endpoints**
- POST: Validate one or more permissions for the authenticated user
**Request Format**
Expects a list of permission objects, each containing:
- action: The action to validate (e.g., 'edit_library', 'delete_library_content')
- scope: The authorization scope (e.g., 'lib:DemoX:CSPROB')
**Response Format**
Returns a list of validation results, each containing:
- action: The requested action
- scope: The requested scope
- allowed: Boolean indicating if the user has the permission
**Authentication and Permissions**
- Requires authenticated user.
**Example Request**
POST /api/authz/v1/permissions/validate/me
.. code-block:: json
[
{"action": "edit_library", "scope": "lib:DemoX:CSPROB"},
{"action": "delete_library_content", "scope": "lib:OpenedX:CS50"}
]
**Example Response**
.. code-block:: json
[
{"action": "edit_library", "scope": "lib:DemoX:CSPROB", "allowed": true},
{"action": "delete_library_content", "scope": "lib:OpenedX:CS50", "allowed": false}
]
"""
@apidocs.schema(
body=PermissionValidationSerializer(help_text="The permissions to validate", many=True),
responses={
status.HTTP_200_OK: PermissionValidationResponseSerializer,
status.HTTP_400_BAD_REQUEST: "The request data is invalid",
status.HTTP_401_UNAUTHORIZED: "The user is not authenticated",
},
)
def post(self, request: HttpRequest) -> Response:
"""Validate one or more permissions for the authenticated user."""
serializer = PermissionValidationSerializer(data=request.data, many=True)
serializer.is_valid(raise_exception=True)
username = request.user.username
response_data = []
for perm in serializer.validated_data:
try:
action = perm["action"]
scope = perm["scope"]
allowed = api.is_user_allowed(username, action, scope)
response_data.append(
{
"action": action,
"scope": scope,
"allowed": allowed,
}
)
except ValueError as e:
logger.error(f"Error validating permission for user {username}: {e}")
return Response(data={"message": "Invalid scope format"}, status=status.HTTP_400_BAD_REQUEST)
except Exception as e: # pylint: disable=broad-exception-caught
logger.error(f"Error validating permission for user {username}: {e}")
return Response(
data={"message": "An error occurred while validating permissions"},
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
serializer = PermissionValidationResponseSerializer(response_data, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
@view_auth_classes()
class RoleUserAPIView(APIView):
"""
API view for managing user-role assignments within specific authorization scopes.
This view provides comprehensive role management capabilities, allowing administrators
to view, assign, and remove role assignments for users within a given scope. It supports
bulk operations for adding and removing multiple users, along with filtering, searching,
sorting, and pagination of results.
**Endpoints**
- GET: Retrieve all users with their role assignments in a scope
- PUT: Assign multiple users to a specific role within a scope
- DELETE: Remove multiple users from a specific role within a scope
**Query Parameters (GET)**
- scope (Required): The authorization scope to query (e.g., 'lib:DemoX:CSPROB')
- search (Optional): Search term to filter users by username, email or full name
- roles (Optional): Filter by comma-separated list of specific role names
- page (Optional): Page number for pagination
- page_size (Optional): Number of items per page
- sort_by (Optional): Field to sort by (e.g., 'username', 'email', 'full_name')
- order (Optional): Sort order ('asc' or 'desc')
**Request Format (PUT)**
- users: List of user identifiers (username or email)
- role: The role to add users to
- scope: The scope to add users to
**Request Format (DELETE)**
Query parameters:
- users: Comma-separated list of user identifiers (username or email)
- role: The role to remove users from
- scope: The scope to remove users from
**Response Format (GET)**
Returns HTTP 200 OK with:
.. code-block:: json
{
"count": 2,
"next": null,
"previous": null,
"results": [
{
"username": "john_doe",
"email": "[email protected]",
"full_name": "John Doe"
"roles": ["library_admin", "library_user"]
},
{
"username": "jane_doe",
"email": "[email protected]",
"full_name": "Jane Doe"
"roles": ["library_user"]
}
]
}
**Response Format (PUT)**
Returns HTTP 207 Multi-Status with:
.. code-block:: json
{
"completed": [{"user_identifier": "john_doe", "status": "role_added"}],
"errors": [{"user_identifier": "jane_doe", "error": "user_already_has_role"}]
}
**Response Format (DELETE)**
Returns HTTP 207 Multi-Status with:
.. code-block:: json
{
"completed": [{"user_identifier": "john_doe", "status": "role_removed"}],
"errors": [{"user_identifier": "jane_doe", "error": "user_does_not_have_role"}]
}
**Authentication and Permissions**
- Requires authenticated user.
- Requires ``manage_library_team`` permission for the scope.
**Example Request**
GET /api/authz/v1/roles/users/?scope=lib:DemoX:CSPROB&search=john&roles=library_admin
PUT /api/authz/v1/roles/users/
.. code-block:: json
{
"role": "library_admin",
"scope": "lib:DemoX:CSPROB",
"users": ["[email protected]", "username2"]
}
DELETE /api/authz/v1/roles/users/?role=library_admin&scope=lib:DemoX:CSPROB&[email protected],username2
"""
pagination_class = AuthZAPIViewPagination
permission_classes = [DynamicScopePermission]
@apidocs.schema(
parameters=[
apidocs.query_parameter("scope", str, description="The authorization scope for the role"),
apidocs.query_parameter("search", str, description="The search query to filter users by"),
apidocs.query_parameter("roles", str, description="The names of the roles to query"),
apidocs.query_parameter("page", int, description="Page number for pagination"),
apidocs.query_parameter("page_size", int, description="Number of items per page"),
apidocs.query_parameter("sort_by", str, description="The field to sort by"),
apidocs.query_parameter("order", str, description="The order to sort by"),
],
responses={
status.HTTP_200_OK: "The users were retrieved successfully",
status.HTTP_400_BAD_REQUEST: "The request parameters are invalid",
status.HTTP_401_UNAUTHORIZED: "The user is not authenticated or does not have the required permissions",
},
)
@authz_permissions(["view_library_team"])
def get(self, request: HttpRequest) -> Response:
"""Retrieve all users with role assignments within a specific scope."""
serializer = ListUsersInRoleWithScopeSerializer(data=request.query_params)
serializer.is_valid(raise_exception=True)
query_params = serializer.validated_data
user_role_assignments = api.get_all_user_role_assignments_in_scope(query_params["scope"])
usernames = {assignment.subject.username for assignment in user_role_assignments}
context = {"user_map": get_user_map(usernames)}
serialized_data = UserRoleAssignmentSerializer(user_role_assignments, many=True, context=context)
filtered_users = filter_users(serialized_data.data, query_params["search"], query_params["roles"])
user_role_assignments = sort_users(filtered_users, query_params["sort_by"], query_params["order"])
paginator = self.pagination_class()
paginated_response_data = paginator.paginate_queryset(user_role_assignments, request)
return paginator.get_paginated_response(paginated_response_data)
@apidocs.schema(
body=AddUsersToRoleWithScopeSerializer,
responses={
status.HTTP_207_MULTI_STATUS: "The users were added to the role",
status.HTTP_400_BAD_REQUEST: "The request data is invalid",
status.HTTP_401_UNAUTHORIZED: "The user is not authenticated or does not have the required permissions",
},
)
@authz_permissions(["manage_library_team"])
def put(self, request: HttpRequest) -> Response:
"""Assign multiple users to a specific role within a scope."""
serializer = AddUsersToRoleWithScopeSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
role = serializer.validated_data["role"]
scope = serializer.validated_data["scope"]
completed, errors = [], []
for user_identifier in serializer.validated_data["users"]:
response_dict = {"user_identifier": user_identifier}
try:
user = get_user_by_username_or_email(user_identifier)
result = api.assign_role_to_user_in_scope(user.username, role, scope)
if result:
response_dict["status"] = RoleOperationStatus.ROLE_ADDED
completed.append(response_dict)
else:
response_dict["error"] = RoleOperationError.USER_ALREADY_HAS_ROLE
errors.append(response_dict)
except User.DoesNotExist:
response_dict["error"] = RoleOperationError.USER_NOT_FOUND
errors.append(response_dict)
except Exception as e: # pylint: disable=broad-exception-caught
logger.error(f"Error assigning role to user {user_identifier}: {e}")
response_dict["error"] = RoleOperationError.ROLE_ASSIGNMENT_ERROR
errors.append(response_dict)
response_data = {"completed": completed, "errors": errors}
return Response(response_data, status=status.HTTP_207_MULTI_STATUS)
@apidocs.schema(
parameters=[
apidocs.query_parameter(
"users", str, description="List of user identifiers (username or email) separated by a comma"
),
apidocs.query_parameter("role", str, description="The role to remove the users from"),
apidocs.query_parameter("scope", str, description="The scope to remove the users from"),
],
responses={
status.HTTP_207_MULTI_STATUS: "The users were removed from the role",
status.HTTP_400_BAD_REQUEST: "The request parameters are invalid",
status.HTTP_401_UNAUTHORIZED: "The user is not authenticated or does not have the required permissions",
},
)
@authz_permissions(["manage_library_team"])
def delete(self, request: HttpRequest) -> Response:
"""Remove multiple users from a specific role within a scope."""
serializer = RemoveUsersFromRoleWithScopeSerializer(data=request.query_params)
serializer.is_valid(raise_exception=True)
role = serializer.validated_data["role"]
scope = serializer.validated_data["scope"]
completed, errors = [], []
for user_identifier in serializer.validated_data["users"]:
response_dict = {"user_identifier": user_identifier}
try:
user = get_user_by_username_or_email(user_identifier)
result = api.unassign_role_from_user(user.username, role, scope)
if result:
response_dict["status"] = RoleOperationStatus.ROLE_REMOVED
completed.append(response_dict)
else:
response_dict["error"] = RoleOperationError.USER_DOES_NOT_HAVE_ROLE
errors.append(response_dict)
except User.DoesNotExist:
response_dict["error"] = RoleOperationError.USER_NOT_FOUND
errors.append(response_dict)
except Exception as e: # pylint: disable=broad-exception-caught
logger.error(f"Error removing role from user {user_identifier}: {e}")
response_dict["error"] = RoleOperationError.ROLE_REMOVAL_ERROR
errors.append(response_dict)
response_data = {"completed": completed, "errors": errors}
return Response(response_data, status=status.HTTP_207_MULTI_STATUS)
@view_auth_classes()
class RoleListView(APIView):
"""API view for retrieving role definitions and their associated permissions within a specific scope.
This view provides read-only access to role definitions within a specific
authorization scope. It returns detailed information about each role including
the permissions granted and the number of users assigned to each role.
**Endpoints**
- GET: Retrieve all roles and their permissions for a specific scope
**Query Parameters**
- scope (Required): The scope to query roles for (e.g., 'lib:OpenedX:CSPROB')
- page (Optional): Page number for pagination
- page_size (Optional): Number of items per page
**Response Format**
Returns a paginated list of role objects, each containing:
- role: The role's external identifier (e.g., 'library_author', 'library_user')
- permissions: List of permission action keys granted by this role (e.g., 'delete_library_content')
- user_count: Number of users currently assigned to this role
**Authentication and Permissions**
- Requires authenticated user.
- Requires ``manage_library_team`` permission for the scope.
**Example Request**
GET /api/authz/v1/roles/?scope=lib:OpenedX:CSPROB&page=1&page_size=10
**Example Response**
.. code-block:: json
{
"count": 2,
"next": null,
"previous": null,
"results": [
{
"role": "library_author",
"permissions": ["delete_library_content", "edit_library"],
"user_count": 5
},
{
"role": "library_user",
"permissions": ["view_library", "view_library_team", "reuse_library_content"],
"user_count": 12
}
]
}
"""
pagination_class = AuthZAPIViewPagination
permission_classes = [DynamicScopePermission]
@apidocs.schema(
parameters=[
apidocs.query_parameter("scope", str, description="The scope to query roles for"),
apidocs.query_parameter("page", int, description="Page number for pagination"),
apidocs.query_parameter("page_size", int, description="Number of items per page"),
],
responses={
status.HTTP_200_OK: ListRolesWithScopeResponseSerializer(many=True),
status.HTTP_400_BAD_REQUEST: "The request parameters are invalid",
status.HTTP_401_UNAUTHORIZED: "The user is not authenticated or does not have the required permissions",
},
)
@authz_permissions(["view_library_team"])
def get(self, request: HttpRequest) -> Response:
"""Retrieve all roles and their permissions for a specific scope."""
serializer = ListRolesWithScopeSerializer(data=request.query_params)
serializer.is_valid(raise_exception=True)
generic_scope = get_generic_scope(serializer.validated_data["scope"])
roles = api.get_role_definitions_in_scope(generic_scope)
response_data = []
for role in roles:
users = api.get_users_for_role(role.external_key)
response_data.append(
{
"role": role.external_key,
"permissions": role.get_permission_identifiers(),
"user_count": len(users),
}
)
paginator = self.pagination_class()
paginated_response_data = paginator.paginate_queryset(response_data, request)
serialized_data = ListRolesWithScopeResponseSerializer(paginated_response_data, many=True)
return paginator.get_paginated_response(serialized_data.data)