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
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
skip_before_action :authenticate_user!
|
|
|
|
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
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def find_model
|
2017-09-10 17:25:29 +05:30
|
|
|
return nil unless params[:id]
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
return render_404 unless upload_model && upload_mount
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
@model = upload_model.find(params[:id])
|
|
|
|
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
|
|
|
|
|
|
|
|
def upload_model
|
|
|
|
upload_models = {
|
2015-09-11 14:41:01 +05:30
|
|
|
"user" => User,
|
|
|
|
"project" => Project,
|
|
|
|
"note" => Note,
|
2016-04-02 18:10:28 +05:30
|
|
|
"group" => Group,
|
2017-08-17 22:00:37 +05:30
|
|
|
"appearance" => Appearance,
|
|
|
|
"personal_snippet" => PersonalSnippet
|
2015-04-26 12:48:37 +05:30
|
|
|
}
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
upload_models[params[:model]]
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def upload_mount
|
2017-08-17 22:00:37 +05:30
|
|
|
return true unless params[:mounted_as]
|
|
|
|
|
2016-04-02 18:10:28 +05:30
|
|
|
upload_mounts = %w(avatar attachment file logo header_logo)
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
if upload_mounts.include?(params[:mounted_as])
|
|
|
|
params[:mounted_as]
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
def uploader
|
|
|
|
return @uploader if defined?(@uploader)
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
case model
|
|
|
|
when nil
|
|
|
|
@uploader = PersonalFileUploader.new(nil, params[:secret])
|
|
|
|
|
|
|
|
@uploader.retrieve_from_store!(params[:filename])
|
|
|
|
when PersonalSnippet
|
2017-08-17 22:00:37 +05:30
|
|
|
@uploader = PersonalFileUploader.new(model, params[:secret])
|
|
|
|
|
|
|
|
@uploader.retrieve_from_store!(params[:filename])
|
|
|
|
else
|
|
|
|
@uploader = @model.send(upload_mount)
|
|
|
|
|
|
|
|
redirect_to @uploader.url unless @uploader.file_storage?
|
|
|
|
end
|
|
|
|
|
|
|
|
@uploader
|
|
|
|
end
|
|
|
|
|
|
|
|
def uploader_class
|
|
|
|
PersonalFileUploader
|
|
|
|
end
|
|
|
|
|
|
|
|
def model
|
|
|
|
@model ||= find_model
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|