2018-09-11 15:08:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-23 12:47:06 -05:00
|
|
|
class PersonalAccessTokensFinder
|
2017-02-27 13:56:54 -05:00
|
|
|
attr_accessor :params
|
|
|
|
|
2018-10-26 10:47:03 -04:00
|
|
|
delegate :build, :find, :find_by_id, :find_by_token, to: :execute
|
2017-03-01 11:59:03 -05:00
|
|
|
|
2017-02-27 13:56:54 -05:00
|
|
|
def initialize(params = {})
|
2017-02-23 12:47:06 -05:00
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
2017-03-01 11:59:03 -05:00
|
|
|
def execute
|
|
|
|
tokens = PersonalAccessToken.all
|
|
|
|
tokens = by_user(tokens)
|
|
|
|
tokens = by_impersonation(tokens)
|
|
|
|
by_state(tokens)
|
2017-02-23 12:47:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-03-01 11:59:03 -05:00
|
|
|
def by_user(tokens)
|
|
|
|
return tokens unless @params[:user]
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2017-03-01 11:59:03 -05:00
|
|
|
tokens.where(user: @params[:user])
|
2017-02-23 12:47:06 -05:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-02-23 12:47:06 -05:00
|
|
|
|
2017-03-01 11:59:03 -05:00
|
|
|
def by_impersonation(tokens)
|
2017-02-27 13:56:54 -05:00
|
|
|
case @params[:impersonation]
|
|
|
|
when true
|
2017-03-01 11:59:03 -05:00
|
|
|
tokens.with_impersonation
|
2017-02-27 13:56:54 -05:00
|
|
|
when false
|
2017-03-01 11:59:03 -05:00
|
|
|
tokens.without_impersonation
|
2017-02-27 13:56:54 -05:00
|
|
|
else
|
2017-03-01 11:59:03 -05:00
|
|
|
tokens
|
2017-02-27 13:56:54 -05:00
|
|
|
end
|
2017-02-23 12:47:06 -05:00
|
|
|
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
|
2017-02-23 12:47:06 -05:00
|
|
|
end
|
|
|
|
end
|