Skip to content

feat: enforce AuthZ permissions in the Cohorts tab endpoints - #39363

Open
BryanttV wants to merge 4 commits into
masterfrom
ks/issue-492-cohorts-authz
Open

BryanttV wants to merge 4 commits into
masterfrom
ks/issue-492-cohorts-authz

Conversation

@BryanttV

@BryanttV BryanttV commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Description

Migrates every endpoint behind the Cohorts tab to the shared Instructor Dashboard AuthZ infrastructure, following the migration guide in lms/djangoapps/instructor/authz/README.rst.

With authz.enable_course_authoring on, InstructorAuthzPermission is the only permission check, so a user whose AuthZ role has no legacy equivalent can use the whole tab. With the flag off, behavior is unchanged.

Affected roles: Course Admin, Course Staff, legacy Instructor, forum Admin/Moderator/Community TA, Django staff.

Endpoints (permissions are courses.view_cohorts / courses.manage_cohorts)

Endpoint Methods AuthZ permission Legacy fallback (flag off)
/api/cohorts/v1/settings/<course_id> GET / PUT view / manage has_cohort_view_access / has_cohort_manage_access
/api/cohorts/v1/courses/<course_id>/cohorts/[<cohort_id>] GET / POST / PATCH view / manage / manage same
/api/cohorts/v1/courses/<course_id>/cohorts/<cohort_id>/users/[<username>] GET / POST / DELETE view / manage / manage same
/courses/<course_id>/cohorts/settings (legacy view, used by Studio) GET / PATCH view / manage get_course_with_access(user, 'staff')
/api/cohorts/v1/courses/<course_id>/users/ (CSV upload) POST manage Django is_staff only
/api/cohorts/v2/courses/<course_id>/group_configurations[/<id>] GET view instructor.dashboard

Notes by endpoint group

  • DRF views (CohortSettings, CohortHandler, CohortUsers): no legacy_permission_check, on purpose. The per-method defaults in LEGACY_PERMISSION_CHECKS already reproduce IsStaffOrAdmin, so IsStaffOrAdmin and course_groups/permissions.py are deleted.
  • course_cohort_settings_handler: keeps its narrower legacy check (_legacy_cohort_staff_access), so it is not broadened to the forum roles.
  • CohortCSV: keeps the is_staff-only fallback it had with IsAdminUser. Whether it should be manage_cohorts long-term is open, see "Other Information".
  • Content groups v2: used only by the Instructor Dashboard MFE, which calls it when the tab loads. It used InstructorPermission, which only sees legacy roles, so it returned 403 for AuthZ-only users. It follows the tab's view_cohorts, not Studio's view_group_configurations: that avoids breaking custom roles without the Studio permission and does not widen access to Course Editor or Auditor.

Other changes

  • Course access inside the views: _get_course_with_access ran get_course_with_access(user, 'staff') after AuthZ had allowed the request, and it only recognizes legacy roles. With the flag on, a staff action now only loads the course. load (CohortHandler.get) keeps going through get_course_with_access, so visibility, dates and prerequisites checks stay. With the flag off the check is kept, because it is what stops forum roles from reaching the staff-only GETs.
  • Shared infra: InstructorAuthzPermission gets an optional course_id_url_kwarg (default "course_id"), because the cohorts API uses course_key_string. Documented in the README.

Supporting Information

Testing Instructions

  1. Enable authz.enable_course_authoring for a test course and create a cohort.

  2. Flag on. Call every endpoint in the table above as each of these users:

    • Course Admin and Course Staff: allowed, including the CSV upload.
    • A user whose AuthZ role has no legacy equivalent but grants the cohorts permissions: allowed, and the Cohorts tab loads without 403/404.
    • A user with no AuthZ role: 403.
    • A legacy instructor with the AuthZ permission explicitly denied: 403.
  3. Flag off. Behavior is unchanged:

    • Course staff/instructor: allowed on the DRF endpoints, the settings handler, and the content groups endpoints.
    • Forum Admin/Moderator/Community TA: allowed on the DRF GET methods only; writes return 403.
    • CSV upload: 403 for a course instructor, allowed for Django staff.
  4. Run the tests:

    pytest openedx/core/djangoapps/course_groups/
    pytest lms/djangoapps/instructor/authz/tests/test_permissions.py

Automated coverage added: CohortsAuthzTest (DRF views), CohortSettingsHandlerAuthzTest, CohortCSVAuthzTest, ContentGroupsAuthzTestCase (content groups v2) and test_custom_course_id_url_kwarg. They cover Course Admin, Course Staff, no permission, an AuthZ role without a legacy equivalent, AuthZ denying a legacy role, and the flag-off path.

Deadline

Willow

Other Information

  • CohortCSV intentionally keeps its is_staff-only fallback. It is worth a product/security conversation whether it should become manage_cohorts.
  • Not migrated here: cohort_handler and the function views users_in_cohort, add_users_to_cohort, remove_user_from_cohort, debug_cohort_mgmt. They do not go through InstructorAuthzPermission, so AuthZ-only users still cannot use them.
  • Check that nothing outside this repo imports IsStaffOrAdmin.

@openedx-webhooks openedx-webhooks added open-source-contribution PR author is not from Axim or 2U core contributor PR author is a Core Contributor (who may or may not have write access to this repo). labels Nov 10, 2026
@openedx-webhooks

openedx-webhooks commented Nov 10, 2026 •

Copy link
Copy Markdown

Thanks for the pull request, @BryanttV!

This repository is currently maintained by @openedx/wg-maintenance-openedx-platform-oncall.

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.

@github-project-automation github-project-automation Bot moved this to Needs Triage in Contributions Nov 10, 2026
@BryanttV BryanttV linked an issue Nov 10, 2026 that may be closed by this pull request
2 tasks
@BryanttV
BryanttV force-pushed the ks/issue-492-cohorts-authz branch from 84c9aab to 535f71c Compare November 10, 2026 17:04
@BryanttV
BryanttV marked this pull request as draft November 10, 2026 17:06
@BryanttV
BryanttV changed the base branch from bav/instructor-authz-shared-infra to master November 10, 2026 17:15
@BryanttV
BryanttV force-pushed the ks/issue-492-cohorts-authz branch 2 times, most recently from 291316e to 3900955 Compare November 10, 2026 17:52
@BryanttV
BryanttV marked this pull request as ready for review November 10, 2026 21:20
@BryanttV
BryanttV force-pushed the ks/issue-492-cohorts-authz branch from 2ebdaa5 to e06a0ce Compare November 11, 2026 18:44
@BryanttV
BryanttV force-pushed the ks/issue-492-cohorts-authz branch 2 times, most recently from f92f60f to 3c00424 Compare November 12, 2026 16:35
@BryanttV
BryanttV changed the base branch from master to bav/update-openedx-authz November 12, 2026 16:35
@BryanttV
BryanttV force-pushed the ks/issue-492-cohorts-authz branch from 3c00424 to c634992 Compare November 12, 2026 16:41
@BryanttV
BryanttV marked this pull request as draft November 12, 2026 17:05
Base automatically changed from bav/update-openedx-authz to master November 12, 2026 17:31
efortish and others added 4 commits November 12, 2026 12:37
Migrate the cohorts API views to InstructorAuthzPermission, following
openedx-authz#486's shared infrastructure:

- CohortSettings, CohortHandler and CohortUsers (openedx.core.djangoapps.
  course_groups.views) now require courses.view_cohorts for GET and
  courses.manage_cohorts for every write method, through a per-method
  authz_permissions mapping. No legacy_permission_check is set for these,
  so each method falls back to the matching default registered in
  LEGACY_PERMISSION_CHECKS (has_cohort_view_access/has_cohort_manage_access,
  already built in #486 to reproduce IsStaffOrAdmin), which is what varies
  the legacy fallback by method without any new code.

- CohortCSV (lms.djangoapps.instructor.views.api) now requires
  courses.manage_cohorts, with a custom legacy_permission_check that
  keeps its pre-existing is_staff-only fallback: unlike the rest of the
  cohorts endpoints, this one was never gated on course staff access.

- course_cohort_settings_handler, the legacy function-based view still
  used by Studio, gets the same permission check via
  user_has_instructor_permission directly, with an explicit legacy check
  reproducing its existing get_course_with_access(user, 'staff', ...)
  call so GET and PATCH keep requiring the same access level they always
  did (the DRF views' IsStaffOrAdmin-derived checks allow broader GET
  access, which this endpoint never had).

Along the way:

- InstructorAuthzPermission assumed the URL kwarg was always named
  course_id. The cohorts API uses course_key_string, so it gained an
  optional course_id_url_kwarg attribute (defaults to "course_id"),
  documented in the README for the other per-tab subtasks too.

- Deleted course_groups.permissions.IsStaffOrAdmin: it was this
  migration's only remaining caller, and legacy.has_cohort_view_access/
  has_cohort_manage_access already reproduce its exact logic.

Add CohortsAuthzTest, CohortSettingsHandlerAuthzTest and CohortCSVAuthzTest
covering Course Admin, Course Staff, a user with no permission, AuthZ
denying a legacy instructor, and the legacy path with the flag off
(including CohortCSV's narrower is_staff-only fallback).

Rebased onto the latest instructor-authz-shared-infra (authz_permission
renamed to authz_permissions, always a collection) and updated the
cohorts views and tests to match.

Closes openedx-authz#492.
Replace the inline legacy_permission_check of CohortCSV with
legacy.is_django_staff, a generic equivalent of DRF's IsAdminUser
(user.is_staff). It is not registered in LEGACY_PERMISSION_CHECKS, so
views opt in explicitly and other endpoints migrated from IsAdminUser
can reuse it.

Add unit tests for is_django_staff.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
_get_course_with_access ran get_course_with_access(user, 'staff', ...) after
InstructorAuthzPermission had already allowed the request. That check only
recognizes legacy roles (or AuthZ roles with a legacy equivalent), so a user
holding courses.view_cohorts/manage_cohorts through an AuthZ role without a
legacy equivalent got a 404.

With authz.enable_course_authoring enabled, a 'staff' action now only loads
the course, leaving the permission to InstructorAuthzPermission. The 'load'
action keeps going through get_course_with_access so the course visibility,
start/end dates and prerequisites checks still apply.

With the flag disabled the legacy behavior is unchanged: the check is not
redundant there, since the legacy view access also admits the forum
Administrator, Moderator and Community TA roles and the 'staff' check is what
keeps them out of the endpoints that require course staff.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Migrate GroupConfigurationsListView and GroupConfigurationDetailView, used by
the Cohorts tab of the Instructor Dashboard, to InstructorAuthzPermission.
They require courses.view_cohorts, and keep instructor.dashboard as the
legacy fallback used when authz.enable_course_authoring is disabled.

They previously used InstructorPermission, which only sees legacy roles, so
a user with an AuthZ role without a legacy equivalent got a 403 when the tab
loaded the content groups.

Update the existing tests to patch InstructorAuthzPermission and add
ContentGroupsAuthzTestCase covering Course Staff, an AuthZ role without a
legacy equivalent, a user with no permission, and the legacy path with the
flag off.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@BryanttV
BryanttV force-pushed the ks/issue-492-cohorts-authz branch from c634992 to f9c8340 Compare November 12, 2026 17:40
@BryanttV
BryanttV marked this pull request as ready for review November 12, 2026 18:50
@mphilbrick211 mphilbrick211 moved this from Needs Triage to Ready for Review in Contributions Nov 16, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core contributor PR author is a Core Contributor (who may or may not have write access to this repo). open-source-contribution PR author is not from Axim or 2U

Projects

Status: Ready for Review

Development

Successfully merging this pull request may close these issues.

Enforcement: Cohorts tab (course_groups API)

3 participants