BigW Consortium Gitlab

users_finder.rb 1.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# 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
17
  include CreatedAtFilter
18
  include CustomAttributesFilter
James Lopez committed
19

20 21 22 23 24 25 26 27 28 29 30 31 32
  attr_accessor :current_user, :params

  def initialize(current_user, params = {})
    @current_user = current_user
    @params = params
  end

  def execute
    users = User.all
    users = by_username(users)
    users = by_search(users)
    users = by_blocked(users)
    users = by_active(users)
33 34
    users = by_external_identity(users)
    users = by_external(users)
35
    users = by_created_at(users)
36
    users = by_custom_attributes(users)
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

    users
  end

  private

  def by_username(users)
    return users unless params[:username]

    users.where(username: params[:username])
  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

  def by_external_identity(users)
68
    return users unless current_user&.admin? && params[:extern_uid] && params[:provider]
69 70 71 72 73

    users.joins(:identities).merge(Identity.with_extern_uid(params[:provider], params[:extern_uid]))
  end

  def by_external(users)
74
    return users = users.where.not(external: true) unless current_user&.admin?
75 76 77 78 79
    return users unless params[:external]

    users.external
  end
end