diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index fe2fd7014..a68b55f8d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -5,7 +5,6 @@ class ApplicationController < ActionController::Base before_action :check_maintenance_status before_action :set_paper_trail_whodunnit - before_action :set_current_user def check_maintenance_status if FeatureToggle.service_moved? @@ -42,8 +41,4 @@ protected def byte_order_mark "\uFEFF" end - - def set_current_user - Thread.current[:current_user] = current_user - end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 9866f92d9..58440d59e 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.search_by(params["query"]).limit(20) + users = User.visible_to_user(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 2ca934480..b2ebb2549 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.where(id: user_id)&.first + selected_user = User.visible_to_user(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.where(id: user_id)&.first + selected_user_option = User.visible_to_user(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 149a53a71..ebc2f5f1e 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.search_by(param)) } + scope :filter_by_user_text_search, ->(param, user) { where(assigned_to: User.visible_to_user(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 831a608eb..f98de6a93 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.search_by(param)) } + scope :filter_by_user_text_search, ->(param, user) { where(assigned_to: User.visible_to_user(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 4cc527e0e..b1f7c8591 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,10 +6,6 @@ class User < ApplicationRecord devise :database_authenticatable, :recoverable, :rememberable, :trackable, :lockable, :two_factor_authenticatable, :confirmable, :timeoutable - def self.current - Thread.current[:current_user] - end - # Marked as optional because we validate organisation_id below instead so that # the error message is linked to the right field on the form belongs_to :organisation, optional: true @@ -88,14 +84,8 @@ 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, lambda { - current_user = User.current - if current_user&.support? - where(discarded_at: nil) - else - where(discarded_at: nil).where(organisation: current_user.organisation.child_organisations + [current_user.organisation]) - end - } + scope :visible, -> { where(discarded_at: nil) } + scope :visible_to_user, ->(user) { user.support? ? visible : visible.where(organisation: user.organisation.child_organisations + [user.organisation]) } attr_accessor :log_reassignment