gitlab-org--gitlab-foss/app/finders/personal_access_tokens_finder.rb
Imre Farkas 8d900ade38
Remove PersonalAccessTokensFinder#find_by method
find_by_token is overriden by TokenAuthenticatable which can be easily
missed or confused with #find_by(:token) defined by ActiveRecord. First
step for safer usage is to remove #find_by.
2018-11-05 11:36:45 +01:00

50 lines
934 B
Ruby

# frozen_string_literal: true
class PersonalAccessTokensFinder
attr_accessor :params
delegate :build, :find, :find_by_id, :find_by_token, to: :execute
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)
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
end