Reduce SQL queries and restrict to claimed groups#377
Open
Reduce SQL queries and restrict to claimed groups#377
Conversation
58ac671 to
43da8fd
Compare
Claimed groups can contain groups they are not relevant for our app so the first quick check is to ensure the list are different then to use a SQL query to fetch the groups of the user. Only set the groups if they are different, it avoids a SELECT of the Django ORM (called by the set()).
| ) | ||
| # Set user's groups to all claimed groups (both existing and | ||
| # newly created) and remove any that are not in the claim. | ||
| user.groups.set(new_claimed_groups) |
Member
There was a problem hiding this comment.
From my understanding, the comment here matches the old code. The new code will set the user's groups to only the groups in the claim that didn't already have a Group instance for them.
Comment on lines
+385
to
+387
| existing_claimed_group_names = ( | ||
| group.name for group in existing_claimed_groups | ||
| ) |
Member
There was a problem hiding this comment.
There's a redefinition of existing_claimed_group_names on line 394 that can be removed because of this.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Check the new groups are different before update
Claimed groups can contain groups that are not relevant for our app, so the first quick check is to ensure the lists are different before using a SQL query to fetch the user's groups. Only setting the groups if they are different avoids an unnecessary SELECT from the Django ORM.
Mirror groups should remove any that are not in the claim
This ensures that when a user is removed from a group in the identity provider, that removal is correctly mirrored in our application.
Formatted with ruff.