Skip to content

feat: add search, sort and user counts to the role catalog - #623

Draft
jacobo-dominguez-wgu wants to merge 1 commit into
openedx:mainfrom
WGU-Open-edX:roles-catalog-controls
Draft

jacobo-dominguez-wgu wants to merge 1 commit into
openedx:mainfrom
WGU-Open-edX:roles-catalog-controls

Conversation

@jacobo-dominguez-wgu

Copy link
Copy Markdown
Contributor

Description

Add search, sort and user counts to the role catalog

Closes #569

Problem

The Admin Console's Roles and Permissions table needs search, sorting by name, resource type, members and last-updated date, and pagination with a total ("Showing N of M"). GET /api/authz/v1/roles/ (RoleListView, ADR 0028) returned the catalog only in role ID order, with no search, no sort and no member count.

The contract follows the draft Custom Roles ADR 0036 (#607), which is still under review, plus the details in this comment on #569.

Approach

New fields on each role

  • user_count: the number of distinct active users with the role, in scopes of the requested types that the caller can view. It uses the same visibility rules as the team members endpoints, with the same staff and superuser bypass.
  • updated_at: null for static roles. The real value will come with custom roles.

New query parameters (unknown values return 400; empty values fall back to the default)

  • search: case-insensitive substring match on the display name.
  • sort_by: display_name (the default), scope_type, user_count or updated_at.
  • order: asc (the default) or desc.
  • definition_kind: static or user_defined. It's in the ADR, but the frontend won't use it for now.

How it works

  • User counts: the new public api.get_visible_role_user_counts loads the assignment rows once, applies _filter_allowed_assignments, drops inactive users with one query, and counts usernames per role.
  • Filtering and sorting: the new RoleSearchFilter and RoleOrderingFilter backends follow the same pattern as the team members endpoints (TeamMemberSearchFilter, TeamMemberOrderingFilter). They call new filter_roles and sort_roles helpers in rest_api/utils.py.
    • Unlike the existing backends, they read ListRolesQuerySerializer's validated output (view.validated_query_params) instead of the raw query string. Values arrive stripped and with defaults filled in, so ?search=%20author%20 still matches.
    • Roles without updated_at sort last in both orders, and ties break by role ID, so pages are stable.
  • Order of steps: filtering and sorting run before pagination, so count, next and previous describe the filtered list. The categories and permissions catalogs stay complete.

Also included: a fix for an existing bug. ?sort_by= or ?order= with an empty value returned a 500 on GET /users/, GET /users/<username>/assignments/ and GET /assignments/. The three ordering backends now fall back to their defaults, and each endpoint has a regression test.

Reviewer notes

  • Default order changed. The catalog now lists roles by display name instead of by role ID. ADR 0028 still says "by identifier"; ADR 0036 changes it.
  • Performance. user_count loads every role assignment on each request: O(N) Python work, plus one User query with an IN list. The users endpoints already work this way (get_visible_role_assignments_for_user), but this endpoint used to make only a few schema queries. The draft ADR mentions "one grouped query". That isn't possible directly because assignment scopes are full keys or globs, and SQL can't apply the caller-visibility rules to them as stored. A lighter loader or a SQL version can follow once there are measurements.
  • Translation. Display names aren't translated at runtime yet, so search and sort use the stored English names. When translation lands, it has to run before the filter backends.
  • Size. About 930 lines across 14 files, roughly 290 of them source and the rest tests. That's above the ~400-line guideline. It could be split into "user count and validation" and "search and sort".

Testing

  • Unit and view tests cover every control: each sort field in both orders, None sorting last, tie-breaks, search with extra spaces, empty results, 400s on bad values, and count after filtering.
  • User count tests cover distinct users, inactive users, glob scopes, caller visibility, staff callers and callers with no roles.
  • The full suite passes on the latest main (1930 tests). Ruff and pylint are clean on the changed files.

Manual testing

  1. Mount this repo in tutor dev and restart: tutor dev restart lms cms.
  2. As a staff user, call GET /api/authz/v1/roles/?scope_types=course-v1,lib&sort_by=user_count&order=desc. Check that user_count and updated_at are on every role and that the order is right.
  3. Add search=admin and check that count drops while permissions stays the same.
  4. Repeat as a user with library_admin in a single library. user_count should only count users in that library.
  5. Try sort_by=bogus (expect 400) and sort_by= (expect the default order).

Merge checklist:
Check off if complete or not applicable:

  • Documentation updated (not only docstrings): CHANGELOG.rst
  • Fixup commits are squashed away
  • Unit tests added/updated
  • Manual testing instructions provided
  • Noted any: Concerns, dependencies, migration issues, deadlines, tickets

@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 12, 2026
@openedx-webhooks

Copy link
Copy Markdown

Thanks for the pull request, @jacobo-dominguez-wgu!

This repository is currently maintained by @openedx/committers-openedx-authz.

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.

🔘 Update the status of your PR

Your PR is currently marked as a draft. After completing the steps above, update its status by clicking "Ready for Review", or removing "WIP" from the title, as appropriate.


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.

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: Needs Triage

Development

Successfully merging this pull request may close these issues.

Backend: roles catalog search, filter, sort, pagination

2 participants