summaryrefslogtreecommitdiff
path: root/app/finders/personal_access_tokens_finder.rb
blob: 7b9a2f6c0bbf3aabc918de319d45b630f16122a4 (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
class PersonalAccessTokensFinder
  attr_accessor :params

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

  def execute(token: nil, id: nil)
    tokens = by_impersonation

    return tokens.find_by_token(token) if token
    return tokens.find_by_id(id) if id

    tokens = by_state(tokens)
    tokens.order(@params[:order]) if @params[:order]

    tokens
  end

  private

  def personal_access_tokens
    @params[:user] ? @params[:user].personal_access_tokens : PersonalAccessToken.all
  end

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

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