diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 58440d59e..1197f1533 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -33,7 +33,7 @@ class UsersController < ApplicationController end def search - users = User.visible_to_user(current_user).search_by(params["query"]).limit(20) + users = User.visible(current_user).search_by(params["query"]).limit(20) user_data = users.each_with_object({}) do |user, hash| hash[user.id] = { value: user.name, hint: user.email } diff --git a/app/helpers/filters_helper.rb b/app/helpers/filters_helper.rb index b2ebb2549..da3310e03 100644 --- a/app/helpers/filters_helper.rb +++ b/app/helpers/filters_helper.rb @@ -112,7 +112,7 @@ module FiltersHelper def assigned_to_filter_options(filter_type) if applied_filters(filter_type)["assigned_to"] == "specific_user" && applied_filters(filter_type)["user"].present? user_id = applied_filters(filter_type)["user"] - selected_user = User.visible_to_user(current_user).where(id: user_id)&.first + selected_user = User.visible(current_user).where(id: user_id)&.first return [OpenStruct.new(id: selected_user.id, name: selected_user.name, hint: selected_user.email)] if selected_user.present? end @@ -306,7 +306,7 @@ private return "You" if session_filters["assigned_to"].include?("you") user_id = session_filters["user"].to_i - selected_user_option = User.visible_to_user(current_user).where(id: user_id)&.first + selected_user_option = User.visible(current_user).where(id: user_id)&.first return unless selected_user_option diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index ebc2f5f1e..86866f647 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -132,7 +132,7 @@ class LettingsLog < Log illness_type_10: false) } - scope :filter_by_user_text_search, ->(param, user) { where(assigned_to: User.visible_to_user(user).search_by(param)) } + scope :filter_by_user_text_search, ->(param, user) { where(assigned_to: User.visible(user).search_by(param)) } scope :filter_by_owning_organisation_text_search, ->(param, _user) { where(owning_organisation: Organisation.search_by(param)) } scope :filter_by_managing_organisation_text_search, ->(param, _user) { where(managing_organisation: Organisation.search_by(param)) } diff --git a/app/models/log.rb b/app/models/log.rb index f98de6a93..cb66074cf 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -53,7 +53,7 @@ class Log < ApplicationRecord scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org).or(where(managing_organisation: org)) } scope :filter_by_owning_organisation, ->(owning_organisation, _user = nil) { where(owning_organisation:) } scope :filter_by_managing_organisation, ->(managing_organisation, _user = nil) { where(managing_organisation:) } - scope :filter_by_user_text_search, ->(param, user) { where(assigned_to: User.visible_to_user(user).search_by(param)) } + scope :filter_by_user_text_search, ->(param, user) { where(assigned_to: User.visible(user).search_by(param)) } scope :filter_by_owning_organisation_text_search, ->(param, _user) { where(owning_organisation: Organisation.search_by(param)) } scope :filter_by_managing_organisation_text_search, ->(param, _user) { where(managing_organisation: Organisation.search_by(param)) } diff --git a/app/models/user.rb b/app/models/user.rb index b1f7c8591..4c952dbe2 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -84,8 +84,13 @@ class User < ApplicationRecord scope :not_signed_in, -> { where(last_sign_in_at: nil, active: true) } scope :deactivated, -> { where(active: false) } scope :active_status, -> { where(active: true).where.not(last_sign_in_at: nil) } - scope :visible, -> { where(discarded_at: nil) } - scope :visible_to_user, ->(user) { user.support? ? visible : visible.where(organisation: user.organisation.child_organisations + [user.organisation]) } + scope :visible, lambda { |user = nil| + if user && !user.support? + where(discarded_at: nil, organisation: user.organisation.child_organisations + [user.organisation]) + else + where(discarded_at: nil) + end + } attr_accessor :log_reassignment