2018-09-11 19:08:34 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-15 13:53:12 +00: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
|
2020-04-15 09:09:46 +00:00
|
|
|
# without_projects: boolean
|
2017-05-15 13:53:12 +00:00
|
|
|
#
|
|
|
|
class UsersFinder
|
2017-07-07 16:31:50 +00:00
|
|
|
include CreatedAtFilter
|
2017-09-28 16:49:42 +00:00
|
|
|
include CustomAttributesFilter
|
2017-07-07 09:38:01 +00:00
|
|
|
|
2017-05-15 13:53:12 +00:00
|
|
|
attr_accessor :current_user, :params
|
|
|
|
|
|
|
|
def initialize(current_user, params = {})
|
|
|
|
@current_user = current_user
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2017-12-04 09:49:53 +00:00
|
|
|
users = User.all.order_id_desc
|
2017-05-15 13:53:12 +00:00
|
|
|
users = by_username(users)
|
|
|
|
users = by_search(users)
|
|
|
|
users = by_blocked(users)
|
|
|
|
users = by_active(users)
|
2017-06-29 07:43:41 +00:00
|
|
|
users = by_external_identity(users)
|
|
|
|
users = by_external(users)
|
2018-04-20 09:21:48 +00:00
|
|
|
users = by_2fa(users)
|
2017-07-07 07:29:00 +00:00
|
|
|
users = by_created_at(users)
|
2020-04-15 09:09:46 +00:00
|
|
|
users = by_without_projects(users)
|
2017-09-28 16:49:42 +00:00
|
|
|
users = by_custom_attributes(users)
|
2017-05-15 13:53:12 +00:00
|
|
|
|
|
|
|
users
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def by_username(users)
|
|
|
|
return users unless params[:username]
|
|
|
|
|
2018-10-18 09:06:44 +00:00
|
|
|
users.by_username(params[:username])
|
2017-05-15 13:53:12 +00: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
|
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-05-15 13:53:12 +00:00
|
|
|
def by_external_identity(users)
|
2017-06-29 07:43:41 +00:00
|
|
|
return users unless current_user&.admin? && params[:extern_uid] && params[:provider]
|
2017-05-15 13:53:12 +00:00
|
|
|
|
|
|
|
users.joins(:identities).merge(Identity.with_extern_uid(params[:provider], params[:extern_uid]))
|
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-05-15 13:53:12 +00:00
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-05-15 13:53:12 +00:00
|
|
|
def by_external(users)
|
2017-06-29 07:43:41 +00:00
|
|
|
return users = users.where.not(external: true) unless current_user&.admin?
|
2017-05-15 13:53:12 +00:00
|
|
|
return users unless params[:external]
|
|
|
|
|
|
|
|
users.external
|
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-04-20 09:21:48 +00:00
|
|
|
|
|
|
|
def by_2fa(users)
|
|
|
|
case params[:two_factor]
|
|
|
|
when 'enabled'
|
|
|
|
users.with_two_factor
|
|
|
|
when 'disabled'
|
|
|
|
users.without_two_factor
|
|
|
|
else
|
|
|
|
users
|
|
|
|
end
|
|
|
|
end
|
2020-04-15 09:09:46 +00:00
|
|
|
|
|
|
|
def by_without_projects(users)
|
|
|
|
return users unless params[:without_projects]
|
|
|
|
|
|
|
|
users.without_projects
|
|
|
|
end
|
2017-05-15 13:53:12 +00:00
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
|
|
|
UsersFinder.prepend_if_ee('EE::UsersFinder')
|