2018-09-11 15:08:34 -04:00
|
|
|
# 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
|
2021-02-24 10:11:10 -05:00
|
|
|
# non_external: boolean
|
2020-04-15 05:09:46 -04:00
|
|
|
# without_projects: boolean
|
2020-06-08 20:08:47 -04:00
|
|
|
# sort: string
|
|
|
|
# id: integer
|
2020-08-31 11:10:41 -04:00
|
|
|
# non_internal: boolean
|
2017-05-15 09:53:12 -04:00
|
|
|
#
|
|
|
|
class UsersFinder
|
2017-07-07 12:31:50 -04:00
|
|
|
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
|
2022-02-01 16:12:02 -05:00
|
|
|
users = base_scope
|
2017-05-15 09:53:12 -04:00
|
|
|
users = by_username(users)
|
2020-06-08 20:08:47 -04:00
|
|
|
users = by_id(users)
|
2021-01-18 07:10:41 -05:00
|
|
|
users = by_admins(users)
|
2017-05-15 09:53:12 -04:00
|
|
|
users = by_search(users)
|
|
|
|
users = by_blocked(users)
|
|
|
|
users = by_active(users)
|
2017-06-29 03:43:41 -04:00
|
|
|
users = by_external_identity(users)
|
|
|
|
users = by_external(users)
|
2021-02-24 10:11:10 -05:00
|
|
|
users = by_non_external(users)
|
2018-04-20 05:21:48 -04:00
|
|
|
users = by_2fa(users)
|
2017-07-07 03:29:00 -04:00
|
|
|
users = by_created_at(users)
|
2020-04-15 05:09:46 -04:00
|
|
|
users = by_without_projects(users)
|
2017-09-28 12:49:42 -04:00
|
|
|
users = by_custom_attributes(users)
|
2020-08-31 11:10:41 -04:00
|
|
|
users = by_non_internal(users)
|
2022-03-30 20:08:33 -04:00
|
|
|
users = by_without_project_bots(users)
|
2017-05-15 09:53:12 -04:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
order(users)
|
2017-05-15 09:53:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-02-01 16:12:02 -05:00
|
|
|
def base_scope
|
2022-04-06 17:08:15 -04:00
|
|
|
scope = current_user&.admin? ? User.all : User.without_forbidden_states
|
|
|
|
scope.order_id_desc
|
2022-02-01 16:12:02 -05:00
|
|
|
end
|
|
|
|
|
2017-05-15 09:53:12 -04:00
|
|
|
def by_username(users)
|
|
|
|
return users unless params[:username]
|
|
|
|
|
2018-10-18 05:06:44 -04:00
|
|
|
users.by_username(params[:username])
|
2017-05-15 09:53:12 -04:00
|
|
|
end
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
def by_id(users)
|
|
|
|
return users unless params[:id]
|
|
|
|
|
|
|
|
users.id_in(params[:id])
|
|
|
|
end
|
|
|
|
|
2021-01-18 07:10:41 -05:00
|
|
|
def by_admins(users)
|
|
|
|
return users unless params[:admins] && current_user&.can_read_all_resources?
|
|
|
|
|
|
|
|
users.admins
|
|
|
|
end
|
|
|
|
|
2017-05-15 09:53:12 -04:00
|
|
|
def by_search(users)
|
|
|
|
return users unless params[:search].present?
|
|
|
|
|
2022-02-03 19:13:53 -05:00
|
|
|
users.search(params[:search], with_private_emails: current_user&.admin?)
|
2017-05-15 09:53:12 -04:00
|
|
|
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 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-05-15 09:53:12 -04:00
|
|
|
def by_external_identity(users)
|
2017-06-29 03:43:41 -04:00
|
|
|
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
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-05-15 09:53:12 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-05-15 09:53:12 -04:00
|
|
|
def by_external(users)
|
|
|
|
return users unless params[:external]
|
|
|
|
|
|
|
|
users.external
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-04-20 05:21:48 -04:00
|
|
|
|
2021-02-24 10:11:10 -05:00
|
|
|
def by_non_external(users)
|
|
|
|
return users unless params[:non_external]
|
|
|
|
|
|
|
|
users.non_external
|
|
|
|
end
|
|
|
|
|
2018-04-20 05:21:48 -04: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 05:09:46 -04:00
|
|
|
|
|
|
|
def by_without_projects(users)
|
|
|
|
return users unless params[:without_projects]
|
|
|
|
|
|
|
|
users.without_projects
|
|
|
|
end
|
2020-06-08 20:08:47 -04:00
|
|
|
|
2020-08-31 11:10:41 -04:00
|
|
|
def by_non_internal(users)
|
|
|
|
return users unless params[:non_internal]
|
|
|
|
|
|
|
|
users.non_internal
|
|
|
|
end
|
|
|
|
|
2022-03-30 20:08:33 -04:00
|
|
|
def by_without_project_bots(users)
|
|
|
|
return users unless params[:without_project_bots]
|
|
|
|
|
|
|
|
users.without_project_bot
|
|
|
|
end
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def order(users)
|
|
|
|
return users unless params[:sort]
|
|
|
|
|
|
|
|
users.order_by(params[:sort])
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-05-15 09:53:12 -04:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
UsersFinder.prepend_mod_with('UsersFinder')
|