feat: include authz course + org role grants in Meilisearch access filter - #39073
feat: include authz course + org role grants in Meilisearch access filter#39073wgu-taylor-payne wants to merge 2 commits into
Conversation
|
Thanks for the pull request, @wgu-taylor-payne! This repository is currently maintained by 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 approvalIf you haven't already, check this list to see if your contribution needs to go through the product review process.
🔘 Provide contextTo 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:
🔘 Get a green buildIf one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green. DetailsWhere 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:
💡 As a result it may take up to several weeks or months to complete a review and merge your PR. |
5d245c8 to
a65995b
Compare
a65995b to
c898c71
Compare
There was a problem hiding this comment.
🔵 Needs a closer look
It changes authorization-sensitive filtering logic (who can search what) and should receive final human review despite strong test coverage.
Pull request overview
This PR updates Studio’s Meilisearch tenant-token access filter generation to include authorization grants coming from openedx-authz (including authz-only course roles like course_editor/course_auditor) so that users granted access via authz can see the appropriate course/org content in Studio search when AUTHZ_COURSE_AUTHORING_FLAG is enabled.
Changes:
- Expand per-course
access_idfiltering to union in authzCourseOverviewDatascope assignments (flag-gated per course, fail-open onDatabaseError). - Expand org
org IN [...]filtering to union in authz org-glob (OrgCourseOverviewGlobData) assignments resolved to org short_names (flag-gated per org via org override/global switch). - Add/extend CMS test coverage for both per-course authz access and org-glob authz access paths.
File summaries
| File | Description |
|---|---|
| openedx/core/djangoapps/content/search/models.py | Adds authz assignment caching and helpers to include authz-derived course/org access in search access computation. |
| openedx/core/djangoapps/content/search/api.py | Unions authz org-glob grants into _get_user_orgs to populate the Meilisearch org clause. |
| openedx/core/djangoapps/content/search/tests/test_models.py | Adds tests for authz-only per-course and org-glob access behavior, including flag gating and fail-open error handling. |
| openedx/core/djangoapps/content/search/tests/test_views.py | Adds a Studio token endpoint test to verify authz org-glob access lands in the org filter clause. |
Review details
- Files reviewed: 4/4 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
CourseWaffleFlag could only resolve a flag for a course key (is_enabled), which walks course override -> org override -> global switch. There was no public way to ask whether a flag is enabled for an entire org, independent of any single course. Add a public is_enabled_for_org(org) that resolves the flag at the org tier only: an org override (force-on / force-off) takes precedence, otherwise the global switch. Per-course overrides are not consulted -- a setting made for one course cannot answer whether the flag is on for the whole org. Extract the org-override read that was inline in _get_course_override_value into a shared _get_org_override_value(org) helper so both entry points resolve org overrides identically and share one cached_flags() entry per org.
…lter get_access_ids_for_request built the Meilisearch tenant-token filter from legacy CourseStaffRole/CourseInstructorRole only, so users holding an authz-only course role (course_editor / course_auditor with no legacy twin) were excluded from the search access filter. Their courses returned zero results in the global Studio search modal and the Library Updates "Review Content Updates" tab. This is openedx-authz#417. Add _get_authz_course_keys to union the user's per-course authz role assignments (CourseOverviewData scope) into the access_id filter clause, gated per-course on AUTHZ_COURSE_AUTHORING_FLAG so global-off deployments with per-course/org overrides still resolve correctly. Also handle org-wide (glob) authz grants, e.g. course-v1:Org+*, which surface as an OrgCourseOverviewGlobData scope rather than a per-course scope and so were missed by both filter clauses -- the same openedx#417 gap for the org case. Add get_authz_org_keys to resolve org-glob grants to org short_names and union them into _get_user_orgs, landing them in the org IN [...] clause (one entry per org, mirroring legacy org staff roles and avoiding per-course access_id fan-out against the JWT size cap). Each org is gated via CourseWaffleFlag.is_enabled_for_org, since a per-course override cannot gate an org-wide grant. A single per-request-cached fetch (_get_cached_authz_assignments) backs both helpers so the enforcer is queried once per request rather than twice. The authz lookups fail open on a DatabaseError (logged, empty set) so search degrades to legacy access rather than 500-ing; any other error propagates. Scope parsing is narrowed to InvalidKeyError so a non-course authz scope (e.g. a library) is skipped rather than mishandled. Closes: openedx/openedx-authz#417
c898c71 to
aef74ef
Compare
Description
Studio search is powered by Meilisearch. On each request to the Studio search token endpoint (
GET /api/content_search/v2/studio/),get_access_ids_for_request/_get_meili_access_filterbuild the per-user tenant-token filter that Meilisearch enforces server-side. The filter has two clauses:org IN [<org short_names>] OR access_id IN [<SearchAccess ids>]. Until now both clauses were built from legacy roles only —CourseStaffRole/CourseInstructorRolefor theaccess_id(per-course) clause, andOrgStaffRole/OrgInstructorRolefor theorgclause.Users who hold an authz-only course role —
course_editororcourse_auditorgranted through openedx-authz, with no corresponding legacyCourseAccessRolerow — were therefore invisible to the filter, and their courses returned zero results in the global Studio search modal (search icon / Cmd+K) and the "Review Content Updates" tab on the Library Updates page (/course/{id}/libraries), which uses Meilisearch to look up component details.This change fixes both clauses:
Per-course grants (
access_idclause): a new_get_authz_course_keyshelper unions the user's per-course authz role assignments (CourseOverviewDatascope) into theaccess_idfilter whenAUTHZ_COURSE_AUTHORING_FLAGis enabled. The flag is aCourseWaffleFlag, so the check is applied per course — a deployment running the flag globally-off with per-course or per-org overrides still resolves correctly.Org-wide (glob) grants (
orgclause): an authz course role can also be granted at an org-wide scope, e.g.course-v1:Org+*, which surfaces as anOrgCourseOverviewGlobDatascope rather than a per-courseCourseOverviewDatascope. That grant means "all courses in this org", so it was missed by both clauses — the per-course helper only looks atCourseOverviewData, and the legacy org clause only knows legacy org roles. A newget_authz_org_keyshelper resolves these glob grants to orgshort_names and unions them into_get_user_orgs, so they land in theorg IN [...]clause — one entry per org, mirroring how legacy org staff roles are handled and avoiding a per-courseaccess_idfan-out that would burn slots against the JWT size cap (MAX_ACCESS_IDS_IN_FILTER). Because an org-wide grant is not tied to a single course, its flag gate is resolved at the org tier via a new publicCourseWaffleFlag.is_enabled_for_org(org)method (added in this PR): an org override (force-on/force-off) takes precedence, otherwise the global switch — a per-course override cannot gate an org-wide grant.Note that the authz→legacy compat layer already bridges
course_staff→staffandcourse_admin→instructor(roles with a legacy twin), so those grants were already reaching the filter; the load-bearing cases here arecourse_editor/course_auditor, which have no legacy twin and are dropped by compat. Both helpers query purely by scope type and are role-agnostic — inclusion is pure widening into anORfilter (you can only gain search on a course/org you already hold a role on), the same safety posture as the fail-open behavior below.Both lookups fail open: a
DatabaseErrorfrom the authz/Casbin backend is logged and degrades to legacy-role access rather than returning a 500, while any unexpected exception propagates so real bugs surface. A non-course scope key (e.g. a library scope) appearing in the per-course query is skipped viaInvalidKeyError.Both helpers read a single per-request-cached fetch of the user's assignment set (
_get_cached_authz_assignments,@request_cached), so the enforcer is queried once per request rather than once per helper. Authz course keys are normalized to strings so a course held via both a legacy and an authz role de-duplicates into one entry in the unioned set.Commit structure. This PR is two commits: (1)
feat: add org-tier resolution to CourseWaffleFlagadds the reusable publicis_enabled_for_orgmethod towaffle_utils(extracting the shared org-override resolutionis_enabledalready used internally); (2)feat: include authz course + org role grants in Meilisearch access filteris the search-filter change that consumes it. The first is a small, self-contained platform addition reviewable on its own.Impacted user roles: Course Author (editors/auditors who rely on authz roles, at course or org scope), Operator (behavior is gated behind
AUTHZ_COURSE_AUTHORING_FLAG).Supporting information
view_library_updatesenforcement work in Addcourses.view_library_updatesenforcement in openedx-platform openedx-authz#398 and PR feat: split read vs write authz checks for library updates #39009.Testing instructions
AUTHZ_COURSE_AUTHORING_FLAG(globally, or as a per-course / per-org override for the course/org under test).course_editor(orcourse_auditor) role on a course, and no legacyCourseStaffRole/CourseInstructorRoleon it.course_editor/course_auditorat an org-glob scope (e.g.course-v1:Org+*), enable the flag for that org, and confirm search returns that org's content via theorg IN [...]clause (previously: zero results).pytest openedx/core/djangoapps/waffle_utils/tests/test_init.py openedx/core/djangoapps/content/search/tests/test_models.py openedx/core/djangoapps/content/search/tests/test_views.py—test_init.pycoversis_enabled_for_org(org override on/off/unset, and global-switch fallback with no override);StudioSearchAuthzAccessTestcovers per-course (authz-only user sees courses with flag on,omit_orgsapplies, flag-off excludes,DatabaseErrorswallowed, unexpected error propagates, unparseable scope skipped, legacy+authz de-duplicate);StudioSearchAuthzOrgAccessTestcovers org-glob (org grant lands in theorgclause, org-override forces on when the flag is globally off).Deadline
None.
Other information
AUTHZ_COURSE_AUTHORING_FLAGresolves enabled for a given course or org; with the flag off everywhere and no overrides, the filter is unchanged from today.get_user_role_assignments_per_scope_typeeagerly expands each role's implicit permissions, but these helpers only readscope.external_key/scope.organd discard the permissions — wasted work for users with many assignments. The computation runs once per token fetch (not per keystroke — the frontend reuses the minted tenant token for subsequent queries). A scopes-only authz API, and/or caching the computed access set with an explicit invalidation contract on role/flag change, would be the clean optimization; both are intentionally left out of this correctness fix.CourseWaffleFlag.is_enabledonly accepts a course key and has no public org-only entry point, so this PR addsCourseWaffleFlag.is_enabled_for_org(org)(org override → global switch) andget_authz_org_keyscalls it. The org-override read that was inline inis_enabled's course resolution is extracted into a shared helper so both entry points resolve identically and share one cache entry per org.openedx/core/djangoapps/waffle_utils/for the newis_enabled_for_orgmethod and its tests (see Commit structure above).