gitlab-org--gitlab-foss/app/finders/personal_access_tokens_finder.rb

51 lines
931 B
Ruby
Raw Normal View History

# frozen_string_literal: true
class PersonalAccessTokensFinder
2017-02-27 13:56:54 -05:00
attr_accessor :params
delegate :build, :find, :find_by, :find_by_token, to: :execute
2017-02-27 13:56:54 -05:00
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)
2017-02-27 13:56:54 -05:00
case @params[:impersonation]
when true
tokens.with_impersonation
2017-02-27 13:56:54 -05:00
when false
tokens.without_impersonation
2017-02-27 13:56:54 -05:00
else
tokens
2017-02-27 13:56:54 -05:00
end
end
2017-02-27 13:56:54 -05:00
def by_state(tokens)
case @params[:state]
when 'active'
tokens.active
when 'inactive'
tokens.inactive
else
tokens
end
end
end