Repository navigation
refactor: enhance user role management with admin role checks and batch processing improvements #7845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
refactor: enhance user role management with admin role checks and batch processing improvements #7845
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here are my observations and suggestions:
1. Correctness / Logic Issues
a) Inconsistent
is_adminsemantics between the two functions_get_user_roles,is_adminis a parameter that means "the caller is an admin" (controls filtering).is_adminis reassigned to mean "this particular user is a builtin admin" — then passed into_get_user_rolesas the first function'sis_adminparam.This works only if the two meanings happen to align (i.e., you're processing a list of users and use one user's admin status to decide visibility for all of them). But since
user_idshere is a list of many users, checkingis_adminfor a singleuser_idand using it to control visibility for the whole batch is a latent bug:_get_user_roleswill suppress builtin-admin roles for all users in the batch if the singleuser_idisn't a builtin admin — meaning actual admins in the list will show no admin role._get_user_roles, or compute a set of admin user IDs and pass that in, e.g.:if not is_admin and str(relation.user_id) not in admin_user_ids and is_builtin_admin_role: continue.b)
role.internalvsrole__internalinternalexists on theRolemodel. Verify the attribute name matches your schema (internal,is_internal, etc.). If it'sis_internal, both places need updating.c)
relation.role.internalmay cause N+1 queriesrelation.role.internalandrelation.role.typeeach trigger lazy loads unless you had.select_related("role"). Confirm_get_user_rolesusesselect_related("role"); otherwise adding anotherrelation.role.Xaccess increases query count. Preferprefetch_related/select_related.2. Variable Shadowing (fixed, but note context)
The rename
user_id→record_user_idin the final block is good cleanup — but double-check there wasn't previously intended reuse. Note the outeruser_idnow retains the admin-check value. Consider renaming the outer one too (e.g.,current_user_id/request_user_id) to avoid confusion with the loop variableuser["id"].3. Style / Minor
not is_admin AND is_builtin_admin_role. That's "non-admin callers don't see builtin-admin roles." Consistent. ✅is_builtin_admin_rolelogic into a helper/mixin (role.is_builtin_admin()) since it's repeated conceptually in both places (DB filter + object check). This avoids drift ifRoleConstantschanges.4. Optimization
.exists()query with the batchedvalues_list(...)query above — fewer queries and fixes the batching bug simultaneously.role__typeandrole__internalfilters have DB indexes (or a composite index), especially ifuser_role_relation_modelis large.Summary of key fix
The main irregularity is using a single user's
is_adminto gate role visibility for a batch of users. Make admin determination per-user (pass a set of admin IDs), and verifyrole.internalis the correct field name and thatroleisselect_relatedto avoid N+1.