Skip to content
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

Prevent duplicate rows in /users when filters applied #2400

Merged
merged 1 commit into from
Oct 4, 2023

Conversation

chrislo
Copy link
Contributor

@chrislo chrislo commented Oct 3, 2023

Trello: https://trello.com/c/i27MQaus

This fixes an issue with duplication of a user in the table on /users (and the corresponding CSV export) when multiple permissions for the same application were selected in the filter.

For example if a user had the "GDS editor" and "signon" permission for the "Transition" app and we selected both of those permissions in the filter drop down, this user would appear twice in table.

The solution is to call distinct on the User#with_permission scope, which is in turn called by UsersFilter#users. I've decided to add a test case in both places to avoid a regression in UsersFilter in case it changes in the future.

Note we have to add the unscoped method to SupportedPermission in the User#with_permission scope since SupportedPermission has an order(:name) default_scope and calling distinct on an ordered query is not supported by MySQL:

Mysql2::Error: Expression 1 of ORDER BY clause is not in SELECT list, references column 'signon_test.supported_permissions.name' which is not in SELECT list; this is incompatible with DISTINCT

@chrislo chrislo force-pushed the fix-duplicate-user-bug-in-users-filter branch from d076882 to 552fefd Compare October 4, 2023 14:56
@chrislo chrislo changed the title WIP: failing tests to reproduce bug Prevent duplicate rows in /users when filters applied Oct 4, 2023
@chrislo chrislo requested a review from floehopper October 4, 2023 14:59
@chrislo chrislo force-pushed the fix-duplicate-user-bug-in-users-filter branch from 552fefd to d98a52d Compare October 4, 2023 14:59
@chrislo chrislo marked this pull request as ready for review October 4, 2023 14:59
@floehopper floehopper self-assigned this Oct 4, 2023
Copy link
Contributor

@floehopper floehopper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work! LGTM 👍

One minor thing - the test names suggest that the problem is specific to multiple permissions on the SAME app, but the commit note / PR description doesn't mention that. I'm guessing that's just an omission in the commit note / PR description, but either way it would be nice to bring the two things into line.

@chrislo
Copy link
Contributor Author

chrislo commented Oct 4, 2023

Thanks @floehopper! I'll clarify that in the commit note before merging.

Trello: https://trello.com/c/i27MQaus

This fixes an issue with duplication of a user in the table on
/users (and the corresponding CSV export) when multiple permissions
for the same application were selected in the filter.

For example if a user had the "GDS editor" and "signon" permission for
the "Transition" app and we selected both of those permissions in the
filter drop down, this user would appear twice in table.

The solution is to call `distinct` on the `User#with_permission`
scope, which is in turn called by `UsersFilter#users`. I've decided to
add a test case in both places to avoid a regression in `UsersFilter`
in case it changes in the future.

Note we have to add the `unscoped` method to `SupportedPermission` in
the `User#with_permission` scope since `SupportedPermission` has an
`order(:name)` `default_scope` and calling `distinct` on an order
query is not supported by MySQL:

    Mysql2::Error: Expression #1 of ORDER BY clause is not in SELECT
    list, references column 'signon_test.supported_permissions.name' which
    is not in SELECT list; this is incompatible with DISTINCT
@chrislo chrislo force-pushed the fix-duplicate-user-bug-in-users-filter branch from d98a52d to b4b0f5b Compare October 4, 2023 15:25
@chrislo chrislo enabled auto-merge October 4, 2023 15:26
@chrislo chrislo merged commit 0aed7d6 into main Oct 4, 2023
6 checks passed
@chrislo chrislo deleted the fix-duplicate-user-bug-in-users-filter branch October 4, 2023 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants