diff --git a/lms/djangoapps/instructor/authz/constants.py b/lms/djangoapps/instructor/authz/constants.py
index 38a48c90a829..a861a34e7379 100644
--- a/lms/djangoapps/instructor/authz/constants.py
+++ b/lms/djangoapps/instructor/authz/constants.py
@@ -8,6 +8,7 @@
from openedx_authz.authz import (
COURSES_ENROLL_LEARNERS,
+ COURSES_MANAGE_BULK_LEARNER_GRADING,
COURSES_MANAGE_COHORTS,
COURSES_MANAGE_COURSE_TEAM,
COURSES_MANAGE_LEARNER_EXTENSIONS,
@@ -36,6 +37,7 @@
COURSES_VIEW_GRADING_SETTINGS.identifier,
COURSES_MANAGE_LEARNER_GRADING.identifier,
COURSES_VIEW_LEARNER_COURSE_STATUS.identifier,
+ COURSES_MANAGE_BULK_LEARNER_GRADING.identifier,
COURSES_VIEW_LEARNER_EXTENSIONS.identifier,
COURSES_MANAGE_LEARNER_EXTENSIONS.identifier,
COURSES_VIEW_COHORTS.identifier,
diff --git a/lms/djangoapps/instructor/authz/legacy.py b/lms/djangoapps/instructor/authz/legacy.py
index bbc428b675e9..0cb8caf03a90 100644
--- a/lms/djangoapps/instructor/authz/legacy.py
+++ b/lms/djangoapps/instructor/authz/legacy.py
@@ -10,6 +10,7 @@
from opaque_keys.edx.keys import CourseKey
from openedx_authz.authz import (
COURSES_ENROLL_LEARNERS,
+ COURSES_MANAGE_BULK_LEARNER_GRADING,
COURSES_MANAGE_COHORTS,
COURSES_MANAGE_COURSE_TEAM,
COURSES_MANAGE_LEARNER_EXTENSIONS,
@@ -216,6 +217,7 @@ def _check(user: AbstractUser, course_key: CourseKey) -> bool:
COURSES_VIEW_GRADING_SETTINGS.identifier: has_grading_settings_access,
COURSES_MANAGE_LEARNER_GRADING.identifier: bridgekeeper_check(permissions.OVERRIDE_GRADES),
COURSES_VIEW_LEARNER_COURSE_STATUS.identifier: bridgekeeper_check(permissions.VIEW_DASHBOARD),
+ COURSES_MANAGE_BULK_LEARNER_GRADING.identifier: has_instructor_access,
COURSES_VIEW_LEARNER_EXTENSIONS.identifier: bridgekeeper_check(permissions.VIEW_DASHBOARD),
COURSES_MANAGE_LEARNER_EXTENSIONS.identifier: bridgekeeper_check(permissions.GIVE_STUDENT_EXTENSION),
COURSES_VIEW_COHORTS.identifier: has_cohort_view_access,
diff --git a/lms/djangoapps/instructor/authz/tests/test_legacy.py b/lms/djangoapps/instructor/authz/tests/test_legacy.py
index d796db35a8ee..28b17c5322ef 100644
--- a/lms/djangoapps/instructor/authz/tests/test_legacy.py
+++ b/lms/djangoapps/instructor/authz/tests/test_legacy.py
@@ -8,6 +8,7 @@
from opaque_keys.edx.locator import CourseLocator
from openedx_authz.authz import (
COURSES_ENROLL_LEARNERS,
+ COURSES_MANAGE_BULK_LEARNER_GRADING,
COURSES_MANAGE_COHORTS,
COURSES_MANAGE_COURSE_TEAM,
COURSES_MANAGE_LEARNER_EXTENSIONS,
@@ -358,6 +359,7 @@ class LegacyPermissionChecksTest(TestCase):
COURSES_VIEW_GRADING_SETTINGS.identifier: STUDIO_READ_ACCESS,
COURSES_MANAGE_LEARNER_GRADING.identifier: STAFF_ACCESS,
COURSES_VIEW_LEARNER_COURSE_STATUS.identifier: DASHBOARD_ACCESS,
+ COURSES_MANAGE_BULK_LEARNER_GRADING.identifier: INSTRUCTOR_ACCESS,
COURSES_VIEW_LEARNER_EXTENSIONS.identifier: DASHBOARD_ACCESS,
COURSES_MANAGE_LEARNER_EXTENSIONS.identifier: STAFF_ACCESS,
COURSES_VIEW_COHORTS.identifier: COHORT_VIEW_ACCESS,
diff --git a/lms/djangoapps/instructor/tests/test_api_v2_authz.py b/lms/djangoapps/instructor/tests/test_api_v2_authz.py
index fc2b8f09a28d..fcc03c312cf1 100644
--- a/lms/djangoapps/instructor/tests/test_api_v2_authz.py
+++ b/lms/djangoapps/instructor/tests/test_api_v2_authz.py
@@ -4,7 +4,10 @@
They cover the tabs, ``authz_enabled`` and the migrated endpoints.
"""
+import json
from datetime import datetime
+from itertools import product
+from textwrap import dedent
from unittest.mock import patch
import ddt
@@ -23,13 +26,16 @@
COURSE_LIMITED_STAFF,
COURSE_STAFF,
COURSES_ENROLL_LEARNERS,
+ COURSES_MANAGE_BULK_LEARNER_GRADING,
COURSES_MANAGE_COURSE_TEAM,
COURSES_MANAGE_LEARNER_EXTENSIONS,
+ COURSES_MANAGE_LEARNER_GRADING,
COURSES_UNENROLL_LEARNERS,
COURSES_VIEW_COURSE_INFO,
COURSES_VIEW_COURSE_TEAM,
COURSES_VIEW_ENROLLMENTS,
COURSES_VIEW_GRADEBOOK,
+ COURSES_VIEW_GRADING_SETTINGS,
COURSES_VIEW_LEARNER_COURSE_STATUS,
COURSES_VIEW_LEARNER_EXTENSIONS,
)
@@ -53,12 +59,15 @@
UserFactory,
)
from lms.djangoapps.ccx.tests.utils import CcxTestCase
+from lms.djangoapps.courseware.models import StudentModule
from lms.djangoapps.courseware.tabs import get_course_tab_list
from lms.djangoapps.instructor.access import update_forum_role
from lms.djangoapps.instructor.authz import constants
from lms.djangoapps.instructor.authz.api import user_has_instructor_permission
from lms.djangoapps.instructor.authz.compat import LEGACY_ROLE_COMPAT_PERMISSIONS
from lms.djangoapps.instructor.authz.tests.mixins import InstructorAuthzTestMixin
+from lms.djangoapps.instructor.permissions import OVERRIDE_GRADES
+from lms.djangoapps.instructor_task.models import InstructorTask
from openedx.core.djangoapps.course_date_signals.handlers import extract_dates
from openedx.core.djangoapps.django_comment_common.models import (
FORUM_ROLE_ADMINISTRATOR,
@@ -1599,3 +1608,269 @@ def test_legacy_does_not_query_authz(self):
assert response.status_code == status.HTTP_200_OK
is_user_allowed.assert_not_called()
+
+
+# Grading tab requests: the AuthZ permission each one requires and the status it returns on success.
+GRADING_ACTIONS = {
+ "view_learner": (COURSES_VIEW_LEARNER_COURSE_STATUS.identifier, status.HTTP_200_OK),
+ "view_problem": (COURSES_MANAGE_LEARNER_GRADING.identifier, status.HTTP_200_OK),
+ "view_grading_config": (COURSES_VIEW_GRADING_SETTINGS.identifier, status.HTTP_200_OK),
+ "reset_attempts": (COURSES_MANAGE_LEARNER_GRADING.identifier, status.HTTP_200_OK),
+ "delete_state": (COURSES_MANAGE_LEARNER_GRADING.identifier, status.HTTP_200_OK),
+ "rescore": (COURSES_MANAGE_LEARNER_GRADING.identifier, status.HTTP_202_ACCEPTED),
+ "override_score": (COURSES_MANAGE_LEARNER_GRADING.identifier, status.HTTP_202_ACCEPTED),
+ "reset_attempts_all_learners": (COURSES_MANAGE_BULK_LEARNER_GRADING.identifier, status.HTTP_202_ACCEPTED),
+ "rescore_all_learners": (COURSES_MANAGE_BULK_LEARNER_GRADING.identifier, status.HTTP_202_ACCEPTED),
+}
+
+# All Learners actions: Course Admin only with the flag on, instructor access with it off.
+ALL_LEARNERS_GRADING_ACTIONS = ("reset_attempts_all_learners", "rescore_all_learners")
+
+# The reads and the Single Learner actions: Course Staff can use them with the flag on, and legacy
+# course staff with it off.
+STAFF_GRADING_ACTIONS = tuple(
+ action for action in GRADING_ACTIONS if action not in ALL_LEARNERS_GRADING_ACTIONS
+)
+
+
+@ddt.ddt
+class GradingTabAuthzTest(InstructorAuthzTestMixin, SharedModuleStoreTestCase):
+ """
+ The Grading tab endpoints follow the AuthZ flag.
+
+ With the flag on, ``LearnerView`` requires ``courses.view_learner_course_status``,
+ ``GradingConfigView`` requires ``courses.view_grading_settings``,
+ ``ProblemView`` and the Single Learner actions require
+ ``courses.manage_learner_grading``, and the All Learners actions require
+ ``courses.manage_bulk_learner_grading``. With the flag off, each view uses
+ its legacy bridgekeeper permission, and the All Learners actions also
+ require instructor access.
+
+ The functional behavior of each endpoint, with a legacy instructor, is
+ covered in ``tests/views/test_api_v2.py``.
+ """
+
+ PROBLEM_XML = dedent("""\
+
`` block
without additional parsing.
"""
- permission_classes = (IsAuthenticated, permissions.InstructorPermission)
+ permission_classes = (IsAuthenticated, InstructorAuthzPermission)
+ authz_permissions = {
+ "GET": (COURSES_VIEW_GRADING_SETTINGS.identifier,),
+ }
permission_name = permissions.VIEW_DASHBOARD
@extend_schema(
@@ -3784,6 +3792,25 @@ def _build_async_response(instructor_task, course_id, problem_location, learner_
return Response(serializer.data, status=status.HTTP_202_ACCEPTED)
+def _require_grading_permission(user, course_key, all_learners, legacy_check=None):
+ """
+ Raise ``PermissionDenied`` unless the user can run a grading action on one learner or on all of them.
+
+ ``courses.manage_learner_grading`` covers a single learner and ``courses.manage_bulk_learner_grading``
+ covers all learners. With ``authz.enable_course_authoring`` disabled for the course, ``legacy_check``
+ decides if given; otherwise the default legacy check of the permission applies: ``OVERRIDE_GRADES``
+ for a single learner, and instructor access for all learners.
+ """
+ if all_learners:
+ permission = COURSES_MANAGE_BULK_LEARNER_GRADING.identifier
+ message = 'This user does not have permission to run grading operations on all learners.'
+ else:
+ permission = COURSES_MANAGE_LEARNER_GRADING.identifier
+ message = 'This user does not have permission to run grading operations on a learner.'
+ if not user_has_instructor_permission(user, permission, course_key, legacy_check):
+ raise PermissionDenied(message)
+
+
@method_decorator(transaction.non_atomic_requests, name='dispatch')
class ResetAttemptsView(DeveloperErrorViewMixin, APIView):
"""
@@ -3792,7 +3819,10 @@ class ResetAttemptsView(DeveloperErrorViewMixin, APIView):
**POST** with `learner` query param: resets a single learner's attempts (synchronous).
**POST** without `learner`: queues a background task to reset all learners (asynchronous).
"""
- permission_classes = (IsAuthenticated, permissions.InstructorPermission)
+ permission_classes = (IsAuthenticated, InstructorAuthzPermission)
+ authz_permissions = {
+ "POST": (COURSES_MANAGE_LEARNER_GRADING.identifier, COURSES_MANAGE_BULK_LEARNER_GRADING.identifier),
+ }
permission_name = permissions.GIVE_STUDENT_EXTENSION
@extend_schema(
@@ -3823,6 +3853,12 @@ def post(self, request, course_id, problem):
course_key, usage_key = parsed
learner_identifier = _get_learner_identifier(request)
+ _require_grading_permission(
+ request.user,
+ course_key,
+ all_learners=not learner_identifier,
+ legacy_check=authz_legacy.bridgekeeper_check(self.permission_name) if learner_identifier else None,
+ )
if learner_identifier:
student, error_response = _resolve_learner(learner_identifier)
@@ -3858,13 +3894,6 @@ def post(self, request, course_id, problem):
return Response(serializer.data, status=status.HTTP_200_OK)
else:
- course = get_course_with_access(request.user, 'staff', course_key, depth=None)
- if not has_access(request.user, 'instructor', course):
- return Response(
- {'error': 'Instructor access required for bulk operations'},
- status=status.HTTP_403_FORBIDDEN,
- )
-
try:
instructor_task = task_api.submit_reset_problem_attempts_for_all_students(
request, usage_key
@@ -3891,7 +3920,10 @@ class DeleteStateView(DeveloperErrorViewMixin, APIView):
The `learner` query parameter is required. This operation is destructive
and cannot be undone.
"""
- permission_classes = (IsAuthenticated, permissions.InstructorPermission)
+ permission_classes = (IsAuthenticated, InstructorAuthzPermission)
+ authz_permissions = {
+ "DELETE": (COURSES_MANAGE_LEARNER_GRADING.identifier,),
+ }
permission_name = permissions.GIVE_STUDENT_EXTENSION
@extend_schema(
@@ -3971,7 +4003,10 @@ class RescoreView(DeveloperErrorViewMixin, APIView):
Optionally accepts `only_if_higher=true` (query param or request body) to only
update if the new score is higher.
"""
- permission_classes = (IsAuthenticated, permissions.InstructorPermission)
+ permission_classes = (IsAuthenticated, InstructorAuthzPermission)
+ authz_permissions = {
+ "POST": (COURSES_MANAGE_LEARNER_GRADING.identifier, COURSES_MANAGE_BULK_LEARNER_GRADING.identifier),
+ }
permission_name = permissions.OVERRIDE_GRADES
@extend_schema(
@@ -4010,6 +4045,7 @@ def post(self, request, course_id, problem):
only_if_higher = _get_only_if_higher(request)
learner_identifier = _get_learner_identifier(request)
+ _require_grading_permission(request.user, course_key, all_learners=not learner_identifier)
if learner_identifier:
student, error_response = _resolve_learner(learner_identifier)
@@ -4041,13 +4077,6 @@ def post(self, request, course_id, problem):
)
else:
- course = get_course_with_access(request.user, 'staff', course_key)
- if not has_access(request.user, 'instructor', course):
- return Response(
- {'error': 'Instructor access required for bulk operations'},
- status=status.HTTP_403_FORBIDDEN,
- )
-
try:
instructor_task = task_api.submit_rescore_problem_for_all_students(
request, usage_key, only_if_higher,
@@ -4078,7 +4107,10 @@ class ScoreOverrideView(DeveloperErrorViewMixin, APIView):
The `learner` query parameter is required. Accepts a JSON body with `score` field.
"""
- permission_classes = (IsAuthenticated, permissions.InstructorPermission)
+ permission_classes = (IsAuthenticated, InstructorAuthzPermission)
+ authz_permissions = {
+ "PUT": (COURSES_MANAGE_LEARNER_GRADING.identifier,),
+ }
permission_name = permissions.OVERRIDE_GRADES
@extend_schema(
@@ -4127,19 +4159,13 @@ def put(self, request, course_id, problem):
score = body_serializer.validated_data['score']
try:
- block = modulestore().get_item(usage_key)
+ modulestore().get_item(usage_key)
except ItemNotFoundError:
return Response(
{'error': 'Problem not found'},
status=status.HTTP_404_NOT_FOUND,
)
- if not has_access(request.user, 'staff', block):
- return Response(
- {'error': 'You do not have permission to override scores for this problem'},
- status=status.HTTP_403_FORBIDDEN,
- )
-
try:
instructor_task = task_api.submit_override_score(
request, usage_key, student, score,