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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Apply permission restrictions to Project::UpdateContract based on the attributes changed.
Dombi Attila2bd389872526
diff --git a/app/contracts/projects/update_contract.rb b/app/contracts/projects/update_contract.rbindex 582501bd64d..8c5e10a2b15 100644--- a/app/contracts/projects/update_contract.rb+++ b/app/contracts/projects/update_contract.rb@@ -30,6 +30,30 @@ module Projectsclass UpdateContract < BaseContractprivate+ def unauthenticated_changed+ permissions = {+ edit_project_attributes: user.allowed_in_project?(:edit_project_attributes, model),+ edit_project: user.allowed_in_project?(:edit_project, model)+ }++ case permissions+ in { edit_project_attributes: true, edit_project: false }+ super + without_custom_fields(changed_by_user)+ in { edit_project_attributes: false, edit_project: true }+ super + with_custom_fields_only(changed_by_user)+ else+ super+ end+ end++ def without_custom_fields(changes)+ changes.grep_v(/^custom_field_/)+ end++ def with_custom_fields_only(changes)+ changes.grep(/^custom_field_/)+ end+def manage_permissionif changed_by_user == ["active"]:archive_projectdiff --git a/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb b/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rbindex 103a3db31a6..dd94b000dce 100644--- a/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb+++ b/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb@@ -277,8 +277,7 @@ module Redmine# Skip when the new value is the default valuenext cfv_changes if value_was.nil? && cfv.default?-- cfv_changes.merge("custom_field_#{cfv.custom_field_id}": [value_was, cfv.value])+ cfv_changes.merge("custom_field_#{cfv.custom_field_id}" => [value_was, cfv.value])endenddiff --git a/spec/contracts/projects/update_contract_spec.rb b/spec/contracts/projects/update_contract_spec.rbindex 6259841b68c..5e690614da9 100644--- a/spec/contracts/projects/update_contract_spec.rb+++ b/spec/contracts/projects/update_contract_spec.rb@@ -31,12 +31,23 @@ require_relative "shared_contract_examples"RSpec.describe Projects::UpdateContract doit_behaves_like "project contract" do+ let(:custom_field) do+ build_stubbed(:integer_project_custom_field).tap do |cf|+ allow_any_instance_of(CustomValue) # rubocop:disable RSpec/AnyInstance+ .to receive(:custom_field).and_return(cf)+ end+ end+let(:project) dobuild_stubbed(:project,active: project_active,public: project_public,status_code: project_status_code,status_explanation: project_status_explanation).tap do |p|+ allow(p).to receive_messages(available_custom_fields: [custom_field],+ all_available_custom_fields: [custom_field])+ next unless project_changed+# in order to actually have something changedp.name = project_namep.parent = project_parent@@ -44,8 +55,10 @@ RSpec.describe Projects::UpdateContract doendendlet(:project_permissions) { [:edit_project] }+ let(:project_changed) { true }+ let(:options) { {} }- subject(:contract) { described_class.new(project, current_user) }+ subject(:contract) { described_class.new(project, current_user, options:) }context "if the identifier is nil" dolet(:project_identifier) { nil }@@ -54,5 +67,108 @@ RSpec.describe Projects::UpdateContract doexpect_valid(false, identifier: %i(blank))endend++ describe "permissions" do+ context "with edit_project_attributes" do+ let(:project_permissions) { [:edit_project_attributes] }++ context "when project_attributes_only flag is true" do+ let(:options) { { project_attributes_only: true } }++ before do+ project.custom_field_values = { custom_field.id => "1" }+ end++ context "and only project_custom_fields are changed" do+ let(:project_changed) { false }++ it_behaves_like "is valid"+ end++ context "and other project attributes are changed too" do+ let(:project_changed) { true }++ it "is invalid" do+ expect_valid(false, { name: %i(error_readonly),+ parent_id: %i(error_readonly),+ identifier: %i(error_readonly) })+ end+ end+ end++ context "when project_attributes_only flag is false" do+ let(:options) { { project_attributes_only: false } }++ it "is invalid" do+ expect_valid(false, base: %i(error_unauthorized))+ end+ end+ end++ context "with edit_project" do+ context "when project_attributes_only flag is true" do+ let(:options) { { project_attributes_only: true } }++ it "is invalid" do+ expect_valid(false, base: %i(error_unauthorized))+ end+ end++ context "when project_attributes_only flag is false" do+ let(:options) { { project_attributes_only: false } }++ context "and only project attributes are changed" do+ let(:project_changed) { true }++ it_behaves_like "is valid"+ end++ context "and project_custom_fields are changed too" do+ let(:project_changed) { true }++ before do+ project.custom_field_values = { custom_field.id => "1" }+ end++ it "is invalid" do+ expect_valid(false, "custom_field_#{custom_field.id}": %i(error_readonly))+ end+ end+ end+ end++ context "with both edit_project and edit_project_attributes are set" do+ let(:project_permissions) { %i(edit_project edit_project_attributes) }++ context "when project_attributes_only flag is false" do+ let(:options) { { project_attributes_only: false } }++ context "and only project attributes are changed" do+ let(:project_changed) { true }++ it_behaves_like "is valid"+ end++ context "and project_custom_fields are changed too" do+ let(:project_changed) { true }++ before do+ project.custom_field_values = { custom_field.id => "1" }+ end++ it_behaves_like "is valid"+ end+ end+ end++ context "without permissions when project_attributes_only flag is true" do+ let(:project_permissions) { [] }+ let(:options) { { project_attributes_only: true } }++ it "is invalid" do+ expect_valid(false, base: %i(error_unauthorized))+ end+ end+ endendend