debian-mirror-gitlab/app/finders/users_finder.rb

125 lines
2.6 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
# 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-22 19:07:51 +05:30
# without_projects: boolean
2020-06-23 00:09:42 +05:30
# sort: string
# id: integer
2017-09-10 17:25:29 +05:30
#
class UsersFinder
include CreatedAtFilter
2018-03-17 18:26:18 +05:30
include CustomAttributesFilter
2017-09-10 17:25:29 +05:30
attr_accessor :current_user, :params
def initialize(current_user, params = {})
@current_user = current_user
@params = params
end
def execute
2018-03-17 18:26:18 +05:30
users = User.all.order_id_desc
2017-09-10 17:25:29 +05:30
users = by_username(users)
2020-06-23 00:09:42 +05:30
users = by_id(users)
2017-09-10 17:25:29 +05:30
users = by_search(users)
users = by_blocked(users)
users = by_active(users)
users = by_external_identity(users)
users = by_external(users)
2018-10-15 14:42:47 +05:30
users = by_2fa(users)
2017-09-10 17:25:29 +05:30
users = by_created_at(users)
2020-04-22 19:07:51 +05:30
users = by_without_projects(users)
2018-03-17 18:26:18 +05:30
users = by_custom_attributes(users)
2017-09-10 17:25:29 +05:30
2020-06-23 00:09:42 +05:30
order(users)
2017-09-10 17:25:29 +05:30
end
private
def by_username(users)
return users unless params[:username]
2018-12-13 13:39:08 +05:30
users.by_username(params[:username])
2017-09-10 17:25:29 +05:30
end
2020-06-23 00:09:42 +05:30
def by_id(users)
return users unless params[:id]
users.id_in(params[:id])
end
2017-09-10 17:25:29 +05:30
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-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
def by_external_identity(users)
return users unless current_user&.admin? && params[:extern_uid] && params[:provider]
users.joins(:identities).merge(Identity.with_extern_uid(params[:provider], params[:extern_uid]))
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
def by_external(users)
return users = users.where.not(external: true) unless current_user&.admin?
return users unless params[:external]
users.external
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-10-15 14:42:47 +05:30
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-22 19:07:51 +05:30
def by_without_projects(users)
return users unless params[:without_projects]
users.without_projects
end
2020-06-23 00:09:42 +05:30
# rubocop: disable CodeReuse/ActiveRecord
def order(users)
return users unless params[:sort]
users.order_by(params[:sort])
end
# rubocop: enable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
end
2019-12-04 20:38:33 +05:30
UsersFinder.prepend_if_ee('EE::UsersFinder')