debian-mirror-gitlab/app/finders/clusters/agent_tokens_finder.rb
2023-06-20 00:43:36 +05:30

31 lines
707 B
Ruby

# frozen_string_literal: true
module Clusters
class AgentTokensFinder
include FinderMethods
def initialize(agent, current_user, params = {})
@agent = agent
@current_user = current_user
@params = params
end
def execute
return ::Clusters::AgentToken.none unless can_read_cluster_agent?
agent.agent_tokens.then { |agent_tokens| by_status(agent_tokens) }
end
private
attr_reader :agent, :current_user, :params
def by_status(agent_tokens)
params[:status].present? ? agent_tokens.with_status(params[:status]) : agent_tokens
end
def can_read_cluster_agent?
current_user&.can?(:read_cluster_agent, agent)
end
end
end