mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 03:41:12 +08:00
FIX: display total number of other accounts with the same IP address in the IP lookup dialog
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
require_dependency 'trust_level'
|
||||
|
||||
class AdminUserIndexQuery
|
||||
|
||||
def initialize(params = {}, klass = User, trust_levels = TrustLevel.levels)
|
||||
@params = params
|
||||
@query = initialize_query_with_order(klass)
|
||||
@ -9,80 +10,7 @@ class AdminUserIndexQuery
|
||||
|
||||
attr_reader :params, :trust_levels
|
||||
|
||||
def initialize_query_with_order(klass)
|
||||
order = [params[:order]]
|
||||
|
||||
if params[:query] == "active"
|
||||
order << "COALESCE(last_seen_at, to_date('1970-01-01', 'YYYY-MM-DD')) DESC"
|
||||
else
|
||||
order << "created_at DESC"
|
||||
end
|
||||
|
||||
order << "username"
|
||||
|
||||
klass.order(order.reject(&:blank?).join(","))
|
||||
end
|
||||
|
||||
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])
|
||||
end
|
||||
end
|
||||
|
||||
def filter_by_query_classification
|
||||
case params[:query]
|
||||
when 'admins' then @query.where(admin: true)
|
||||
when 'moderators' then @query.where(moderator: true)
|
||||
when 'blocked' then @query.blocked
|
||||
when 'suspended' then @query.suspended
|
||||
when 'pending' then @query.not_suspended.where(approved: false)
|
||||
end
|
||||
end
|
||||
|
||||
def filter_by_search
|
||||
if params[:filter].present?
|
||||
if ip = IPAddr.new(params[:filter]) rescue nil
|
||||
@query.where('ip_address <<= :ip OR registration_ip_address <<= :ip', ip: ip.to_cidr_s)
|
||||
else
|
||||
@query.where('username_lower ILIKE :filter OR email ILIKE :filter', filter: "%#{params[:filter]}%")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def filter_by_ip
|
||||
if params[:ip].present?
|
||||
@query.where('ip_address = :ip OR registration_ip_address = :ip', ip: params[:ip])
|
||||
end
|
||||
end
|
||||
|
||||
def filter_exclude
|
||||
if params[:exclude].present?
|
||||
@query.where('id != ?', params[:exclude])
|
||||
end
|
||||
end
|
||||
|
||||
def limit
|
||||
limit = params[:limit].to_i
|
||||
@query.limit(limit > 0 ? limit : 100)
|
||||
end
|
||||
|
||||
# this might not be needed in rails 4 ?
|
||||
def append(active_relation)
|
||||
@query = active_relation if active_relation
|
||||
end
|
||||
|
||||
def find_users_query
|
||||
append filter_by_trust
|
||||
append filter_by_query_classification
|
||||
append filter_by_ip
|
||||
append filter_exclude
|
||||
append filter_by_search
|
||||
append limit
|
||||
@query
|
||||
end
|
||||
|
||||
def find_users
|
||||
def find_users(limit=100)
|
||||
find_users_query.includes(:user_stat)
|
||||
.includes(:single_sign_on_record)
|
||||
.includes(:facebook_user_info)
|
||||
@ -91,5 +19,80 @@ class AdminUserIndexQuery
|
||||
.includes(:google_user_info)
|
||||
.includes(:oauth2_user_info)
|
||||
.includes(:user_open_ids)
|
||||
.limit(limit)
|
||||
end
|
||||
|
||||
def count_users
|
||||
find_users_query.count
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def initialize_query_with_order(klass)
|
||||
order = [params[:order]]
|
||||
|
||||
if params[:query] == "active"
|
||||
order << "COALESCE(last_seen_at, to_date('1970-01-01', 'YYYY-MM-DD')) DESC"
|
||||
else
|
||||
order << "created_at DESC"
|
||||
end
|
||||
|
||||
order << "username"
|
||||
|
||||
klass.order(order.reject(&:blank?).join(","))
|
||||
end
|
||||
|
||||
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])
|
||||
end
|
||||
end
|
||||
|
||||
def filter_by_query_classification
|
||||
case params[:query]
|
||||
when 'admins' then @query.where(admin: true)
|
||||
when 'moderators' then @query.where(moderator: true)
|
||||
when 'blocked' then @query.blocked
|
||||
when 'suspended' then @query.suspended
|
||||
when 'pending' then @query.not_suspended.where(approved: false)
|
||||
end
|
||||
end
|
||||
|
||||
def filter_by_search
|
||||
if params[:filter].present?
|
||||
if ip = IPAddr.new(params[:filter]) rescue nil
|
||||
@query.where('ip_address <<= :ip OR registration_ip_address <<= :ip', ip: ip.to_cidr_s)
|
||||
else
|
||||
@query.where('username_lower ILIKE :filter OR email ILIKE :filter', filter: "%#{params[:filter]}%")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def filter_by_ip
|
||||
if params[:ip].present?
|
||||
@query.where('ip_address = :ip OR registration_ip_address = :ip', ip: params[:ip])
|
||||
end
|
||||
end
|
||||
|
||||
def filter_exclude
|
||||
if params[:exclude].present?
|
||||
@query.where('id != ?', params[:exclude])
|
||||
end
|
||||
end
|
||||
|
||||
# this might not be needed in rails 4 ?
|
||||
def append(active_relation)
|
||||
@query = active_relation if active_relation
|
||||
end
|
||||
|
||||
def find_users_query
|
||||
append filter_by_trust
|
||||
append filter_by_query_classification
|
||||
append filter_by_ip
|
||||
append filter_exclude
|
||||
append filter_by_search
|
||||
@query
|
||||
end
|
||||
|
||||
end
|
||||
|
Reference in New Issue
Block a user