2018-09-14 05:42:05 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-02-20 12:13:48 +00:00
|
|
|
class UploadsController < ApplicationController
|
2017-05-01 13:14:35 +00:00
|
|
|
include UploadsActions
|
2019-07-09 18:51:42 +00:00
|
|
|
include WorkhorseRequest
|
2015-03-03 02:11:50 +00:00
|
|
|
|
2018-01-29 17:57:34 +00:00
|
|
|
UnknownUploadModelError = Class.new(StandardError)
|
|
|
|
|
|
|
|
MODEL_CLASSES = {
|
|
|
|
"user" => User,
|
|
|
|
"project" => Project,
|
|
|
|
"note" => Note,
|
|
|
|
"group" => Group,
|
|
|
|
"appearance" => Appearance,
|
|
|
|
"personal_snippet" => PersonalSnippet,
|
2021-10-13 09:11:55 +00:00
|
|
|
"projects/topic" => Projects::Topic,
|
2018-01-29 17:57:34 +00:00
|
|
|
nil => PersonalSnippet
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
rescue_from UnknownUploadModelError, with: :render_404
|
|
|
|
|
2017-05-01 13:14:35 +00:00
|
|
|
skip_before_action :authenticate_user!
|
2020-09-24 15:09:51 +00:00
|
|
|
skip_before_action :check_two_factor_requirement, only: [:show]
|
2018-01-29 17:57:34 +00:00
|
|
|
before_action :upload_mount_satisfied?
|
2017-05-01 13:14:35 +00:00
|
|
|
before_action :authorize_access!, only: [:show]
|
2019-07-09 18:51:42 +00:00
|
|
|
before_action :authorize_create_access!, only: [:create, :authorize]
|
|
|
|
before_action :verify_workhorse_api!, only: [:authorize]
|
2015-02-24 03:35:42 +00:00
|
|
|
|
2020-10-05 12:08:47 +00:00
|
|
|
feature_category :not_owned
|
|
|
|
|
2020-11-20 12:09:35 +00:00
|
|
|
def self.model_classes
|
|
|
|
MODEL_CLASSES
|
|
|
|
end
|
|
|
|
|
2018-01-29 17:57:34 +00:00
|
|
|
def uploader_class
|
|
|
|
PersonalFileUploader
|
|
|
|
end
|
2015-03-03 02:11:50 +00:00
|
|
|
|
2015-03-10 13:50:42 +00:00
|
|
|
def find_model
|
2018-01-29 17:57:34 +00:00
|
|
|
upload_model_class.find(params[:id])
|
2015-03-10 13:50:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_access!
|
2015-04-16 12:03:37 +00:00
|
|
|
authorized =
|
2017-05-01 13:14:35 +00:00
|
|
|
case model
|
2015-03-10 13:50:42 +00:00
|
|
|
when Note
|
2017-05-01 13:14:35 +00:00
|
|
|
can?(current_user, :read_project, model.project)
|
2020-01-23 12:08:38 +00:00
|
|
|
when Snippet, ProjectSnippet
|
|
|
|
can?(current_user, :read_snippet, model)
|
2017-05-01 13:14:35 +00:00
|
|
|
when User
|
2019-06-04 23:50:57 +00:00
|
|
|
# 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.
|
|
|
|
!secret? || can?(current_user, :update_user, model)
|
2017-05-19 09:20:51 +00:00
|
|
|
when Appearance
|
|
|
|
true
|
2021-10-13 09:11:55 +00:00
|
|
|
when Projects::Topic
|
|
|
|
true
|
2017-05-01 13:14:35 +00:00
|
|
|
else
|
2019-05-11 12:06:44 +00:00
|
|
|
permission = "read_#{model.class.underscore}".to_sym
|
2017-05-01 13:14:35 +00:00
|
|
|
|
|
|
|
can?(current_user, permission, model)
|
2015-03-10 13:50:42 +00:00
|
|
|
end
|
|
|
|
|
2017-05-01 13:14:35 +00:00
|
|
|
render_unauthorized unless authorized
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_create_access!
|
2019-06-04 23:50:57 +00:00
|
|
|
authorized =
|
|
|
|
case model
|
|
|
|
when User
|
|
|
|
can?(current_user, :update_user, model)
|
|
|
|
else
|
|
|
|
can?(current_user, :create_note, model)
|
|
|
|
end
|
2015-03-10 13:50:42 +00:00
|
|
|
|
2017-05-01 13:14:35 +00:00
|
|
|
render_unauthorized unless authorized
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_unauthorized
|
2019-07-09 18:51:42 +00:00
|
|
|
if current_user || workhorse_authorize_request?
|
2015-10-09 17:07:29 +00:00
|
|
|
render_404
|
2015-03-10 13:50:42 +00:00
|
|
|
else
|
|
|
|
authenticate_user!
|
2015-02-24 03:35:42 +00:00
|
|
|
end
|
|
|
|
end
|
2015-03-03 02:11:50 +00:00
|
|
|
|
2019-10-14 15:06:07 +00:00
|
|
|
def cache_settings
|
|
|
|
case model
|
2021-10-13 09:11:55 +00:00
|
|
|
when User, Appearance, Projects::Topic
|
2019-10-14 15:06:07 +00:00
|
|
|
[5.minutes, { public: true, must_revalidate: false }]
|
|
|
|
when Project, Group
|
|
|
|
[5.minutes, { private: true, must_revalidate: true }]
|
|
|
|
end
|
2019-01-21 21:25:54 +00:00
|
|
|
end
|
|
|
|
|
2019-06-04 23:50:57 +00:00
|
|
|
def secret?
|
|
|
|
params[:secret].present?
|
|
|
|
end
|
|
|
|
|
2018-01-29 17:57:34 +00:00
|
|
|
def upload_model_class
|
2020-11-20 12:09:35 +00:00
|
|
|
self.class.model_classes[params[:model]] || raise(UnknownUploadModelError)
|
2015-03-03 02:11:50 +00:00
|
|
|
end
|
2017-05-01 13:14:35 +00:00
|
|
|
|
2018-01-29 17:57:34 +00:00
|
|
|
def upload_model_class_has_mounts?
|
|
|
|
upload_model_class < CarrierWave::Mount::Extension
|
2017-05-01 13:14:35 +00:00
|
|
|
end
|
|
|
|
|
2018-01-29 17:57:34 +00:00
|
|
|
def upload_mount_satisfied?
|
|
|
|
return true unless upload_model_class_has_mounts?
|
2017-05-01 13:14:35 +00:00
|
|
|
|
2018-01-29 17:57:34 +00:00
|
|
|
upload_model_class.uploader_options.has_key?(upload_mount)
|
2017-05-01 13:14:35 +00:00
|
|
|
end
|
2015-02-20 12:13:48 +00:00
|
|
|
end
|
2020-11-20 12:09:35 +00:00
|
|
|
|
2021-05-11 21:10:21 +00:00
|
|
|
UploadsController.prepend_mod_with('UploadsController')
|