Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lms/djangoapps/instructor/authz/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Every decision is logged with the path that resolved it (``authz`` or ``legacy``
Migrating a view
****************

Replace ``InstructorPermission`` by ``InstructorAuthzPermission`` and declare the AuthZ permission next to the legacy one. ``course_id`` must be a URL kwarg: the permission class reads it from ``view.kwargs`` and returns a ``404`` if it is not a valid course key. Pick the case that matches the view:
Replace ``InstructorPermission`` by ``InstructorAuthzPermission`` and declare the AuthZ permission next to the legacy one. ``course_id`` must be a URL kwarg: the permission class reads it from ``view.kwargs`` and returns a ``404`` if it is not a valid course key. If the view's URL pattern names that kwarg something else (e.g. the cohorts API's ``course_key_string``), set ``course_id_url_kwarg`` on the view to match. Pick the case that matches the view:

Case 1: Same permissions for every HTTP method
==============================================
Expand Down
30 changes: 25 additions & 5 deletions lms/djangoapps/instructor/authz/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@
)


def is_django_staff(user: AbstractUser, course_key: CourseKey) -> bool: # pylint: disable=unused-argument
"""
Check the legacy access of the endpoints restricted to Django staff.

It follows the same rule as DRF's ``IsAdminUser`` (``user.is_staff``), regardless
of the course. It is not a default of ``LEGACY_PERMISSION_CHECKS``: views pass it
as their ``legacy_permission_check``.

Args:
user (AbstractUser): The user to check.
course_key (CourseKey): The course to check. It is not used, as the rule
does not depend on the course.

Returns:
bool: True for Django staff. Course staff and course instructors are not
included.
"""
return bool(user and user.is_staff)


def has_staff_access(user: AbstractUser, course_key: CourseKey) -> bool:
"""
Check whether the user has course staff access.
Expand Down Expand Up @@ -88,9 +108,9 @@ def has_cohort_manage_access(user: AbstractUser, course_key: CourseKey) -> bool:
"""
Check the legacy access to change cohorts.

It applies to any non-GET request of the cohorts API and uses the same rule
as ``IsStaffOrAdmin`` (``openedx.core.djangoapps.course_groups.permissions``)
for those requests.
It applies to any non-GET request of the cohorts API. Forum roles are not
enough here: only global staff, superusers and course staff or instructors
can change cohorts.

Args:
user (AbstractUser): The user to check.
Expand All @@ -110,8 +130,8 @@ def has_cohort_view_access(user: AbstractUser, course_key: CourseKey) -> bool:
"""
Check the legacy access to read cohorts (GET requests of the cohorts API).

It uses the same rule as ``IsStaffOrAdmin``
(``openedx.core.djangoapps.course_groups.permissions``) for GET requests.
It applies to GET requests of the cohorts API, where the forum Administrator,
Moderator and Community TA roles are also allowed to read.

Args:
user (AbstractUser): The user to check.
Expand Down
14 changes: 10 additions & 4 deletions lms/djangoapps/instructor/authz/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class InstructorAuthzPermission(BasePermission):
legacy_permission_check (Callable[[User, CourseKey], bool]): Optional.
Replaces the ``permission_name`` check, for legacy rules that are
not a bridgekeeper permission.
course_id_url_kwarg (str): Optional. The name of the URL kwarg holding
the course key. Defaults to ``"course_id"``, the one every
Instructor Dashboard v2 view uses; set it on the view when a
migrated endpoint uses a different name (e.g. the cohorts API's
``course_key_string``).
"""

def has_permission(self, request, view):
Expand All @@ -59,15 +64,16 @@ def has_permission(self, request, view):
bool: True if access is granted.

Raises:
Http404: If the ``course_id`` kwarg of the view is not a valid
course key.
Http404: If the course key URL kwarg of the view is missing or not
a valid course key.
ImproperlyConfigured: If the view does not define
``authz_permissions``, or defines a bare string instead of a
collection.
"""
course_id_url_kwarg = getattr(view, "course_id_url_kwarg", "course_id")
try:
course_key = CourseKey.from_string(view.kwargs.get("course_id"))
except InvalidKeyError as exc:
course_key = CourseKey.from_string(view.kwargs.get(course_id_url_kwarg))
except (InvalidKeyError, TypeError) as exc:
raise Http404("Invalid course key") from exc

authz_permissions = getattr(view, "authz_permissions", None)
Expand Down
Loading
Loading