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
|
|
|
|
2020-08-06 17:10:15 -04:00
|
|
|
def initialize(params = {}, current_user = nil)
|
2017-02-23 12:47:06 -05:00
|
|
|
@params = params
|
2020-08-06 17:10:15 -04:00
|
|
|
@current_user = current_user
|
2017-02-23 12:47:06 -05:00
|
|
|
end
|
|
|
|
|
2017-03-01 11:59:03 -05:00
|
|
|
def execute
|
|
|
|
tokens = PersonalAccessToken.all
|
2020-08-06 17:10:15 -04:00
|
|
|
tokens = by_current_user(tokens)
|
2017-03-01 11:59:03 -05:00
|
|
|
tokens = by_user(tokens)
|
2020-11-16 16:09:02 -05:00
|
|
|
tokens = by_users(tokens)
|
2017-03-01 11:59:03 -05:00
|
|
|
tokens = by_impersonation(tokens)
|
2019-12-17 04:07:48 -05:00
|
|
|
tokens = by_state(tokens)
|
2022-03-07 01:15:33 -05:00
|
|
|
tokens = by_owner_type(tokens)
|
2022-09-20 05:10:52 -04:00
|
|
|
tokens = by_revoked_state(tokens)
|
|
|
|
tokens = by_created_before(tokens)
|
|
|
|
tokens = by_created_after(tokens)
|
|
|
|
tokens = by_last_used_before(tokens)
|
|
|
|
tokens = by_last_used_after(tokens)
|
|
|
|
tokens = by_search(tokens)
|
2019-12-17 04:07:48 -05:00
|
|
|
|
|
|
|
sort(tokens)
|
2017-02-23 12:47:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-08-06 17:10:15 -04:00
|
|
|
attr_reader :current_user
|
|
|
|
|
|
|
|
def by_current_user(tokens)
|
|
|
|
return tokens if current_user.nil? || current_user.admin?
|
|
|
|
return PersonalAccessToken.none unless Ability.allowed?(current_user, :read_user_personal_access_tokens, params[:user])
|
|
|
|
|
|
|
|
tokens
|
|
|
|
end
|
|
|
|
|
2022-03-07 01:15:33 -05:00
|
|
|
def by_owner_type(tokens)
|
|
|
|
case @params[:owner_type]
|
|
|
|
when 'human'
|
|
|
|
tokens.owner_is_human
|
|
|
|
else
|
|
|
|
tokens
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
2019-12-17 04:07:48 -05:00
|
|
|
tokens.for_user(@params[:user])
|
|
|
|
end
|
|
|
|
|
2020-11-16 16:09:02 -05:00
|
|
|
def by_users(tokens)
|
|
|
|
return tokens unless @params[:users]
|
|
|
|
|
|
|
|
tokens.for_users(@params[:users])
|
|
|
|
end
|
|
|
|
|
2019-12-17 04:07:48 -05:00
|
|
|
def sort(tokens)
|
|
|
|
available_sort_orders = PersonalAccessToken.simple_sorts.keys
|
|
|
|
|
|
|
|
return tokens unless available_sort_orders.include?(params[:sort])
|
|
|
|
|
|
|
|
tokens.order_by(params[:sort])
|
2017-02-23 12:47:06 -05:00
|
|
|
end
|
|
|
|
|
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
|
2022-09-20 05:10:52 -04:00
|
|
|
|
|
|
|
def by_revoked_state(tokens)
|
|
|
|
return tokens unless params.has_key?(:revoked)
|
|
|
|
|
|
|
|
params[:revoked] ? tokens.revoked : tokens.not_revoked
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_created_before(tokens)
|
|
|
|
return tokens unless params[:created_before]
|
|
|
|
|
|
|
|
tokens.created_before(params[:created_before])
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_created_after(tokens)
|
|
|
|
return tokens unless params[:created_after]
|
|
|
|
|
|
|
|
tokens.created_after(params[:created_after])
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_last_used_before(tokens)
|
|
|
|
return tokens unless params[:last_used_before]
|
|
|
|
|
|
|
|
tokens.last_used_before(params[:last_used_before])
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_last_used_after(tokens)
|
|
|
|
return tokens unless params[:last_used_after]
|
|
|
|
|
|
|
|
tokens.last_used_after(params[:last_used_after])
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_search(tokens)
|
|
|
|
return tokens unless params[:search]
|
|
|
|
|
|
|
|
tokens.search(params[:search])
|
|
|
|
end
|
2017-02-23 12:47:06 -05:00
|
|
|
end
|