Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Allow overriding whitelist in FinishDirectUploadJob

Oliver Günthere9a08e64f010
diff --git a/app/workers/attachments/finish_direct_upload_job.rb b/app/workers/attachments/finish_direct_upload_job.rb
index bf9a78f7f66..ca530e13705 100644
--- a/app/workers/attachments/finish_direct_upload_job.rb
+++ b/app/workers/attachments/finish_direct_upload_job.rb
@@ -31,7 +31,7 @@
class Attachments::FinishDirectUploadJob < ApplicationJob
queue_with_priority :high
- def perform(attachment_id)
+ def perform(attachment_id, whitelist: true)
attachment = Attachment.pending_direct_uploads.find_by(id: attachment_id)
# An attachment is guaranteed to have a file.
# But if the attachment is nil the expression attachment&.file will be nil and attachment&.file.local_file
@@ -43,15 +43,15 @@ class Attachments::FinishDirectUploadJob < ApplicationJob
end
User.execute_as(attachment.author) do
- attach_uploaded_file(attachment, local_file)
+ attach_uploaded_file(attachment, local_file, whitelist)
end
end
private
- def attach_uploaded_file(attachment, local_file)
+ def attach_uploaded_file(attachment, local_file, whitelist)
set_attributes_from_file(attachment, local_file)
- validate_attachment(attachment)
+ validate_attachment(attachment, whitelist)
save_attachment(attachment)
journalize_container(attachment)
attachment_created_event(attachment)
@@ -76,8 +76,8 @@ class Attachments::FinishDirectUploadJob < ApplicationJob
attachment.save! if attachment.changed?
end
- def validate_attachment(attachment)
- contract = create_contract attachment
+ def validate_attachment(attachment, whitelist)
+ contract = create_contract attachment, whitelist
unless contract.valid?
errors = contract.errors.full_messages.join(", ")
@@ -85,8 +85,22 @@ class Attachments::FinishDirectUploadJob < ApplicationJob
end
end
- def create_contract(attachment)
- ::Attachments::CreateContract.new(attachment, attachment.author)
+ def create_contract(attachment, whitelist)
+ options = derive_contract_options(whitelist)
+ ::Attachments::CreateContract.new attachment,
+ attachment.author,
+ options: options
+ end
+
+ def derive_contract_options(whitelist)
+ case whitelist
+ when false
+ { whitelist: [] }
+ when Array
+ { whitelist: whitelist.map(&:to_s) }
+ else
+ {}
+ end
end
def journalize_container(attachment)
diff --git a/modules/bim/app/controllers/bim/ifc_models/ifc_models_controller.rb b/modules/bim/app/controllers/bim/ifc_models/ifc_models_controller.rb
index bd80c26f009..c7aa171c533 100644
--- a/modules/bim/app/controllers/bim/ifc_models/ifc_models_controller.rb
+++ b/modules/bim/app/controllers/bim/ifc_models/ifc_models_controller.rb
@@ -110,7 +110,8 @@ module Bim
session.delete :pending_ifc_model_ifc_model_id
if service_result.success?
- ::Attachments::FinishDirectUploadJob.perform_later attachment.id
+ ::Attachments::FinishDirectUploadJob.perform_later attachment.id,
+ whitelist: false
flash[:notice] = if new_model
t('ifc_models.flash_messages.upload_successful')
diff --git a/spec/workers/attachments/finish_direct_upload_job_integration_spec.rb b/spec/workers/attachments/finish_direct_upload_job_integration_spec.rb
index b6124d40012..c2af82f4010 100644
--- a/spec/workers/attachments/finish_direct_upload_job_integration_spec.rb
+++ b/spec/workers/attachments/finish_direct_upload_job_integration_spec.rb
@@ -154,6 +154,19 @@ describe Attachments::FinishDirectUploadJob, 'integration', type: :job do
expect(container.attachments).to be_empty
expect { pending_attachment.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
+
+ context 'when the job is getting a whitelist override' do
+ it "Does save the attachment" do
+ job.perform(pending_attachment.id, whitelist: false)
+
+ container.reload
+
+ expect(container.attachments.count).to eq 1
+ expect { pending_attachment.reload }.not_to raise_error(ActiveRecord::RecordNotFound)
+
+ expect(pending_attachment.downloads).to eq 0
+ end
+ end
end
context 'with the user not being allowed',