Repository navigation
feat: add search, sort and user counts to the role catalog - #623
jacobo-dominguez-wgu wants to merge 1 commit into
Conversation
|
Thanks for the pull request, @jacobo-dominguez-wgu! 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. 🔘 Update the status of your PRYour 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:
💡 As a result it may take up to several weeks or months to complete a review and merge your PR. |
3fac58f to
3b010f9
Compare
3b010f9 to
68cac6d
Compare
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:nullfor 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_countorupdated_at.order:asc(the default) ordesc.definition_kind:staticoruser_defined. It's in the ADR, but the frontend won't use it for now.How it works
api.get_visible_role_user_countsloads the assignment rows once, applies_filter_allowed_assignments, drops inactive users with one query, and counts usernames per role.RoleSearchFilterandRoleOrderingFilterbackends follow the same pattern as the team members endpoints (TeamMemberSearchFilter,TeamMemberOrderingFilter). They call newfilter_rolesandsort_roleshelpers inrest_api/utils.py.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%20still matches.updated_atsort last in both orders, and ties break by role ID, so pages are stable.count,nextandpreviousdescribe the filtered list. Thecategoriesandpermissionscatalogs stay complete.Also included: a fix for an existing bug.
?sort_by=or?order=with an empty value returned a 500 onGET /users/,GET /users/<username>/assignments/andGET /assignments/. The three ordering backends now fall back to their defaults, and each endpoint has a regression test.Reviewer notes
user_countloads every role assignment on each request: O(N) Python work, plus oneUserquery with anINlist. 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.Testing
Nonesorting last, tie-breaks, search with extra spaces, empty results, 400s on bad values, andcountafter filtering.main(1930 tests). Ruff and pylint are clean on the changed files.Manual testing
tutor dev restart lms cms.GET /api/authz/v1/roles/?scope_types=course-v1,lib&sort_by=user_count&order=desc. Check thatuser_countandupdated_atare on every role and that the order is right.search=adminand check thatcountdrops whilepermissionsstays the same.library_adminin a single library.user_countshould only count users in that library.sort_by=bogus(expect 400) andsort_by=(expect the default order).Merge checklist:
Check off if complete or not applicable:
CHANGELOG.rst