Repository navigation
refactor: simplify role visibility logic for non-admin users #7852
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
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 is the code review, highlighting potential issues and optimization suggestions:
1. Logic Inconsistency & Potential Bug
Issue: The logic for filtering "ADMIN" roles is inconsistent between
_get_user_rolesandupdate_user_role._get_user_roles: You previously filtered based onrole.internal == True AND role.type == ADMIN. Now you filter based only onrole.type == ADMIN. This means if there are non-internal roles with typeADMIN, they are now also hidden from non-admins. Ensure this change in behavior is intentional.update_user_role: The determination ofis_adminchecks if the user has a role that isADMINtype ANDinternal=True. However, when saving (deleting) mappings, it removes any role whererole__type != ADMIN.name.is_adminwill beFalse. The code will then delete mappings excludingrole__type=ADMIN. This effectively preserves the non-internal admin role assignments. This seems correct based on your comment, but it relies heavily on the distinction betweentypeandinternal. Ensure your data model consistently enforces that only system admins usetype=ADMINor explicitly handle the case where custom roles exist withtype=ADMIN.2. N+1 Query Problem in
_get_user_rolesIssue: The snippet shows accessing
relation.role.internalandrelation.role.type. Ifrelation.roleis accessed via a loop withoutselect_related, this triggers a database query for every relation.Suggestion: Ensure the queryset that generates
relationuses.select_related('role').3. Hardcoded User ID
Issue:
Using hardcoded UUIDs is bad practice. It makes the code brittle, difficult to maintain, and hard to understand why this specific user is treated differently.
Suggestion: Use a constant with a descriptive name (e.g.,
DEFAULT_SYSTEM_ADMIN_ID) or a configuration flag/setting. Ideally, determine superuser status viauser.is_superuseror a similar generic flag unless this specific ID serves a very unique migration legacy purpose.4. Ambiguous Naming / Magic Strings
Issue:
RoleConstants.ADMIN.namelikely evaluates to the string"ADMIN"._get_user_roles:relation.role.type == RoleConstants.ADMIN.nameupdate_user_role:role_id=RoleConstants.ADMIN.nameQuestion: Are you comparing
role.type(usually a string category like 'BUILTIN' vs 'CUSTOM'?) withrole.role_name?If
role_idactually maps to the Role Identifier UUID or Name, ensure you are querying the correct field. Comparingrole_idto a name string is suspicious unlessrole_idis genuinely stored as that string. Ifrole_idis a ForeignKey ID, this query will fail or cause a type mismatch error.role__typeis a safer check if you are looking for role category.5. Race Condition in
update_user_roleIssue:
Deleting all mappings for
user.idcan be dangerous if other concurrent processes are adding/updating roles. This is not atomic.Suggestion: Instead of "Delete All Then Add," consider using
bulk_createwithignore_conflictsor performing deletions based on precise IDs of the old mappings you are replacing. Alternatively, wrap the operation in atransaction.atomic()block with appropriate locking if concurrency is a concern.6. Optimization Suggestion
In
update_user_role, theworkspace_user_role_mapping_model.objects.filter(...).exists()call to determineis_adminmight be slightly inefficient if you need the full role object later. If you don't,.exists()is fine. However, ensure there are proper database indexes on(user_id, role_id)and(user_id, role__type)to speed up the filter/delete operations.Summary of Recommended Actions:
role_id=RoleConstants.ADMIN.nametargets the correct database column (ID vs Name)..select_related('role')to the queryset feeding into_get_user_roles.role.typefiltering: does "ADMIN" strictly mean "System Builtin Admin" in your schema?