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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class User < ApplicationRecord
scope :not_setup_2sv, -> { not_exempt_from_2sv.does_not_have_2sv }

scope :with_role, ->(role) { where(role:) }
scope :with_permission, ->(permission) { joins(:supported_permissions).merge(SupportedPermission.where(id: permission)) }
scope :with_permission, ->(permission) { joins(:supported_permissions).merge(SupportedPermission.unscoped.where(id: permission)).distinct }
scope :with_organisation, ->(organisation) { where(organisation:) }
scope :with_partially_matching_name, ->(name) { where(arel_table[:name].matches("%#{name}%")) }
scope :with_partially_matching_email, ->(email) { where(arel_table[:email].matches("%#{email}%")) }
Expand Down
10 changes: 10 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,16 @@ def setup
specified_permissions = [app1.signin_permission, app2.signin_permission]
assert_equal [user1, user2], User.with_permission(specified_permissions.map(&:to_param))
end

should "only return a user once even when they have two permissions for the same app" do
app = create(:application)

permission = create(:supported_permission, application: app)
create(:user, supported_permissions: [app.signin_permission, permission])

specified_permissions = [app.signin_permission, permission]
assert_equal 1, User.with_permission(specified_permissions.map(&:to_param)).count
end
end

context ".with_organisation" do
Expand Down
11 changes: 11 additions & 0 deletions test/models/users_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,17 @@ class UsersFilterTest < ActiveSupport::TestCase

assert_equal %w[user1 user3], filter.users.map(&:name)
end

should "return a user once if they have multiple selected permissions for the same app" do
app = create(:application, name: "App 1")
permission = create(:supported_permission, application: app, name: "Permission 1")

create(:user, name: "user", supported_permissions: [app.signin_permission, permission])

filter = UsersFilter.new(User.all, @current_user, permissions: [app.signin_permission, permission].map(&:to_param))

assert_equal %w[user], filter.users.map(&:name)
end
end

context "#permission_option_select_options" do
Expand Down