|
| 1 | +"""Django REST framework permissions.""" |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +from django.db.models import Q |
| 6 | +from learning_paths.models import LearningPath |
| 7 | +from opaque_keys import InvalidKeyError |
| 8 | +from opaque_keys.edx.keys import LearningContextKey |
| 9 | +from rest_framework.exceptions import NotFound, ParseError |
| 10 | +from rest_framework.permissions import BasePermission |
| 11 | + |
| 12 | +from learning_credentials.compat import get_course_enrollments |
| 13 | + |
| 14 | +if TYPE_CHECKING: |
| 15 | + from django.contrib.auth.models import User |
| 16 | + from learning_paths.keys import LearningPathKey |
| 17 | + from opaque_keys.edx.keys import CourseKey |
| 18 | + from rest_framework.request import Request |
| 19 | + from rest_framework.views import APIView |
| 20 | + |
| 21 | + |
| 22 | +class CanAccessLearningContext(BasePermission): |
| 23 | + """Permission to allow access to learning context if the user is enrolled.""" |
| 24 | + |
| 25 | + def has_permission(self, request: "Request", view: "APIView") -> bool: |
| 26 | + """Check if the user can access the learning context.""" |
| 27 | + try: |
| 28 | + key = view.kwargs.get("learning_context_key") or request.query_params.get("learning_context_key") |
| 29 | + learning_context_key = LearningContextKey.from_string(key) |
| 30 | + except InvalidKeyError as e: |
| 31 | + msg = "Invalid learning context key." |
| 32 | + raise ParseError(msg) from e |
| 33 | + |
| 34 | + if request.user.is_staff: |
| 35 | + return True |
| 36 | + |
| 37 | + if learning_context_key.is_course: |
| 38 | + if self._can_access_course(learning_context_key, request.user): |
| 39 | + return True |
| 40 | + |
| 41 | + msg = "Course not found or user does not have access." |
| 42 | + raise NotFound(msg) |
| 43 | + |
| 44 | + # For learning paths, check enrollment or if it's not invite-only. |
| 45 | + if self._can_access_learning_path(learning_context_key, request.user): |
| 46 | + return True |
| 47 | + |
| 48 | + msg = "Learning path not found or user does not have access." |
| 49 | + raise NotFound(msg) |
| 50 | + |
| 51 | + def _can_access_course(self, course_key: "CourseKey", user: "User") -> bool: |
| 52 | + """Check if user can access a course.""" |
| 53 | + # Check if user is enrolled in the course. |
| 54 | + if get_course_enrollments(course_key, user.id): # ty: ignore[unresolved-attribute] |
| 55 | + return True |
| 56 | + |
| 57 | + # Check if the course is a part of a learning path the user can access. |
| 58 | + return self._can_access_course_via_learning_path(course_key, user) |
| 59 | + |
| 60 | + def _get_accessible_learning_paths_filter(self, user: "User") -> Q: |
| 61 | + """Get Q filter for learning paths that the user can access.""" |
| 62 | + return Q(invite_only=False) | Q(learningpathenrollment__user=user, learningpathenrollment__is_active=True) |
| 63 | + |
| 64 | + def _can_access_course_via_learning_path(self, course_key: "CourseKey", user: "User") -> bool: |
| 65 | + """Check if user can access a course through learning path membership.""" |
| 66 | + accessible_paths = ( |
| 67 | + LearningPath.objects.filter(steps__course_key=course_key) |
| 68 | + .filter(self._get_accessible_learning_paths_filter(user)) |
| 69 | + .distinct() |
| 70 | + ) |
| 71 | + |
| 72 | + return accessible_paths.exists() |
| 73 | + |
| 74 | + def _can_access_learning_path(self, learning_path_key: "LearningPathKey", user: "User") -> bool: |
| 75 | + """Check if user can access a learning path.""" |
| 76 | + # Single query to check if learning path exists and user can access it |
| 77 | + accessible_path = LearningPath.objects.filter(key=learning_path_key).filter( |
| 78 | + self._get_accessible_learning_paths_filter(user) |
| 79 | + ) |
| 80 | + |
| 81 | + return accessible_path.exists() |
0 commit comments