debian-mirror-gitlab/app/controllers/uploads_controller.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

125 lines
3.2 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
class UploadsController < ApplicationController
2017-08-17 22:00:37 +05:30
include UploadsActions
2019-09-04 21:01:54 +05:30
include WorkhorseRequest
2015-04-26 12:48:37 +05:30
2018-03-17 18:26:18 +05:30
UnknownUploadModelError = Class.new(StandardError)
MODEL_CLASSES = {
2022-08-27 11:52:29 +05:30
"user" => User,
"project" => Project,
"note" => Note,
"group" => Group,
"appearance" => Appearance,
2018-03-17 18:26:18 +05:30
"personal_snippet" => PersonalSnippet,
2022-08-27 11:52:29 +05:30
"projects/topic" => Projects::Topic,
2022-06-21 17:19:12 +05:30
'alert_management_metric_image' => ::AlertManagement::MetricImage,
2023-03-17 16:20:25 +05:30
"achievements/achievement" => Achievements::Achievement,
2022-06-21 17:19:12 +05:30
nil => PersonalSnippet
2018-03-17 18:26:18 +05:30
}.freeze
rescue_from UnknownUploadModelError, with: :render_404
2017-08-17 22:00:37 +05:30
skip_before_action :authenticate_user!
2020-11-24 15:15:51 +05:30
skip_before_action :check_two_factor_requirement, only: [:show]
2018-03-17 18:26:18 +05:30
before_action :upload_mount_satisfied?
2017-08-17 22:00:37 +05:30
before_action :authorize_access!, only: [:show]
2019-09-04 21:01:54 +05:30
before_action :authorize_create_access!, only: [:create, :authorize]
before_action :verify_workhorse_api!, only: [:authorize]
2015-04-26 12:48:37 +05:30
2022-06-21 17:19:12 +05:30
feature_category :not_owned # rubocop:todo Gitlab/AvoidFeatureCategoryNotOwned
2021-01-03 14:25:43 +05:30
2021-02-22 17:27:13 +05:30
def self.model_classes
MODEL_CLASSES
end
2018-03-17 18:26:18 +05:30
def uploader_class
PersonalFileUploader
end
2015-04-26 12:48:37 +05:30
def find_model
2018-03-17 18:26:18 +05:30
upload_model_class.find(params[:id])
2015-04-26 12:48:37 +05:30
end
2022-05-07 20:08:51 +05:30
def authorized?
case model
when Note
can?(current_user, :read_project, model.project)
when Snippet, ProjectSnippet
can?(current_user, :read_snippet, model)
when User
# We validate the current user has enough (writing)
# access to itself when a secret is given.
# For instance, user avatars are readable by anyone,
# while temporary, user snippet uploads are not.
2023-01-10 11:22:00 +05:30
return false if !current_user && public_visibility_restricted?
2022-05-07 20:08:51 +05:30
!secret? || can?(current_user, :update_user, model)
when Appearance
true
when Projects::Topic
true
2022-06-21 17:19:12 +05:30
when ::AlertManagement::MetricImage
can?(current_user, :read_alert_management_metric_image, model.alert)
2023-03-17 16:20:25 +05:30
when ::Achievements::Achievement
true
2022-05-07 20:08:51 +05:30
else
can?(current_user, "read_#{model.class.underscore}".to_sym, model)
end
end
2015-04-26 12:48:37 +05:30
2022-05-07 20:08:51 +05:30
def authorize_access!
render_unauthorized unless authorized?
2017-08-17 22:00:37 +05:30
end
def authorize_create_access!
2019-07-07 11:18:12 +05:30
authorized =
case model
when User
can?(current_user, :update_user, model)
else
2019-07-31 22:56:46 +05:30
can?(current_user, :create_note, model)
2019-07-07 11:18:12 +05:30
end
2015-04-26 12:48:37 +05:30
2017-08-17 22:00:37 +05:30
render_unauthorized unless authorized
end
def render_unauthorized
2019-09-04 21:01:54 +05:30
if current_user || workhorse_authorize_request?
2015-10-24 18:46:33 +05:30
render_404
2015-04-26 12:48:37 +05:30
else
authenticate_user!
end
end
2019-12-21 20:55:43 +05:30
def cache_settings
case model
2023-03-17 16:20:25 +05:30
when User, Appearance, Projects::Topic, Achievements::Achievement
2019-12-21 20:55:43 +05:30
[5.minutes, { public: true, must_revalidate: false }]
when Project, Group
[5.minutes, { private: true, must_revalidate: true }]
end
2019-03-02 22:35:43 +05:30
end
2019-07-07 11:18:12 +05:30
def secret?
params[:secret].present?
end
2018-03-17 18:26:18 +05:30
def upload_model_class
2021-02-22 17:27:13 +05:30
self.class.model_classes[params[:model]] || raise(UnknownUploadModelError)
2015-04-26 12:48:37 +05:30
end
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
def upload_model_class_has_mounts?
upload_model_class < CarrierWave::Mount::Extension
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
def upload_mount_satisfied?
return true unless upload_model_class_has_mounts?
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
upload_model_class.uploader_options.has_key?(upload_mount)
2017-08-17 22:00:37 +05:30
end
2015-04-26 12:48:37 +05:30
end
2021-02-22 17:27:13 +05:30
2021-06-08 01:23:25 +05:30
UploadsController.prepend_mod_with('UploadsController')