2018-11-06 07:13:33 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ClusterAncestorsFinder
|
2018-12-11 00:13:29 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2018-12-06 13:47:41 -05:00
|
|
|
def initialize(clusterable, current_user)
|
2018-11-06 07:13:33 -05:00
|
|
|
@clusterable = clusterable
|
2018-12-06 13:47:41 -05:00
|
|
|
@current_user = current_user
|
2018-11-06 07:13:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2018-12-06 13:47:41 -05:00
|
|
|
return [] unless can_read_clusters?
|
|
|
|
|
2018-11-06 07:13:33 -05:00
|
|
|
clusterable.clusters + ancestor_clusters
|
|
|
|
end
|
|
|
|
|
2018-12-11 00:13:29 -05:00
|
|
|
def has_ancestor_clusters?
|
|
|
|
ancestor_clusters.any?
|
|
|
|
end
|
|
|
|
|
2018-11-06 07:13:33 -05:00
|
|
|
private
|
|
|
|
|
2018-12-06 13:47:41 -05:00
|
|
|
attr_reader :clusterable, :current_user
|
|
|
|
|
|
|
|
def can_read_clusters?
|
|
|
|
Ability.allowed?(current_user, :read_cluster, clusterable)
|
|
|
|
end
|
2018-11-06 07:13:33 -05:00
|
|
|
|
2018-12-11 00:13:29 -05:00
|
|
|
# This unfortunately returns an Array, not a Relation!
|
2018-11-06 07:13:33 -05:00
|
|
|
def ancestor_clusters
|
2018-12-11 00:13:29 -05:00
|
|
|
strong_memoize(:ancestor_clusters) do
|
|
|
|
Clusters::Cluster.ancestor_clusters_for_clusterable(clusterable)
|
|
|
|
end
|
2018-11-06 07:13:33 -05:00
|
|
|
end
|
|
|
|
end
|