Module: API::Helpers::PackagesHelpers
- Extended by:
- Gitlab::Utils::Override
- Includes:
- Gitlab::Utils::StrongMemoize
- Included in:
- API::Helpers::Packages::Npm, PackagesManagerClientsHelpers
- Defined in:
- lib/api/helpers/packages_helpers.rb
Constant Summary collapse
- MAX_PACKAGE_FILE_SIZE =
50.megabytes.freeze
- ALLOWED_REQUIRED_PERMISSIONS =
%i[read_package read_group].freeze
Instance Method Summary collapse
- #authorize_create_package!(subject = user_project) ⇒ Object
- #authorize_destroy_package!(subject = user_project) ⇒ Object
- #authorize_packages_access!(subject = user_project, required_permission = :read_package) ⇒ Object
- #authorize_read_package!(subject = user_project) ⇒ Object
- #authorize_upload!(subject = user_project) ⇒ Object
- #authorize_workhorse!(subject: user_project, has_length: true, maximum_size: MAX_PACKAGE_FILE_SIZE) ⇒ Object
- #present_package_file!(package_file, supports_direct_download: true) ⇒ Object
- #require_dependency_proxy_enabled! ⇒ Object
- #require_packages_enabled! ⇒ Object
- #track_package_event(action, scope, **args) ⇒ Object
- #user_project(action: :read_project) ⇒ Object
-
#user_project_with_read_package ⇒ Object
This function is similar to the ‘find_project!` function, but it considers the `read_package` ability.
Methods included from Gitlab::Utils::Override
extended, extensions, included, method_added, override, prepended, queue_verification, verify!
Instance Method Details
#authorize_create_package!(subject = user_project) ⇒ Object
24 25 26 |
# File 'lib/api/helpers/packages_helpers.rb', line 24 def (subject = user_project) (:create_package, subject) end |
#authorize_destroy_package!(subject = user_project) ⇒ Object
28 29 30 |
# File 'lib/api/helpers/packages_helpers.rb', line 28 def (subject = user_project) (:destroy_package, subject) end |
#authorize_packages_access!(subject = user_project, required_permission = :read_package) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/api/helpers/packages_helpers.rb', line 32 def (subject = user_project, = :read_package) require_packages_enabled! return forbidden! unless .in?(ALLOWED_REQUIRED_PERMISSIONS) if == :read_package (subject) else (, subject) end end |
#authorize_read_package!(subject = user_project) ⇒ Object
20 21 22 |
# File 'lib/api/helpers/packages_helpers.rb', line 20 def (subject = user_project) (:read_package, subject.try(:packages_policy_subject) || subject) end |
#authorize_upload!(subject = user_project) ⇒ Object
56 57 58 59 |
# File 'lib/api/helpers/packages_helpers.rb', line 56 def (subject = user_project) (subject) require_gitlab_workhorse! end |
#authorize_workhorse!(subject: user_project, has_length: true, maximum_size: MAX_PACKAGE_FILE_SIZE) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/api/helpers/packages_helpers.rb', line 43 def (subject: user_project, has_length: true, maximum_size: MAX_PACKAGE_FILE_SIZE) (subject) Gitlab::Workhorse.verify_api_request!(headers) status 200 content_type Gitlab::Workhorse::INTERNAL_API_CONTENT_TYPE params = { has_length: has_length } params[:maximum_size] = maximum_size unless has_length ::Packages::PackageFileUploader.(**params) end |
#present_package_file!(package_file, supports_direct_download: true) ⇒ Object
111 112 113 114 |
# File 'lib/api/helpers/packages_helpers.rb', line 111 def present_package_file!(package_file, supports_direct_download: true) package_file.package.touch_last_downloaded_at present_carrierwave_file!(package_file.file, supports_direct_download: supports_direct_download) end |
#require_dependency_proxy_enabled! ⇒ Object
16 17 18 |
# File 'lib/api/helpers/packages_helpers.rb', line 16 def require_dependency_proxy_enabled! not_found! unless ::Gitlab.config.dependency_proxy.enabled end |
#require_packages_enabled! ⇒ Object
12 13 14 |
# File 'lib/api/helpers/packages_helpers.rb', line 12 def require_packages_enabled! not_found! unless ::Gitlab.config.packages.enabled end |
#track_package_event(action, scope, **args) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/api/helpers/packages_helpers.rb', line 88 def track_package_event(action, scope, **args) service = ::Packages::CreateEventService.new(nil, current_user, event_name: action, scope: scope) service.execute category = args.delete(:category) || self.[:for].name args[:user] = current_user if current_user event_name = "i_package_#{scope}_user" ::Gitlab::Tracking.event( category, action.to_s, property: event_name, label: 'redis_hll_counters.user_packages.user_packages_total_unique_counts_monthly', context: [Gitlab::Tracking::ServicePingContext.new(data_source: :redis_hll, event: event_name).to_context], **args ) if action.to_s == 'push_package' && service.originator_type == :deploy_token track_snowplow_event("push_package_by_deploy_token", category, args) elsif action.to_s == 'pull_package' && service.originator_type == :guest track_snowplow_event("pull_package_by_guest", category, args) end end |
#user_project(action: :read_project) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/api/helpers/packages_helpers.rb', line 62 def user_project(action: :read_project) case action when :read_project super() when :read_package user_project_with_read_package else raise ArgumentError, "unexpected action: #{action}" end end |
#user_project_with_read_package ⇒ Object
This function is similar to the ‘find_project!` function, but it considers the `read_package` ability.
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/api/helpers/packages_helpers.rb', line 74 def user_project_with_read_package project = find_project(params[:id]) return forbidden! unless (project) return project if can?(current_user, :read_package, project&.packages_policy_subject) # guest users can have :read_project but not :read_package return forbidden! if can?(current_user, :read_project, project) return if authenticate_non_public? not_found!('Project') end |