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:
24
lms/djangoapps/course_home_api/utils.py
Normal file
24
lms/djangoapps/course_home_api/utils.py
Normal 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
|
||||
Reference in New Issue
Block a user