debian-mirror-gitlab/app/finders/personal_access_tokens_finder.rb

129 lines
2.7 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
class PersonalAccessTokensFinder
attr_accessor :params
2018-12-13 13:39:08 +05:30
delegate :build, :find, :find_by_id, :find_by_token, to: :execute
2017-08-17 22:00:37 +05:30
2020-10-24 23:57:45 +05:30
def initialize(params = {}, current_user = nil)
2017-08-17 22:00:37 +05:30
@params = params
2020-10-24 23:57:45 +05:30
@current_user = current_user
2017-08-17 22:00:37 +05:30
end
def execute
tokens = PersonalAccessToken.all
2020-10-24 23:57:45 +05:30
tokens = by_current_user(tokens)
2017-08-17 22:00:37 +05:30
tokens = by_user(tokens)
2021-01-29 00:20:46 +05:30
tokens = by_users(tokens)
2017-08-17 22:00:37 +05:30
tokens = by_impersonation(tokens)
2020-01-01 13:55:28 +05:30
tokens = by_state(tokens)
2022-05-07 20:08:51 +05:30
tokens = by_owner_type(tokens)
2022-11-25 23:54:43 +05:30
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)
2020-01-01 13:55:28 +05:30
sort(tokens)
2017-08-17 22:00:37 +05:30
end
private
2020-10-24 23:57:45 +05:30
attr_reader :current_user
def by_current_user(tokens)
2023-03-04 22:38:38 +05:30
return tokens if current_user.nil? || current_user.can_admin_all_resources?
2020-10-24 23:57:45 +05:30
return PersonalAccessToken.none unless Ability.allowed?(current_user, :read_user_personal_access_tokens, params[:user])
tokens
end
2022-05-07 20:08:51 +05:30
def by_owner_type(tokens)
case @params[:owner_type]
when 'human'
tokens.owner_is_human
else
tokens
end
end
2017-08-17 22:00:37 +05:30
def by_user(tokens)
return tokens unless @params[:user]
2018-03-17 18:26:18 +05:30
2020-01-01 13:55:28 +05:30
tokens.for_user(@params[:user])
end
2021-01-29 00:20:46 +05:30
def by_users(tokens)
return tokens unless @params[:users]
tokens.for_users(@params[:users])
end
2020-01-01 13:55:28 +05:30
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-08-17 22:00:37 +05:30
end
def by_impersonation(tokens)
case @params[:impersonation]
when true
tokens.with_impersonation
when false
tokens.without_impersonation
else
tokens
end
end
def by_state(tokens)
case @params[:state]
when 'active'
tokens.active
when 'inactive'
tokens.inactive
else
tokens
end
end
2022-11-25 23:54:43 +05:30
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-08-17 22:00:37 +05:30
end