|
16 | 16 |
|
17 | 17 | from openedx_authz import api |
18 | 18 | from openedx_authz.constants import permissions |
| 19 | +from openedx_authz.engine.enforcer import AuthzEnforcer |
19 | 20 | from openedx_authz.rest_api.data import RoleOperationError, RoleOperationStatus |
20 | 21 | from openedx_authz.rest_api.decorators import authz_permissions, view_auth_classes |
21 | 22 | from openedx_authz.rest_api.utils import ( |
@@ -102,23 +103,23 @@ class PermissionValidationMeView(APIView): |
102 | 103 | ) |
103 | 104 | def post(self, request: HttpRequest) -> Response: |
104 | 105 | """Validate one or more permissions for the authenticated user.""" |
| 106 | + AuthzEnforcer.get_enforcer().load_policy() |
| 107 | + |
105 | 108 | serializer = PermissionValidationSerializer(data=request.data, many=True) |
106 | 109 | serializer.is_valid(raise_exception=True) |
| 110 | + data = serializer.validated_data |
107 | 111 |
|
108 | 112 | username = request.user.username |
109 | 113 | response_data = [] |
110 | | - for perm in serializer.validated_data: |
| 114 | + for permission in data: |
111 | 115 | try: |
112 | | - action = perm["action"] |
113 | | - scope = perm["scope"] |
| 116 | + action = permission["action"] |
| 117 | + scope = permission["scope"] |
114 | 118 | allowed = api.is_user_allowed(username, action, scope) |
115 | 119 | response_data.append({"action": action, "scope": scope, "allowed": allowed}) |
116 | 120 | except ValueError as e: |
117 | 121 | logger.error(f"Error validating permission for user {username}: {e}") |
118 | | - return Response( |
119 | | - data={"message": "Invalid scope format"}, |
120 | | - status=status.HTTP_400_BAD_REQUEST, |
121 | | - ) |
| 122 | + return Response(data={"message": "Invalid scope format"}, status=status.HTTP_400_BAD_REQUEST) |
122 | 123 | except Exception as e: # pylint: disable=broad-exception-caught |
123 | 124 | logger.error(f"Error validating permission for user {username}: {e}") |
124 | 125 | return Response( |
@@ -283,15 +284,14 @@ def put(self, request: HttpRequest) -> Response: |
283 | 284 | """Assign multiple users to a specific role within a scope.""" |
284 | 285 | serializer = AddUsersToRoleWithScopeSerializer(data=request.data) |
285 | 286 | serializer.is_valid(raise_exception=True) |
| 287 | + data = serializer.validated_data |
286 | 288 |
|
287 | | - role = serializer.validated_data["role"] |
288 | | - scope = serializer.validated_data["scope"] |
289 | 289 | completed, errors = [], [] |
290 | | - for user_identifier in serializer.validated_data["users"]: |
| 290 | + for user_identifier in data["users"]: |
291 | 291 | response_dict = {"user_identifier": user_identifier} |
292 | 292 | try: |
293 | 293 | user = get_user_by_username_or_email(user_identifier) |
294 | | - result = api.assign_role_to_user_in_scope(user.username, role, scope) |
| 294 | + result = api.assign_role_to_user_in_scope(user.username, data["role"], data["scope"]) |
295 | 295 | if result: |
296 | 296 | response_dict["status"] = RoleOperationStatus.ROLE_ADDED |
297 | 297 | completed.append(response_dict) |
@@ -330,15 +330,14 @@ def delete(self, request: HttpRequest) -> Response: |
330 | 330 | """Remove multiple users from a specific role within a scope.""" |
331 | 331 | serializer = RemoveUsersFromRoleWithScopeSerializer(data=request.query_params) |
332 | 332 | serializer.is_valid(raise_exception=True) |
| 333 | + data = serializer.validated_data |
333 | 334 |
|
334 | | - role = serializer.validated_data["role"] |
335 | | - scope = serializer.validated_data["scope"] |
336 | 335 | completed, errors = [], [] |
337 | | - for user_identifier in serializer.validated_data["users"]: |
| 336 | + for user_identifier in data["users"]: |
338 | 337 | response_dict = {"user_identifier": user_identifier} |
339 | 338 | try: |
340 | 339 | user = get_user_by_username_or_email(user_identifier) |
341 | | - result = api.unassign_role_from_user(user.username, role, scope) |
| 340 | + result = api.unassign_role_from_user(user.username, data["role"], data["scope"]) |
342 | 341 | if result: |
343 | 342 | response_dict["status"] = RoleOperationStatus.ROLE_REMOVED |
344 | 343 | completed.append(response_dict) |
|
0 commit comments