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

feat[Op#57579]: Define CF activation contract

Kabiru Mwenjaaa573fe881f5
diff --git a/app/contracts/custom_fields/custom_field_projects/base_contract.rb b/app/contracts/custom_fields/custom_field_projects/base_contract.rb
new file mode 100644
index 00000000000..3db96e9eaf8
--- /dev/null
+++ b/app/contracts/custom_fields/custom_field_projects/base_contract.rb
@@ -0,0 +1,63 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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.
+#++
+
+module CustomFields
+ module CustomFieldProjects
+ class BaseContract < ::ModelContract
+ attribute :project_id
+ attribute :custom_field_id
+
+ validate :select_custom_fields_permission
+ validate :not_for_all
+ # FIXME: Confirm whether visible context is relevant
+ # validate :visible_to_user
+
+ def select_custom_fields_permission
+ return if user.allowed_in_project?(:select_custom_fields, model.project)
+
+ errors.add :base, :error_unauthorized
+ end
+
+ def not_for_all
+ # Only mappings of custom fields which are not enabled for all projects can be manipulated by the user
+ return if model.custom_field.nil? || !model.custom_field.is_for_all?
+
+ errors.add :custom_field_id, :cannot_delete_mapping
+ end
+
+ def visible_to_user
+ # "invisible" custom fields can only be seen and edited by admins
+ # using visible scope to check if the custom field is actually visible to the user
+ return if model.custom_field.nil? ||
+ CustomField.visible(user).pluck(:id).include?(model.custom_field.id)
+
+ errors.add :custom_field_id, :invalid
+ end
+ end
+ end
+end
diff --git a/app/contracts/custom_fields/custom_field_projects/update_contract.rb b/app/contracts/custom_fields/custom_field_projects/update_contract.rb
new file mode 100644
index 00000000000..cfbf9dcccaf
--- /dev/null
+++ b/app/contracts/custom_fields/custom_field_projects/update_contract.rb
@@ -0,0 +1,34 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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.
+#++
+
+module CustomFields
+ module CustomFieldProjects
+ class UpdateContract < BaseContract
+ end
+ end
+end
diff --git a/spec/contracts/custom_fields/custom_field_projects/base_contract_spec.rb b/spec/contracts/custom_fields/custom_field_projects/base_contract_spec.rb
new file mode 100644
index 00000000000..f679df41818
--- /dev/null
+++ b/spec/contracts/custom_fields/custom_field_projects/base_contract_spec.rb
@@ -0,0 +1,75 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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"
+
+RSpec.describe CustomFields::CustomFieldProjects::BaseContract do
+ include_context "ModelContract shared context"
+
+ let(:contract) { described_class.new(custom_field_project, user) }
+ let(:user) { build_stubbed(:admin) }
+ let(:project) { build_stubbed(:project) }
+ let(:custom_field) { build_stubbed(:custom_field) }
+ let(:custom_field_project) { build_stubbed(:custom_fields_project, project:, custom_field:) }
+
+ before { User.current = user }
+
+ context "when the custom field is for all" do
+ let(:custom_field) { build_stubbed(:custom_field, is_for_all: true) }
+
+ it_behaves_like "contract is invalid"
+ end
+
+ context "with non-visible custom field and admin user" do
+ pending "FIXME: Confirm whether visible context is relevant"
+ let(:custom_field) { build_stubbed(:custom_field, admin_only: true) }
+
+ before do
+ allow(CustomField).to receive(:all).and_return([custom_field])
+ end
+
+ it_behaves_like "contract is valid"
+ end
+
+ context "with non-visible custom field and non-admin user" do
+ pending "FIXME: Confirm whether visible context is relevant"
+ let(:user) { build_stubbed(:user) }
+ let(:custom_field) { build_stubbed(:custom_field, admin_only: true) }
+
+ # before do
+ # allow(CustomField).to receive(:visible).and_return([custom_field])
+ # end
+
+ it_behaves_like "contract is invalid"
+ end
+
+ include_examples "contract reuses the model errors"
+end