2018-09-14 01:42:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-02-20 07:13:48 -05:00
|
|
|
class UploadsController < ApplicationController
|
2017-05-01 09:14:35 -04:00
|
|
|
include UploadsActions
|
2015-03-02 21:11:50 -05:00
|
|
|
|
2018-01-29 12:57:34 -05:00
|
|
|
UnknownUploadModelError = Class.new(StandardError)
|
|
|
|
|
|
|
|
MODEL_CLASSES = {
|
|
|
|
"user" => User,
|
|
|
|
"project" => Project,
|
|
|
|
"note" => Note,
|
|
|
|
"group" => Group,
|
|
|
|
"appearance" => Appearance,
|
|
|
|
"personal_snippet" => PersonalSnippet,
|
|
|
|
nil => PersonalSnippet
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
rescue_from UnknownUploadModelError, with: :render_404
|
|
|
|
|
2017-05-01 09:14:35 -04:00
|
|
|
skip_before_action :authenticate_user!
|
2018-01-29 12:57:34 -05:00
|
|
|
before_action :upload_mount_satisfied?
|
2017-05-01 09:14:35 -04:00
|
|
|
before_action :find_model
|
|
|
|
before_action :authorize_access!, only: [:show]
|
|
|
|
before_action :authorize_create_access!, only: [:create]
|
2015-02-23 22:35:42 -05:00
|
|
|
|
2018-01-29 12:57:34 -05:00
|
|
|
def uploader_class
|
|
|
|
PersonalFileUploader
|
|
|
|
end
|
2015-03-02 21:11:50 -05:00
|
|
|
|
2015-03-10 09:50:42 -04:00
|
|
|
def find_model
|
2019-02-08 07:19:53 -05:00
|
|
|
return unless params[:id]
|
2017-05-03 11:26:49 -04:00
|
|
|
|
2018-01-29 12:57:34 -05:00
|
|
|
upload_model_class.find(params[:id])
|
2015-03-10 09:50:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_access!
|
2019-02-08 07:19:53 -05:00
|
|
|
return unless model
|
2017-05-29 03:54:35 -04:00
|
|
|
|
2015-04-16 08:03:37 -04:00
|
|
|
authorized =
|
2017-05-01 09:14:35 -04:00
|
|
|
case model
|
2015-03-10 09:50:42 -04:00
|
|
|
when Note
|
2017-05-01 09:14:35 -04:00
|
|
|
can?(current_user, :read_project, model.project)
|
|
|
|
when User
|
2019-06-04 19:50:57 -04: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 05:20:51 -04:00
|
|
|
when Appearance
|
|
|
|
true
|
2017-05-01 09:14:35 -04:00
|
|
|
else
|
2019-05-11 08:06:44 -04:00
|
|
|
permission = "read_#{model.class.underscore}".to_sym
|
2017-05-01 09:14:35 -04:00
|
|
|
|
|
|
|
can?(current_user, permission, model)
|
2015-03-10 09:50:42 -04:00
|
|
|
end
|
|
|
|
|
2017-05-01 09:14:35 -04:00
|
|
|
render_unauthorized unless authorized
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_create_access!
|
2019-02-08 07:19:53 -05:00
|
|
|
return unless model
|
2017-05-03 11:26:49 -04:00
|
|
|
|
2019-06-04 19:50:57 -04:00
|
|
|
authorized =
|
|
|
|
case model
|
|
|
|
when User
|
|
|
|
can?(current_user, :update_user, model)
|
|
|
|
else
|
|
|
|
can?(current_user, :create_note, model)
|
|
|
|
end
|
2015-03-10 09:50:42 -04:00
|
|
|
|
2017-05-01 09:14:35 -04:00
|
|
|
render_unauthorized unless authorized
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_unauthorized
|
2015-03-10 09:50:42 -04:00
|
|
|
if current_user
|
2015-10-09 13:07:29 -04:00
|
|
|
render_404
|
2015-03-10 09:50:42 -04:00
|
|
|
else
|
|
|
|
authenticate_user!
|
2015-02-23 22:35:42 -05:00
|
|
|
end
|
|
|
|
end
|
2015-03-02 21:11:50 -05:00
|
|
|
|
2019-01-22 05:09:04 -05:00
|
|
|
def cache_publicly?
|
|
|
|
User === model || Appearance === model
|
2019-01-21 16:25:54 -05:00
|
|
|
end
|
|
|
|
|
2019-06-04 19:50:57 -04:00
|
|
|
def secret?
|
|
|
|
params[:secret].present?
|
|
|
|
end
|
|
|
|
|
2018-01-29 12:57:34 -05:00
|
|
|
def upload_model_class
|
|
|
|
MODEL_CLASSES[params[:model]] || raise(UnknownUploadModelError)
|
2015-03-02 21:11:50 -05:00
|
|
|
end
|
2017-05-01 09:14:35 -04:00
|
|
|
|
2018-01-29 12:57:34 -05:00
|
|
|
def upload_model_class_has_mounts?
|
|
|
|
upload_model_class < CarrierWave::Mount::Extension
|
2017-05-01 09:14:35 -04:00
|
|
|
end
|
|
|
|
|
2018-01-29 12:57:34 -05:00
|
|
|
def upload_mount_satisfied?
|
|
|
|
return true unless upload_model_class_has_mounts?
|
2017-05-01 09:14:35 -04:00
|
|
|
|
2018-01-29 12:57:34 -05:00
|
|
|
upload_model_class.uploader_options.has_key?(upload_mount)
|
2017-05-01 09:14:35 -04:00
|
|
|
end
|
2015-02-20 07:13:48 -05:00
|
|
|
end
|