summaryrefslogtreecommitdiff
path: root/app/finders/personal_access_tokens_finder.rb
blob: 81fd3b7a5475e72fc84a7fc30426308c4ccfbfd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# frozen_string_literal: true

class PersonalAccessTokensFinder
  attr_accessor :params

  delegate :build, :find, :find_by, :find_by_token, to: :execute

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

  def execute
    tokens = PersonalAccessToken.all
    tokens = by_user(tokens)
    tokens = by_impersonation(tokens)
    by_state(tokens)
  end

  private

  # rubocop: disable CodeReuse/ActiveRecord
  def by_user(tokens)
    return tokens unless @params[:user]

    tokens.where(user: @params[:user])
  end
  # rubocop: enable CodeReuse/ActiveRecord

  def by_impersonation(tokens)
    case @params[:impersonation]
    when true
      tokens.with_impersonation
    when false
      tokens.without_impersonation
    else
      tokens
    end
  end

  def by_state(tokens)
    case @params[:state]
    when 'active'
      tokens.active
    when 'inactive'
      tokens.inactive
    else
      tokens
    end
  end
end