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

Give access to the issue project when trying to save a viewpoint (#9707)

Oliver Günther0b5da9b9e1e1
diff --git a/app/services/attachments/base_service.rb b/app/services/attachments/base_service.rb
new file mode 100644
index 00000000000..c0a2ff697c4
--- /dev/null
+++ b/app/services/attachments/base_service.rb
@@ -0,0 +1,43 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+module Attachments
+ class BaseService < ::BaseServices::Create
+ ##
+ # Create an attachment service bypassing the user-provided whitelist
+ # for internal purposes such as exporting data.
+ #
+ # @param user The user to call the service with
+ # @param whitelist A custom whitelist to validate with, or empty to disable validation
+ #
+ # Warning: When passing an empty whitelist, this results in no validations on the content type taking place.
+ def self.bypass_whitelist(user:, whitelist: [])
+ new(user: user, contract_options: { whitelist: whitelist.map(&:to_s) })
+ end
+ end
+end
diff --git a/app/services/attachments/build_service.rb b/app/services/attachments/build_service.rb
index 9a725f571bf..51225070c9f 100644
--- a/app/services/attachments/build_service.rb
+++ b/app/services/attachments/build_service.rb
@@ -27,7 +27,7 @@
#++
module Attachments
- class BuildService < ::BaseServices::Create
+ class BuildService < BaseService
private
def persist(service_result)
diff --git a/app/services/attachments/create_service.rb b/app/services/attachments/create_service.rb
index 850f6c13e0e..506b24d76a6 100644
--- a/app/services/attachments/create_service.rb
+++ b/app/services/attachments/create_service.rb
@@ -26,74 +26,64 @@
# See docs/COPYRIGHT.rdoc for more details.
#++
-class Attachments::CreateService < ::BaseServices::Create
- include Attachments::TouchContainer
+module Attachments
+ class CreateService < BaseService
+ include TouchContainer
- around_call :error_wrapped_call
+ around_call :error_wrapped_call
- ##
- # Create an attachment service bypassing the user-provided whitelist
- # for internal purposes such as exporting data.
- #
- # @param user The user to call the service with
- # @param whitelist A custom whitelist to validate with, or empty to disable validation
- #
- # Warning: When passing an empty whitelist, this results in no validations on the content type taking place.
- def self.bypass_whitelist(user:, whitelist: [])
- new(user: user, contract_options: { whitelist: whitelist.map(&:to_s) })
- end
-
- def persist(call)
- attachment = call.result
- if attachment.container
- in_container_mutex(attachment.container) { super }
- else
- super
+ def persist(call)
+ attachment = call.result
+ if attachment.container
+ in_container_mutex(attachment.container) { super }
+ else
+ super
+ end
end
- end
- def in_container_mutex(container)
- OpenProject::Mutex.with_advisory_lock_transaction(container) do
- yield.tap do
- # Get the latest attachments to ensure having them all for journalization.
- # We just created an attachment and a different worker might have added attachments
- # in the meantime, e.g when bulk uploading.
- container.attachments.reload
+ def in_container_mutex(container)
+ OpenProject::Mutex.with_advisory_lock_transaction(container) do
+ yield.tap do
+ # Get the latest attachments to ensure having them all for journalization.
+ # We just created an attachment and a different worker might have added attachments
+ # in the meantime, e.g when bulk uploading.
+ container.attachments.reload
+ end
end
end
- end
- def after_perform(call)
- attachment = call.result
- container = attachment.container
+ def after_perform(call)
+ attachment = call.result
+ container = attachment.container
- touch(container) unless container.nil?
+ touch(container) unless container.nil?
- OpenProject::Notifications.send(
- OpenProject::Events::ATTACHMENT_CREATED,
- attachment: attachment
- )
+ OpenProject::Notifications.send(
+ OpenProject::Events::ATTACHMENT_CREATED,
+ attachment: attachment
+ )
- call
- end
+ call
+ end
- def error_wrapped_call
- yield
- rescue StandardError => e
- log_attachment_saving_error(e)
+ def error_wrapped_call
+ yield
+ rescue StandardError => e
+ log_attachment_saving_error(e)
- message =
- if e&.class&.to_s == 'Errno::EACCES'
- I18n.t('api_v3.errors.unable_to_create_attachment_permissions')
- else
- I18n.t('api_v3.errors.unable_to_create_attachment')
- end
- raise message
- end
+ message =
+ if e&.class&.to_s == 'Errno::EACCES'
+ I18n.t('api_v3.errors.unable_to_create_attachment_permissions')
+ else
+ I18n.t('api_v3.errors.unable_to_create_attachment')
+ end
+ raise message
+ end
- def log_attachment_saving_error(error)
- message = "Failed to save attachment: #{error&.class} - #{error&.message || 'Unknown error'}"
+ def log_attachment_saving_error(error)
+ message = "Failed to save attachment: #{error&.class} - #{error&.message || 'Unknown error'}"
- OpenProject.logger.error message
+ OpenProject.logger.error message
+ end
end
end
diff --git a/modules/bim/app/models/bim/bcf/viewpoint.rb b/modules/bim/app/models/bim/bcf/viewpoint.rb
index aaa3bff718b..b68a06d7f4d 100644
--- a/modules/bim/app/models/bim/bcf/viewpoint.rb
+++ b/modules/bim/app/models/bim/bcf/viewpoint.rb
@@ -42,11 +42,11 @@ module Bim::Bcf
def snapshot=(file)
snapshot&.destroy
build_snapshot file
- end
+ end
def build_snapshot(file, user: User.current)
::Attachments::BuildService
- .new(user: user)
+ .bypass_whitelist(user: user)
.call(file: file, container: self, filename: file.original_filename, description: 'snapshot')
.result
end
diff --git a/modules/bim/app/services/bim/ifc_models/set_attributes_service.rb b/modules/bim/app/services/bim/ifc_models/set_attributes_service.rb
index 0120d833e90..61ea6d6b5bf 100644
--- a/modules/bim/app/services/bim/ifc_models/set_attributes_service.rb
+++ b/modules/bim/app/services/bim/ifc_models/set_attributes_service.rb
@@ -77,7 +77,7 @@ module Bim
model.attachments << ifc_attachment
else
::Attachments::BuildService
- .new(user: user)
+ .bypass_whitelist(user: user)
.call(file: ifc_attachment, container: model, filename: ifc_attachment.original_filename, description: 'ifc')
end
end
diff --git a/modules/bim/lib/open_project/bim/bcf_xml/issue_reader.rb b/modules/bim/lib/open_project/bim/bcf_xml/issue_reader.rb
index 90727378d25..18862d85bf2 100644
--- a/modules/bim/lib/open_project/bim/bcf_xml/issue_reader.rb
+++ b/modules/bim/lib/open_project/bim/bcf_xml/issue_reader.rb
@@ -45,7 +45,7 @@ module OpenProject::Bim::BcfXml
# If there are already errors during the BCF issue creation, don't create or update the WP.
return if issue.errors.any?
- self.is_update = issue.work_package.present?
+ self.is_update = issue.work_package.persisted?
self.wp_last_updated_at = issue.work_package&.updated_at
call =
@@ -218,7 +218,7 @@ module OpenProject::Bim::BcfXml
end
def initialize_issue
- ::Bim::Bcf::Issue.new(uuid: topic_uuid)
+ ::Bim::Bcf::Issue.new(uuid: topic_uuid, project: project)
end
##
diff --git a/modules/bim/spec/bcf/bcf_xml/importer_spec.rb b/modules/bim/spec/bcf/bcf_xml/importer_spec.rb
index 5e153921573..8bab6134a74 100644
--- a/modules/bim/spec/bcf/bcf_xml/importer_spec.rb
+++ b/modules/bim/spec/bcf/bcf_xml/importer_spec.rb
@@ -40,6 +40,7 @@ describe ::OpenProject::Bim::BcfXml::Importer do
let(:project) do
FactoryBot.create(:project,
identifier: 'bim_project',
+ enabled_module_names: %w[bim work_package_tracking],
types: [type])
end
let(:member_role) do
@@ -99,4 +100,20 @@ describe ::OpenProject::Bim::BcfXml::Importer do
expect(WorkPackage.count).to be_eql 2
end
end
+
+ context 'with a viewpoint and snapshot' do
+ let(:filename) { 'issue-with-viewpoint.bcf' }
+
+ it 'imports that viewpoint successfully' do
+ expect(subject.import!).to be_present
+
+ expect(::Bim::Bcf::Issue.count).to eq 1
+ issue = ::Bim::Bcf::Issue.last
+ expect(issue.viewpoints.count).to eq 1
+
+ viewpoint = issue.viewpoints.first
+ expect(viewpoint.attachments.count).to eq 1
+ expect(viewpoint.snapshot).to be_present
+ end
+ end
end
diff --git a/modules/bim/spec/contracts/ifc_models/shared_contract_examples.rb b/modules/bim/spec/contracts/ifc_models/shared_contract_examples.rb
index d16b8d0adf1..d78af7dad37 100644
--- a/modules/bim/spec/contracts/ifc_models/shared_contract_examples.rb
+++ b/modules/bim/spec/contracts/ifc_models/shared_contract_examples.rb
@@ -106,7 +106,7 @@ shared_examples_for 'ifc model contract' do
let(:ifc_file) { FileHelpers.mock_uploaded_file name: "model.ifc", content_type: 'application/binary', binary: true }
let(:ifc_attachment) do
::Attachments::BuildService
- .new(user: current_user)
+ .bypass_whitelist(user: current_user)
.call(file: ifc_file, filename: 'model.ifc')
.result
end
@@ -122,7 +122,7 @@ shared_examples_for 'ifc model contract' do
end
let(:ifc_attachment) do
::Attachments::BuildService
- .new(user: current_user)
+ .bypass_whitelist(user: current_user)
.call(file: ifc_file, filename: 'model.ifc')
.result
end
diff --git a/modules/bim/spec/fixtures/files/issue-with-viewpoint.bcf b/modules/bim/spec/fixtures/files/issue-with-viewpoint.bcf
new file mode 100644
index 00000000000..7ca26d3eb0c
Binary files /dev/null and b/modules/bim/spec/fixtures/files/issue-with-viewpoint.bcf differ