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)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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
|
2020-06-18 08:09:25 -04:00
|
|
|
when 'active_or_expired'
|
|
|
|
tokens.not_revoked.expired.or(tokens.active)
|
2017-02-27 13:56:54 -05:00
|
|
|
else
|
|
|
|
tokens
|
|
|
|
end
|
2017-02-23 12:47:06 -05:00
|
|
|
end
|
|
|
|
end
|