gitlab-org--gitlab-foss/app/finders/users_finder.rb

98 lines
2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2017-05-15 09:53:12 -04:00
# UsersFinder
#
# Used to filter users by set of params
#
# Arguments:
# current_user - which user use
# params:
# username: string
# extern_uid: string
# provider: string
# search: string
# active: boolean
# blocked: boolean
# external: boolean
#
class UsersFinder
include CreatedAtFilter
2017-09-28 12:49:42 -04:00
include CustomAttributesFilter
2017-07-07 05:38:01 -04:00
2017-05-15 09:53:12 -04:00
attr_accessor :current_user, :params
def initialize(current_user, params = {})
@current_user = current_user
@params = params
end
def execute
2017-12-04 04:49:53 -05:00
users = User.all.order_id_desc
2017-05-15 09:53:12 -04:00
users = by_username(users)
users = by_search(users)
users = by_blocked(users)
users = by_active(users)
users = by_external_identity(users)
users = by_external(users)
users = by_2fa(users)
users = by_created_at(users)
2017-09-28 12:49:42 -04:00
users = by_custom_attributes(users)
2017-05-15 09:53:12 -04:00
users
end
private
def by_username(users)
return users unless params[:username]
users.by_username(params[:username])
2017-05-15 09:53:12 -04:00
end
def by_search(users)
return users unless params[:search].present?
users.search(params[:search])
end
def by_blocked(users)
return users unless params[:blocked]
users.blocked
end
def by_active(users)
return users unless params[:active]
users.active
end
# rubocop: disable CodeReuse/ActiveRecord
2017-05-15 09:53:12 -04:00
def by_external_identity(users)
return users unless current_user&.admin? && params[:extern_uid] && params[:provider]
2017-05-15 09:53:12 -04:00
users.joins(:identities).merge(Identity.with_extern_uid(params[:provider], params[:extern_uid]))
end
# rubocop: enable CodeReuse/ActiveRecord
2017-05-15 09:53:12 -04:00
# rubocop: disable CodeReuse/ActiveRecord
2017-05-15 09:53:12 -04:00
def by_external(users)
return users = users.where.not(external: true) unless current_user&.admin?
2017-05-15 09:53:12 -04:00
return users unless params[:external]
users.external
end
# rubocop: enable CodeReuse/ActiveRecord
def by_2fa(users)
case params[:two_factor]
when 'enabled'
users.with_two_factor
when 'disabled'
users.without_two_factor
else
users
end
end
2017-05-15 09:53:12 -04:00
end