debian-mirror-gitlab/app/finders/packages/group_packages_finder.rb

64 lines
1.9 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
module Packages
class GroupPackagesFinder
2021-03-11 19:13:27 +05:30
include ::Packages::FinderHelper
2020-07-28 23:09:34 +05:30
2021-11-18 22:05:49 +05:30
def initialize(current_user, group, params = { exclude_subgroups: false, exact_name: false, order_by: 'created_at', sort: 'asc' })
2020-07-28 23:09:34 +05:30
@current_user = current_user
@group = group
@params = params
end
def execute
return ::Packages::Package.none unless group
packages_for_group_projects
end
private
2021-03-11 19:13:27 +05:30
attr_reader :current_user, :group, :params
2021-06-08 01:23:25 +05:30
def packages_for_group_projects(installable_only: false)
2020-07-28 23:09:34 +05:30
packages = ::Packages::Package
2021-12-11 22:18:48 +05:30
.preload_pipelines
2020-11-24 15:15:51 +05:30
.including_project_route
.including_tags
2021-01-29 00:20:46 +05:30
.for_projects(group_projects_visible_to_current_user.select(:id))
2020-07-28 23:09:34 +05:30
.sort_by_attribute("#{params[:order_by]}_#{params[:sort]}")
2021-03-08 18:12:59 +05:30
packages = filter_with_version(packages)
2020-07-28 23:09:34 +05:30
packages = filter_by_package_type(packages)
2021-11-18 22:05:49 +05:30
packages = (params[:exact_name] ? filter_by_exact_package_name(packages) : filter_by_package_name(packages))
2021-06-08 01:23:25 +05:30
packages = filter_by_package_version(packages)
installable_only ? packages.installable : filter_by_status(packages)
2020-07-28 23:09:34 +05:30
end
def group_projects_visible_to_current_user
2021-01-29 00:20:46 +05:30
# according to project_policy.rb
# access to packages is ruled by:
# - project is public or the current user has access to it with at least the reporter level
# - the repository feature is available to the current_user
2022-01-26 12:08:38 +05:30
if current_user.is_a?(DeployToken)
current_user.accessible_projects
else
::Project
.in_namespace(groups)
.public_or_visible_to_user(current_user, Gitlab::Access::REPORTER)
.with_feature_available_for_user(:repository, current_user)
end
2020-07-28 23:09:34 +05:30
end
def groups
return [group] if exclude_subgroups?
group.self_and_descendants
end
def exclude_subgroups?
params[:exclude_subgroups]
end
end
end