2013-07-30 03:36:34 -04:00
|
|
|
require_dependency 'trust_level'
|
|
|
|
|
2013-06-19 12:11:04 -04:00
|
|
|
class AdminUserIndexQuery
|
|
|
|
def initialize(params = {}, klass = User, trust_levels = TrustLevel.levels)
|
|
|
|
@params = params
|
|
|
|
@query = initialize_query_with_order(klass)
|
|
|
|
@trust_levels = trust_levels
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :params, :trust_levels
|
|
|
|
|
|
|
|
def initialize_query_with_order(klass)
|
2014-11-19 15:38:53 -05:00
|
|
|
order = [params[:order]]
|
|
|
|
|
2013-06-19 12:11:04 -04:00
|
|
|
if params[:query] == "active"
|
2014-11-19 15:38:53 -05:00
|
|
|
order << "COALESCE(last_seen_at, to_date('1970-01-01', 'YYYY-MM-DD')) DESC"
|
2013-06-19 12:11:04 -04:00
|
|
|
else
|
2014-11-19 15:38:53 -05:00
|
|
|
order << "created_at DESC"
|
2013-06-19 12:11:04 -04:00
|
|
|
end
|
2014-11-19 15:38:53 -05:00
|
|
|
|
|
|
|
order << "username"
|
|
|
|
|
|
|
|
klass.order(order.reject(&:blank?).join(","))
|
2013-06-19 12:11:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def filter_by_trust
|
2014-08-14 17:54:55 -04:00
|
|
|
levels = trust_levels.map { |key, _| key.to_s }
|
2013-06-19 12:11:04 -04:00
|
|
|
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]
|
2014-09-29 16:31:05 -04:00
|
|
|
when 'admins' then @query.where(admin: true)
|
|
|
|
when 'moderators' then @query.where(moderator: true)
|
2013-06-19 12:11:04 -04:00
|
|
|
when 'blocked' then @query.blocked
|
2013-11-07 13:53:32 -05:00
|
|
|
when 'suspended' then @query.suspended
|
2014-09-29 16:31:05 -04:00
|
|
|
when 'pending' then @query.not_suspended.where(approved: false)
|
2013-06-19 12:11:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_by_search
|
|
|
|
if params[:filter].present?
|
2014-10-29 17:08:41 -04:00
|
|
|
if params[:filter] =~ Resolv::IPv4::Regex || params[:filter] =~ Resolv::IPv6::Regex
|
|
|
|
@query.where('ip_address = :ip OR registration_ip_address = :ip', ip: params[:filter])
|
2014-10-07 06:05:38 -04:00
|
|
|
else
|
2014-10-29 17:08:41 -04:00
|
|
|
@query.where('username_lower ILIKE :filter OR email ILIKE :filter', filter: "%#{params[:filter]}%")
|
2014-10-07 06:05:38 -04:00
|
|
|
end
|
2013-06-19 12:11:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-15 12:14:19 -04:00
|
|
|
def filter_by_ip
|
|
|
|
if params[:ip].present?
|
2014-10-07 06:05:38 -04:00
|
|
|
@query.where('ip_address = :ip OR registration_ip_address = :ip', ip: params[:ip])
|
2014-06-15 12:14:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_exclude
|
|
|
|
if params[:exclude].present?
|
|
|
|
@query.where('id != ?', params[:exclude])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-19 12:11:04 -04:00
|
|
|
# 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
|
2014-06-15 12:14:19 -04:00
|
|
|
append filter_by_ip
|
|
|
|
append filter_exclude
|
2013-06-19 12:11:04 -04:00
|
|
|
append filter_by_search
|
|
|
|
@query
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_users
|
2014-09-29 16:31:05 -04:00
|
|
|
find_users_query.includes(:user_stat)
|
|
|
|
.includes(:single_sign_on_record)
|
|
|
|
.includes(:facebook_user_info)
|
|
|
|
.includes(:twitter_user_info)
|
|
|
|
.includes(:github_user_info)
|
|
|
|
.includes(:google_user_info)
|
|
|
|
.includes(:oauth2_user_info)
|
2014-11-03 06:46:08 -05:00
|
|
|
.includes(:user_open_ids)
|
2014-11-18 06:04:59 -05:00
|
|
|
.take(100)
|
2013-06-19 12:11:04 -04:00
|
|
|
end
|
2013-07-30 03:36:34 -04:00
|
|
|
end
|