diff options
Diffstat (limited to 'app/models/ci/catalog/listing.rb')
-rw-r--r-- | app/models/ci/catalog/listing.rb | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/app/models/ci/catalog/listing.rb b/app/models/ci/catalog/listing.rb index 51bd85016a5..2c96dda988e 100644 --- a/app/models/ci/catalog/listing.rb +++ b/app/models/ci/catalog/listing.rb @@ -2,19 +2,17 @@ module Ci module Catalog + # This class is the SSoT to displaying the list of resources in the CI/CD Catalog. class Listing - # This class is the SSoT to displaying the list of resources in the CI/CD Catalog. - # This model is not directly backed by a table and joins catalog resources - # with projects to return relevant data. - MIN_SEARCH_LENGTH = 3 def initialize(current_user) @current_user = current_user end - def resources(namespace: nil, sort: nil, search: nil) - relation = all_resources + def resources(namespace: nil, sort: nil, search: nil, scope: :all) + relation = Ci::Catalog::Resource.published.includes(:project) + relation = by_scope(relation, scope) relation = by_namespace(relation, namespace) relation = by_search(relation, search) @@ -29,20 +27,25 @@ module Ci end end - private + def find_resource(id: nil, full_path: nil) + resource = id ? Ci::Catalog::Resource.find_by_id(id) : Project.find_by_full_path(full_path)&.catalog_resource - attr_reader :current_user + return unless resource.present? + return unless resource.published? + return unless Ability.allowed?(current_user, :read_code, resource.project) - def all_resources - Ci::Catalog::Resource.joins(:project).includes(:project) - .merge(Project.public_or_visible_to_user(current_user)) + resource end + private + + attr_reader :current_user + def by_namespace(relation, namespace) return relation unless namespace raise ArgumentError, 'Namespace is not a root namespace' unless namespace.root? - relation.merge(Project.in_namespace(namespace.self_and_descendant_ids)) + relation.joins(:project).merge(Project.in_namespace(namespace.self_and_descendant_ids)) end def by_search(relation, search) @@ -51,6 +54,14 @@ module Ci relation.search(search) end + + def by_scope(relation, scope) + if scope == :namespaces && Feature.enabled?(:ci_guard_for_catalog_resource_scope, current_user) + relation.visible_to_user(current_user) + else + relation.public_or_visible_to_user(current_user) + end + end end end end |