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

[#30583] Introduce module specific views for BIM module (#9954)

Wieland Lindenthal20c7a578f145
diff --git a/modules/bim/app/contracts/bim/views/contract_strategy.rb b/modules/bim/app/contracts/bim/views/contract_strategy.rb
new file mode 100644
index 00000000000..932054cda02
--- /dev/null
+++ b/modules/bim/app/contracts/bim/views/contract_strategy.rb
@@ -0,0 +1,29 @@
+module ::Bim
+ module Views
+ class ContractStrategy < ::BaseContract
+ validate :manageable
+
+ private
+
+ def manageable
+ return if model.query.blank?
+
+ errors.add(:base, :error_unauthorized) unless view_permissions?
+ end
+
+ def view_permissions?
+ return false unless user_allowed_on_view?(:view_ifc_models)
+ return false unless user_allowed_on_view?(:save_bcf_queries)
+ if model.query.is_public && !user_allowed_on_view?(:manage_public_bcf_queries)
+ return false
+ end
+
+ true
+ end
+
+ def user_allowed_on_view?(permission)
+ user.allowed_to?(permission, model.query.project, global: model.query.project.nil?)
+ end
+ end
+ end
+end
diff --git a/modules/bim/lib/open_project/bim/engine.rb b/modules/bim/lib/open_project/bim/engine.rb
index e94a680208a..504f5a0ab43 100644
--- a/modules/bim/lib/open_project/bim/engine.rb
+++ b/modules/bim/lib/open_project/bim/engine.rb
@@ -43,6 +43,7 @@ module OpenProject::Bim
}
} do
project_module(:bim,
+ dependencies: :work_package_tracking,
if: ->(*) { OpenProject::Configuration.bim? }) do
permission :view_ifc_models,
{
@@ -74,6 +75,12 @@ module OpenProject::Bim
edit_work_packages
delete_work_packages],
contract_actions: { bcf: %i[destroy] }
+ permission :save_bcf_queries,
+ {},
+ dependencies: %i[save_queries]
+ permission :manage_public_bcf_queries,
+ {},
+ dependencies: %i[manage_public_queries save_bcf_queries]
end
OpenProject::AccessControl.permission(:view_work_packages).controller_actions << 'bim/bcf/issues/redirect_to_bcf_issues_list'
@@ -225,5 +232,8 @@ module OpenProject::Bim
end
end
end
+
+ add_view :Bim,
+ contract_strategy: 'Bim::Views::ContractStrategy'
end
end
diff --git a/modules/bim/spec/contracts/views/create_contract_bim_spec.rb b/modules/bim/spec/contracts/views/create_contract_bim_spec.rb
new file mode 100644
index 00000000000..e1430879c30
--- /dev/null
+++ b/modules/bim/spec/contracts/views/create_contract_bim_spec.rb
@@ -0,0 +1,100 @@
+#-- 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+require 'contracts/shared/model_contract_shared_context'
+require 'contracts/views/shared_contract_examples'
+
+describe Views::CreateContract do
+ it_behaves_like 'view contract', true do
+ let(:view) do
+ View.new(query: view_query,
+ type: view_type)
+ end
+ let(:view_type) do
+ 'bim'
+ end
+ let(:permissions) do
+ %i[view_work_packages
+ view_ifc_models
+ save_queries
+ save_bcf_queries
+ manage_public_queries
+ manage_public_bcf_queries]
+ end
+
+ subject(:contract) do
+ described_class.new(view, current_user)
+ end
+
+ describe 'validation' do
+ context 'with the type being nil' do
+ let(:view_type) { nil }
+
+ it_behaves_like 'contract is invalid', type: :inclusion
+ end
+
+ context 'with the type not being one of the configured' do
+ let(:view_type) { 'blubs' }
+
+ it_behaves_like 'contract is invalid', type: :inclusion
+ end
+
+ context 'without the :view_ifc_models permission' do
+ let(:permissions) { %i[view_work_packages save_queries save_bcf_queries] }
+
+ it_behaves_like 'contract is invalid', base: :error_unauthorized
+ end
+
+ context 'without the :save_bcf_queries permission' do
+ let(:permissions) { %i[view_ifc_models view_work_packages save_queries] }
+
+ it_behaves_like 'contract is invalid', base: :error_unauthorized
+ end
+
+ context 'with a public query and without the :manage_public_bcf_queries permission' do
+ let(:query_public) { true }
+ let(:permissions) { %i[view_ifc_models view_work_packages save_queries save_bcf_queries manage_public_queries] }
+
+ it_behaves_like 'contract is invalid', base: :error_unauthorized
+ end
+
+ context 'with a public query and all necessary permissions' do
+ let(:query_public) { true }
+
+ it_behaves_like 'contract is valid'
+ end
+
+ context 'with a private query and all necessary permissions' do
+ let(:permissions) { %i[view_work_packages view_ifc_models save_queries save_bcf_queries] }
+
+ it_behaves_like 'contract is valid'
+ end
+ end
+ end
+end
diff --git a/modules/bim/spec/models/queries/views/filters/type_filter_spec.rb b/modules/bim/spec/models/queries/views/filters/type_filter_spec.rb
new file mode 100644
index 00000000000..1481948e850
--- /dev/null
+++ b/modules/bim/spec/models/queries/views/filters/type_filter_spec.rb
@@ -0,0 +1,50 @@
+#-- 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+
+describe Queries::Views::Filters::TypeFilter, type: :model do
+ let(:current_user) { FactoryBot.create(:user) }
+
+ before do
+ login_as(current_user)
+ end
+
+ it_behaves_like 'basic query filter' do
+ let(:class_key) { :type }
+ let(:type) { :list_optional }
+
+ describe '#allowed_values' do
+ it 'includes the Bim views' do
+ expected_bim_view = %w[Views::Bim Views::Bim]
+
+ expect(([expected_bim_view] - instance.allowed_values).size).to be 0
+ end
+ end
+ end
+end
diff --git a/spec/models/queries/views/filters/type_filter_spec.rb b/spec/models/queries/views/filters/type_filter_spec.rb
index 032e92a034a..2b958ef0752 100644
--- a/spec/models/queries/views/filters/type_filter_spec.rb
+++ b/spec/models/queries/views/filters/type_filter_spec.rb
@@ -40,13 +40,13 @@ describe Queries::Views::Filters::TypeFilter, type: :model do
let(:type) { :list_optional }
describe '#allowed_values' do
- it 'is a list of the possible values' do
- expected = [
+ it 'includes the core views' do
+ expected_core_views = [
%w[Views::TeamPlanner Views::TeamPlanner],
%w[Views::WorkPackagesTable Views::WorkPackagesTable]
]
- expect(instance.allowed_values).to match_array(expected)
+ expect((expected_core_views - instance.allowed_values).size).to be 0
end
end
end