|
| 1 | +"""Decorators for AuthZ-based permissions enforcement.""" |
| 2 | +import logging |
| 3 | +from functools import wraps |
| 4 | +from collections.abc import Callable |
| 5 | + |
| 6 | +from django.contrib.auth.models import AbstractUser |
| 7 | +from opaque_keys import InvalidKeyError |
| 8 | +from opaque_keys.edx.keys import CourseKey, UsageKey |
| 9 | +from openedx.core.djangoapps.authz.constants import LEGACY_PERMISSION_HANDLER_MAP, LegacyAuthoringPermission |
| 10 | +from openedx_authz import api as authz_api |
| 11 | +from rest_framework import status |
| 12 | + |
| 13 | +from openedx.core import toggles as core_toggles |
| 14 | +from openedx.core.lib.api.view_utils import DeveloperErrorViewMixin |
| 15 | + |
| 16 | +log = logging.getLogger(__name__) |
| 17 | + |
| 18 | + |
| 19 | +def authz_permission_required( |
| 20 | + authz_permission: str, |
| 21 | + legacy_permission: LegacyAuthoringPermission | None = None) -> Callable: |
| 22 | + """ |
| 23 | + Decorator enforcing course author permissions via AuthZ |
| 24 | + with optional legacy fallback. |
| 25 | +
|
| 26 | + This decorator checks if the requesting user has the specified AuthZ permission for the course. |
| 27 | + If AuthZ is not enabled for the course, and a legacy_permission is provided, it falls back to checking |
| 28 | + the legacy permission. |
| 29 | +
|
| 30 | + Raises: |
| 31 | + DeveloperErrorResponseException: If the user does not have the required permissions. |
| 32 | + """ |
| 33 | + |
| 34 | + def decorator(view_func): |
| 35 | + |
| 36 | + @wraps(view_func) |
| 37 | + def _wrapped_view(self, request, course_id, *args, **kwargs): |
| 38 | + course_key = get_course_key(course_id) |
| 39 | + |
| 40 | + if not user_has_course_permission( |
| 41 | + request.user, |
| 42 | + authz_permission, |
| 43 | + course_key, |
| 44 | + legacy_permission |
| 45 | + ): |
| 46 | + raise DeveloperErrorViewMixin.api_error( |
| 47 | + status_code=status.HTTP_403_FORBIDDEN, |
| 48 | + developer_message="You do not have permission to perform this action.", |
| 49 | + error_code="permission_denied", |
| 50 | + ) |
| 51 | + |
| 52 | + return view_func(self, request, course_key, *args, **kwargs) |
| 53 | + |
| 54 | + return _wrapped_view |
| 55 | + |
| 56 | + return decorator |
| 57 | + |
| 58 | + |
| 59 | +def user_has_course_permission( |
| 60 | + user: AbstractUser, |
| 61 | + authz_permission: str, |
| 62 | + course_key: CourseKey, |
| 63 | + legacy_permission: LegacyAuthoringPermission | None = None, |
| 64 | +) -> bool: |
| 65 | + """ |
| 66 | + Checks if the user has the specified AuthZ permission for the course, |
| 67 | + with optional fallback to legacy permissions. |
| 68 | + """ |
| 69 | + if core_toggles.enable_authz_course_authoring(course_key): |
| 70 | + # If AuthZ is enabled for this course, check the permission via AuthZ only. |
| 71 | + is_user_allowed = authz_api.is_user_allowed(user.username, authz_permission, str(course_key)) |
| 72 | + log.info( |
| 73 | + "AuthZ permission granted = {}".format(is_user_allowed), |
| 74 | + extra={ |
| 75 | + "user_id": user.id, |
| 76 | + "authz_permission": authz_permission, |
| 77 | + "course_key": str(course_key), |
| 78 | + }, |
| 79 | + ) |
| 80 | + return is_user_allowed |
| 81 | + |
| 82 | + # If AuthZ is not enabled for this course, fall back to legacy course author |
| 83 | + # access check if legacy_permission is provided. |
| 84 | + has_legacy_permission: Callable | None = LEGACY_PERMISSION_HANDLER_MAP.get(legacy_permission) |
| 85 | + if legacy_permission and has_legacy_permission and has_legacy_permission(user, course_key): |
| 86 | + log.info( |
| 87 | + "AuthZ fallback used", |
| 88 | + extra={ |
| 89 | + "user_id": user.id, |
| 90 | + "authz_permission": authz_permission, |
| 91 | + "legacy_permission": legacy_permission, |
| 92 | + "course_key": str(course_key), |
| 93 | + }, |
| 94 | + ) |
| 95 | + return True |
| 96 | + |
| 97 | + log.info( |
| 98 | + "AuthZ permission denied", |
| 99 | + extra={ |
| 100 | + "user_id": user.id, |
| 101 | + "authz_permission": authz_permission, |
| 102 | + "course_key": str(course_key), |
| 103 | + }, |
| 104 | + ) |
| 105 | + return False |
| 106 | + |
| 107 | + |
| 108 | +def get_course_key(course_id: str) -> CourseKey: |
| 109 | + """ |
| 110 | + Given a course_id string, attempts to parse it as a CourseKey. |
| 111 | + If that fails, attempts to parse it as a UsageKey and extract the course key from it. |
| 112 | + """ |
| 113 | + try: |
| 114 | + return CourseKey.from_string(course_id) |
| 115 | + except InvalidKeyError: |
| 116 | + # If the course_id doesn't match the COURSE_KEY_PATTERN, it might be a usage key. |
| 117 | + # Attempt to parse it as such and extract the course key. |
| 118 | + usage_key = UsageKey.from_string(course_id) |
| 119 | + return usage_key.course_key |
0 commit comments