2019-04-29 20:25:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-06-19 12:11:04 -04:00
|
|
|
class AdminUserIndexQuery
|
|
|
|
def initialize(params = {}, klass = User, trust_levels = TrustLevel.levels)
|
|
|
|
@params = params
|
2022-04-08 10:59:44 -04:00
|
|
|
@query = initialize_query_with_order(klass)
|
2013-06-19 12:11:04 -04:00
|
|
|
@trust_levels = trust_levels
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :params, :trust_levels
|
|
|
|
|
2017-02-25 13:51:40 -05:00
|
|
|
SORTABLE_MAPPING = {
|
|
|
|
"created" => "created_at",
|
|
|
|
"last_emailed" => "COALESCE(last_emailed_at, to_date('1970-01-01', 'YYYY-MM-DD'))",
|
|
|
|
"seen" => "COALESCE(last_seen_at, to_date('1970-01-01', 'YYYY-MM-DD'))",
|
|
|
|
"username" => "username",
|
|
|
|
"email" => "email",
|
|
|
|
"trust_level" => "trust_level",
|
|
|
|
"days_visited" => "user_stats.days_visited",
|
|
|
|
"posts_read" => "user_stats.posts_read_count",
|
|
|
|
"topics_viewed" => "user_stats.topics_entered",
|
|
|
|
"posts" => "user_stats.post_count",
|
|
|
|
"read_time" => "user_stats.time_read",
|
|
|
|
}
|
|
|
|
|
2014-11-24 13:34:04 -05:00
|
|
|
def find_users(limit = 100)
|
2017-05-12 16:28:35 -04:00
|
|
|
page = params[:page].to_i - 1
|
|
|
|
page = 0 if page < 0
|
|
|
|
find_users_query.limit(limit).offset(page * limit)
|
2014-11-24 13:34:04 -05:00
|
|
|
end
|
2014-11-19 15:38:53 -05:00
|
|
|
|
2014-11-24 13:34:04 -05:00
|
|
|
def count_users
|
|
|
|
find_users_query.count
|
|
|
|
end
|
2014-11-19 15:38:53 -05:00
|
|
|
|
2017-02-24 19:11:17 -05:00
|
|
|
def custom_direction
|
2021-11-12 09:52:59 -05:00
|
|
|
if params[:ascending]
|
|
|
|
Discourse.deprecate(
|
|
|
|
":ascending is deprecated please use :asc instead",
|
|
|
|
output_in_test: true,
|
|
|
|
drop_from: "2.9.0",
|
|
|
|
)
|
2023-01-09 07:10:19 -05:00
|
|
|
end
|
2020-05-14 22:10:59 -04:00
|
|
|
asc = params[:asc] || params[:ascending]
|
2017-02-24 19:11:17 -05:00
|
|
|
asc.present? && asc ? "ASC" : "DESC"
|
|
|
|
end
|
|
|
|
|
2014-11-24 13:48:08 -05:00
|
|
|
def initialize_query_with_order(klass)
|
2016-07-28 11:42:06 -04:00
|
|
|
order = []
|
|
|
|
|
|
|
|
custom_order = params[:order]
|
|
|
|
if custom_order.present? &&
|
2023-01-20 13:52:49 -05:00
|
|
|
without_dir = SORTABLE_MAPPING[custom_order.downcase.sub(/ (asc|desc)\z/, "")]
|
2017-02-25 13:51:40 -05:00
|
|
|
order << "#{without_dir} #{custom_direction}"
|
2016-07-28 11:42:06 -04:00
|
|
|
end
|
2017-02-25 13:51:40 -05:00
|
|
|
|
2017-02-24 19:11:17 -05:00
|
|
|
if !custom_order.present?
|
|
|
|
if params[:query] == "active"
|
2022-04-08 10:59:44 -04:00
|
|
|
order << "users.last_seen_at DESC NULLS LAST"
|
2017-02-24 19:11:17 -05:00
|
|
|
else
|
|
|
|
order << "users.created_at DESC"
|
|
|
|
end
|
2014-11-19 15:38:53 -05:00
|
|
|
|
2017-02-24 19:11:17 -05:00
|
|
|
order << "users.username"
|
2014-11-24 13:48:08 -05:00
|
|
|
end
|
2013-06-19 12:11:04 -04:00
|
|
|
|
2019-03-15 02:59:16 -04:00
|
|
|
query = klass.includes(:totps).order(order.reject(&:blank?).join(","))
|
|
|
|
|
|
|
|
query = query.includes(:user_stat) unless params[:stats].present? && params[:stats] == false
|
|
|
|
|
2022-05-04 13:07:22 -04:00
|
|
|
query = query.joins(:primary_email) if params[:show_emails] == "true"
|
|
|
|
|
2019-03-15 02:59:16 -04:00
|
|
|
query
|
2014-11-24 13:48:08 -05:00
|
|
|
end
|
2013-06-19 12:11:04 -04:00
|
|
|
|
2014-11-24 13:48:08 -05:00
|
|
|
def filter_by_trust
|
|
|
|
levels = trust_levels.map { |key, _| key.to_s }
|
|
|
|
if levels.include?(params[:query])
|
|
|
|
@query.where("trust_level = ?", trust_levels[params[:query].to_sym])
|
2013-06-19 12:11:04 -04:00
|
|
|
end
|
2014-11-24 13:48:08 -05:00
|
|
|
end
|
2013-06-19 12:11:04 -04:00
|
|
|
|
2014-11-24 13:48:08 -05:00
|
|
|
def filter_by_query_classification
|
|
|
|
case params[:query]
|
2014-11-26 13:10:12 -05:00
|
|
|
when "staff"
|
|
|
|
@query.where("admin or moderator")
|
2014-11-24 13:48:08 -05:00
|
|
|
when "admins"
|
|
|
|
@query.where(admin: true)
|
|
|
|
when "moderators"
|
|
|
|
@query.where(moderator: true)
|
2017-11-10 12:18:08 -05:00
|
|
|
when "silenced"
|
|
|
|
@query.silenced
|
2014-11-24 13:48:08 -05:00
|
|
|
when "suspended"
|
|
|
|
@query.suspended
|
2017-09-04 12:55:23 -04:00
|
|
|
when "pending"
|
|
|
|
@query.not_suspended.where(approved: false, active: true)
|
2018-05-16 19:52:49 -04:00
|
|
|
when "staged"
|
|
|
|
@query.where(staged: true)
|
2013-06-19 12:11:04 -04:00
|
|
|
end
|
2014-11-24 13:48:08 -05:00
|
|
|
end
|
2013-06-19 12:11:04 -04:00
|
|
|
|
2014-11-24 13:48:08 -05:00
|
|
|
def filter_by_search
|
2017-11-09 01:04:21 -05:00
|
|
|
if params[:email].present?
|
2022-04-08 10:59:44 -04:00
|
|
|
return @query.joins(:primary_email).where("user_emails.email = ?", params[:email].downcase)
|
2017-11-09 01:04:21 -05:00
|
|
|
end
|
|
|
|
|
2017-11-09 00:53:14 -05:00
|
|
|
filter = params[:filter]
|
|
|
|
if filter.present?
|
2019-04-29 20:25:53 -04:00
|
|
|
filter = filter.strip
|
2017-11-09 00:53:14 -05:00
|
|
|
if ip =
|
2023-01-09 07:10:19 -05:00
|
|
|
begin
|
2017-11-09 00:53:14 -05:00
|
|
|
IPAddr.new(filter)
|
|
|
|
rescue StandardError
|
|
|
|
nil
|
2023-01-09 07:10:19 -05:00
|
|
|
end
|
2014-11-24 13:48:08 -05:00
|
|
|
@query.where("ip_address <<= :ip OR registration_ip_address <<= :ip", ip: ip.to_cidr_s)
|
|
|
|
else
|
2018-03-22 01:42:46 -04:00
|
|
|
@query.filter_by_username_or_email(filter)
|
2014-11-24 13:34:04 -05:00
|
|
|
end
|
2014-06-15 12:14:19 -04:00
|
|
|
end
|
2014-11-24 13:48:08 -05:00
|
|
|
end
|
2014-06-15 12:14:19 -04:00
|
|
|
|
2014-11-24 13:48:08 -05:00
|
|
|
def filter_by_ip
|
|
|
|
if params[:ip].present?
|
2016-10-10 10:18:57 -04:00
|
|
|
@query.where("ip_address = :ip OR registration_ip_address = :ip", ip: params[:ip].strip)
|
2014-06-15 12:14:19 -04:00
|
|
|
end
|
2014-11-24 13:48:08 -05:00
|
|
|
end
|
2014-06-15 12:14:19 -04:00
|
|
|
|
2014-11-24 13:48:08 -05:00
|
|
|
def filter_exclude
|
2017-04-26 14:47:36 -04:00
|
|
|
@query.where("users.id != ?", params[:exclude]) if params[:exclude].present?
|
2014-11-24 13:48:08 -05:00
|
|
|
end
|
2014-11-24 12:05:40 -05:00
|
|
|
|
2014-11-24 13:48:08 -05:00
|
|
|
# this might not be needed in rails 4 ?
|
|
|
|
def append(active_relation)
|
|
|
|
@query = active_relation if active_relation
|
|
|
|
end
|
2013-06-19 12:11:04 -04:00
|
|
|
|
2014-11-24 13:48:08 -05:00
|
|
|
def find_users_query
|
|
|
|
append filter_by_trust
|
|
|
|
append filter_by_query_classification
|
|
|
|
append filter_by_ip
|
|
|
|
append filter_exclude
|
|
|
|
append filter_by_search
|
2017-08-21 22:16:13 -04:00
|
|
|
@query
|
2014-11-24 13:48:08 -05:00
|
|
|
end
|
2013-07-30 03:36:34 -04:00
|
|
|
end
|