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

87 lines
1.9 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
2015-04-26 12:48:37 +05:30
2018-03-17 18:26:18 +05:30
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-08-17 22:00:37 +05:30
skip_before_action :authenticate_user!
2018-03-17 18:26:18 +05:30
before_action :upload_mount_satisfied?
2017-08-17 22:00:37 +05:30
before_action :find_model
before_action :authorize_access!, only: [:show]
before_action :authorize_create_access!, only: [:create]
2015-04-26 12:48:37 +05:30
2018-03-17 18:26:18 +05:30
def uploader_class
PersonalFileUploader
end
2015-04-26 12:48:37 +05:30
def find_model
2017-09-10 17:25:29 +05:30
return nil unless params[:id]
2018-03-17 18:26:18 +05:30
upload_model_class.find(params[:id])
2015-04-26 12:48:37 +05:30
end
def authorize_access!
2017-09-10 17:25:29 +05:30
return nil unless model
2015-09-11 14:41:01 +05:30
authorized =
2017-08-17 22:00:37 +05:30
case model
2015-04-26 12:48:37 +05:30
when Note
2017-08-17 22:00:37 +05:30
can?(current_user, :read_project, model.project)
when User
2015-04-26 12:48:37 +05:30
true
2017-08-17 22:00:37 +05:30
when Appearance
true
else
permission = "read_#{model.class.to_s.underscore}".to_sym
can?(current_user, permission, model)
2015-04-26 12:48:37 +05:30
end
2017-08-17 22:00:37 +05:30
render_unauthorized unless authorized
end
def authorize_create_access!
2017-09-10 17:25:29 +05:30
return nil unless model
2017-08-17 22:00:37 +05:30
# for now we support only personal snippets comments
authorized = can?(current_user, :comment_personal_snippet, model)
2015-04-26 12:48:37 +05:30
2017-08-17 22:00:37 +05:30
render_unauthorized unless authorized
end
def render_unauthorized
2015-04-26 12:48:37 +05:30
if current_user
2015-10-24 18:46:33 +05:30
render_404
2015-04-26 12:48:37 +05:30
else
authenticate_user!
end
end
2018-03-17 18:26:18 +05:30
def upload_model_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