Skip to content

feat: implement grading permissions and authorization hooks#2988

Open
bra-i-am wants to merge 6 commits intoopenedx:masterfrom
eduNEXT:bc/implement-grading-permissions
Open

feat: implement grading permissions and authorization hooks#2988
bra-i-am wants to merge 6 commits intoopenedx:masterfrom
eduNEXT:bc/implement-grading-permissions

Conversation

@bra-i-am
Copy link
Copy Markdown
Contributor

@bra-i-am bra-i-am commented Apr 8, 2026

Description

This PR implements AuthZ permission checks for the Settings → Grading page in Studio, enabling role-based UI control based on the courses.view_grading_settings and courses.edit_grading_settings permissions from openedx-authz.

What changed

New shared authz utilities

  • src/authz/hooks.ts — New useUserPermissionsWithAuthzCourse(courseId, permissions) hook that abstracts the common pattern of checking the enableAuthzCourseAuthoring waffle flag, fetching permissions when enabled, and defaulting all permissions to true when disabled.
  • src/authz/permissionHelpers.ts — New getGradingPermissions(courseId) and getFilesPermissions(courseId) helpers that return structured permission query objects.
  • src/authz/constants.ts — Added VIEW_GRADING_SETTINGS and EDIT_GRADING_SETTINGS to COURSE_PERMISSIONS.

Grading Settings page (GradingSettings.jsx):

  • If the user lacks courses.view_grading_settings → renders <PermissionDeniedAlert /> (full page block).
  • If the user lacks courses.edit_grading_settings → all edit controls are disabled (read-only view).
  • isEditable prop propagated to all child components: GradingScale, GradingScaleSegment, GradingScaleHandle, AssignmentSection, AssignmentItem, AssignmentTypeName, DeadlineSection, CreditSection.

Supporting information

  • Backend authz permission definitions: openedx_authz.constants.permissions.COURSES_VIEW_GRADING_SETTINGS / COURSES_EDIT_GRADING_SETTINGS
  • Waffle flag: authz.enable_course_authoring (CourseWaffleFlag, default false)

Testing instructions

Prerequisites:

  1. Enable the waffle flag: Django Admin → Waffle → Flags → create authz.enable_course_authoring set to Everyone.
  2. Create a test course.

Test view permission denied:

  1. Create a user with no role assigned to the course.
  2. Navigate to Settings → Grading for that course.
  3. Expected: Permission denied alert is shown.

Test view-only (read-only UI):

  1. Assign course_auditor role to a user for the course via PUT /api/authz/v1/roles/users/ with scope=course-v1:Org%2BCourse%2BRun.
  2. Log in as that user and navigate to Settings → Grading.
  3. Expected: Page loads, but all inputs are disabled and the Save button is disabled.

Test full edit access:

  1. Assign course_editor role to a user.
  2. Navigate to Settings → Grading.
  3. Expected: Full edit access — all inputs enabled, Save button works.

Other information

Best Practices Checklist

We're trying to move away from some deprecated patterns in this codebase. Please
check if your PR meets these recommendations before asking for a review:

  • Any new files are using TypeScript (.ts, .tsx).
  • Avoid propTypes and defaultProps in any new or modified code — existing JSX child components were modified and retain their existing patterns. Full migration to TypeScript is out of scope for this PR.
  • Tests should use the helpers in src/testUtils.tsx (specifically initializeMocks) — to be added in a follow-up.
  • Do not add new fields to the Redux state/store. Use React Context to share state among multiple components.
  • Use React Query to load data from REST APIs. See any apiHooks.ts in this repo for examples.
  • All new i18n messages in messages.ts files have a description for translators to use.
  • Avoid using ../ in import paths. To import from parent folders, use @src.

@bra-i-am bra-i-am marked this pull request as draft April 8, 2026 21:55
@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Apr 8, 2026
@openedx-webhooks
Copy link
Copy Markdown

openedx-webhooks commented Apr 8, 2026

Thanks for the pull request, @bra-i-am!

This repository is currently maintained by @bradenmacdonald.

Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review.

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

Details
Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 8, 2026

Codecov Report

❌ Patch coverage is 97.36842% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 95.46%. Comparing base (b2a4b9c) to head (e330471).

Files with missing lines Patch % Lines
...s/grading-scale/components/GradingScaleSegment.tsx 66.66% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##           master    #2988    +/-   ##
========================================
  Coverage   95.46%   95.46%            
========================================
  Files        1378     1380     +2     
  Lines       32596    32632    +36     
  Branches     7479     7277   -202     
========================================
+ Hits        31117    31152    +35     
- Misses       1410     1424    +14     
+ Partials       69       56    -13     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bra-i-am bra-i-am force-pushed the bc/implement-grading-permissions branch from f36766a to 72cb564 Compare April 9, 2026 16:56
@mphilbrick211 mphilbrick211 moved this from Needs Triage to Waiting on Author in Contributions Apr 9, 2026
@bra-i-am bra-i-am force-pushed the bc/implement-grading-permissions branch from cbce6d4 to ad49a12 Compare April 9, 2026 20:48
@bra-i-am bra-i-am marked this pull request as ready for review April 10, 2026 20:34
@bradenmacdonald
Copy link
Copy Markdown
Contributor

Cool! I'll try to review soon. I'm a bit behind in my reviews at the moment though, sorry. If @dcoa can confirm everything is tested and working, then I can do a faster review just by reading the code.

@dcoa
Copy link
Copy Markdown
Contributor

dcoa commented Apr 15, 2026

Thank you @bra-i-am, I will review it today.

Copy link
Copy Markdown
Contributor

@dcoa dcoa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works!!

I leave a couple of suggestions (no blockers) for enhance the hook’s readability.

Also the no having permission over the grading is displaying an alert what is good, but we can consider removing the option for the dropdown menu in that specific case. @bradenmacdonald, what do you think?

Comment thread src/authz/hooks.ts Outdated
* const { canViewGradingSettings, canEditGradingSettings } = permissions;
* ```
*/
export const useUserPermissionsWithAuthzCourse = (
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the hook name is quite verbose maybe useValidateCourseAccess or useCourseUserPermissions.

For the description maybe we can change it a little bit to be more clear:

/**
 * Custom hook to retrieve and evaluate user permissions for the current course using the openedx-authz service.
 *
 * The hook:
 * 1. Validate if authz is enabled via waffle flag
 * 2. Fetch user permissions when authz is enabled
 * 3. Fallback all permissions to 'true' when authz is disabled
 * 4. Provide fallback values for undefined permissions
*/

Maybe to avoid the else if we can do

  const resolvePermission = (key: string): boolean => {
    if (!isAuthzEnabled) {
      return true;
    }
    return userPermissions?.[key] ?? false;
  };

  const permissionResults: PermissionValidationAnswer = isLoadingUserPermissions
    ? {}
    : Object.keys(permissions).reduce<PermissionValidationAnswer>((acc, key) => {
      acc[key] = resolvePermission(key);
      return acc;
    }, {});

@bradenmacdonald
Copy link
Copy Markdown
Contributor

Also the no having permission over the grading is displaying an alert what is good, but we can consider removing the option for the dropdown menu in that specific case. @bradenmacdonald, what do you think?

Yeah, I think it's better to remove it than to show it but display an error when the user clicks on it.

Copy link
Copy Markdown
Contributor

@bradenmacdonald bradenmacdonald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, just a couple suggestions:

Comment thread src/authz/hooks.ts Outdated
export const useUserPermissionsWithAuthzCourse = (
courseId: string,
permissions: PermissionValidationQuery,
): UseUserPermissionsWithAuthzCourseReturn => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now this just returns a basic UseUserPermissionsWithAuthzCourseReturn type, which doesn't specify what permissions it contains. Please fix the types so that it returns the actual requested permissions:

--- a/src/authz/hooks.ts
+++ b/src/authz/hooks.ts
@@ -1,10 +1,10 @@
 import { useWaffleFlags } from '@src/data/apiHooks';
 import { useUserPermissions } from '@src/authz/data/apiHooks';
-import { PermissionValidationQuery, PermissionValidationAnswer } from '@src/authz/types';
+import { PermissionValidationQuery } from '@src/authz/types';
 
-interface UseUserPermissionsWithAuthzCourseReturn {
+interface UseUserPermissionsWithAuthzCourseReturn<Query extends PermissionValidationQuery> {
   isLoading: boolean;
-  permissions: PermissionValidationAnswer;
+  permissions: { [K in keyof Query]: boolean };
   isAuthzEnabled: boolean;
 }
 
@@ -40,10 +40,10 @@ interface UseUserPermissionsWithAuthzCourseReturn {
  * const { canViewGradingSettings, canEditGradingSettings } = permissions;
  * ```
  */
-export const useUserPermissionsWithAuthzCourse = (
+export const useUserPermissionsWithAuthzCourse = <Query extends PermissionValidationQuery>(
   courseId: string,
-  permissions: PermissionValidationQuery,
-): UseUserPermissionsWithAuthzCourseReturn => {
+  permissions: Query,
+): UseUserPermissionsWithAuthzCourseReturn<Query> => {
   const waffleFlags = useWaffleFlags(courseId);
   const isAuthzEnabled: boolean = waffleFlags?.enableAuthzCourseAuthoring ?? false;
 
@@ -52,7 +52,7 @@ export const useUserPermissionsWithAuthzCourse = (
     data: userPermissions,
   } = useUserPermissions(permissions, isAuthzEnabled);
 
-  const permissionResults: PermissionValidationAnswer = {};
+  const permissionResults: Record<string, boolean> = {};
 
   if (isAuthzEnabled && !isLoadingUserPermissions) {
     Object.keys(permissions).forEach((permissionKey: string) => {
@@ -66,7 +66,7 @@ export const useUserPermissionsWithAuthzCourse = (
 
   return {
     isLoading: isAuthzEnabled ? isLoadingUserPermissions : false,
-    permissions: permissionResults,
+    permissions: permissionResults as { [K in keyof Query]: boolean },
     isAuthzEnabled,
   };
 };

Comment thread src/authz/hooks.ts Outdated
* }
* );
*
* const { canViewGradingSettings, canEditGradingSettings } = permissions;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit/optional:

It would be more useful it this returned the permissions right away:

Instead of:

const { isLoading, permissions, isAuthzEnabled } = useUserPermissionsWithAuthzCourse(...);
const { canViewGradingSettings, canEditGradingSettings } = permissions;

I would rather write:

const { isLoading, canViewGradingSettings, canEditGradingSettings, isAuthzEnabled } = useUserPermissionsWithAuthzCourse(...);

Especially in the future when we don't have isAuthzEnabled any more, this way will be more convenient.

@bra-i-am bra-i-am force-pushed the bc/implement-grading-permissions branch 3 times, most recently from c859e5d to 3327288 Compare April 16, 2026 15:43
@bra-i-am bra-i-am force-pushed the bc/implement-grading-permissions branch from 3327288 to e330471 Compare April 16, 2026 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

open-source-contribution PR author is not from Axim or 2U

Projects

Status: Waiting on Author

Development

Successfully merging this pull request may close these issues.

Task - RBAC Authz - Implement frontend check for Settings -> Grading permissions

5 participants