BigW Consortium Gitlab

personal_access_tokens_finder.rb 796 Bytes
Newer Older
1
class PersonalAccessTokensFinder
2 3
  attr_accessor :params

4 5
  delegate :build, :find, :find_by, to: :execute

6
  def initialize(params = {})
7 8 9
    @params = params
  end

10 11 12 13 14
  def execute
    tokens = PersonalAccessToken.all
    tokens = by_user(tokens)
    tokens = by_impersonation(tokens)
    by_state(tokens)
15 16 17 18
  end

  private

19 20 21
  def by_user(tokens)
    return tokens unless @params[:user]
    tokens.where(user: @params[:user])
22 23
  end

24
  def by_impersonation(tokens)
25 26
    case @params[:impersonation]
    when true
27
      tokens.with_impersonation
28
    when false
29
      tokens.without_impersonation
30
    else
31
      tokens
32
    end
33 34
  end

35 36 37 38 39 40 41 42 43
  def by_state(tokens)
    case @params[:state]
    when 'active'
      tokens.active
    when 'inactive'
      tokens.inactive
    else
      tokens
    end
44 45
  end
end