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-07-09 08:55:56 +05:30
|
|
|
agent_tokens_by_status
|
2023-01-13 00:05:48 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
attr_reader :agent, :current_user, :params
|
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
def agent_tokens_by_status
|
|
|
|
# If the `status` parameter is set to `active`, we use the `active_agent_tokens` scope
|
|
|
|
# in case this called from GraphQL's AgentTokensResolver. This prevents a repeat query
|
|
|
|
# to the database, because `active_agent_tokens` is already preloaded in the AgentsResolver
|
|
|
|
return agent.active_agent_tokens if active_tokens_only?
|
|
|
|
|
|
|
|
# Else, we use the `agent_tokens` scope combined with `with_status` if necessary
|
|
|
|
params[:status].present? ? agent.agent_tokens.with_status(params[:status]) : agent.agent_tokens
|
|
|
|
end
|
|
|
|
|
|
|
|
def active_tokens_only?
|
|
|
|
params[:status].present? && params[:status].to_sym == :active
|
2023-03-04 22:38:38 +05:30
|
|
|
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
|