2023-01-13 00:05:48 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Clusters
|
|
|
|
class AgentTokensFinder
|
2023-03-04 22:38:38 +05:30
|
|
|
include FinderMethods
|
|
|
|
|
|
|
|
def initialize(agent, current_user, params = {})
|
|
|
|
@agent = agent
|
2023-01-13 00:05:48 +05:30
|
|
|
@current_user = current_user
|
2023-03-04 22:38:38 +05:30
|
|
|
@params = params
|
2023-01-13 00:05:48 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2023-06-20 00:43:36 +05:30
|
|
|
return ::Clusters::AgentToken.none unless can_read_cluster_agent?
|
2023-01-13 00:05:48 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
agent.agent_tokens.then { |agent_tokens| by_status(agent_tokens) }
|
2023-01-13 00:05:48 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
attr_reader :agent, :current_user, :params
|
|
|
|
|
|
|
|
def by_status(agent_tokens)
|
|
|
|
params[:status].present? ? agent_tokens.with_status(params[:status]) : agent_tokens
|
|
|
|
end
|
2023-01-13 00:05:48 +05:30
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
def can_read_cluster_agent?
|
|
|
|
current_user&.can?(:read_cluster_agent, agent)
|
2023-01-13 00:05:48 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|