fix: return a 403 instead of a redirect from course_home_api api methods (#32210)

* fix: return 403 on course access error rather than redirect
This commit is contained in:
Jansen Kantor
2023-05-24 12:09:34 -04:00
committed by GitHub
parent bbb1fdbdf1
commit 7b91f8579f
6 changed files with 116 additions and 10 deletions

View File

@@ -0,0 +1,24 @@
""" Utilities for views in the course home api"""
from rest_framework.exceptions import PermissionDenied
from lms.djangoapps.courseware.courses import get_course_with_access as base_get_course_with_access
from lms.djangoapps.courseware.exceptions import CourseAccessRedirect
def get_course_or_403(*args, **kwargs):
"""
When we make requests to the various Learner Home API endpoints, we do not want to return the actual redirects,
Instead we should return an error code. The redirect info is returned from the course metadata endpoint and the
URL can be followed by whatever client is calling.
Raises:
- 404 if course is not found
- 403 if the requesting user does not have access to the course
"""
try:
return base_get_course_with_access(*args, **kwargs)
except CourseAccessRedirect as e:
raise PermissionDenied(
detail=e.access_error.user_message,
code=e.access_error.error_code
) from e