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
[#66298] It is possible to assign disabled and/or admin only project attributes given only edit permission
Dombi Attila8c4c2b104dfa
diff --git a/app/contracts/projects/base_contract.rb b/app/contracts/projects/base_contract.rbindex ec4da6ebe73..6a8c861eb32 100644--- a/app/contracts/projects/base_contract.rb+++ b/app/contracts/projects/base_contract.rb@@ -75,6 +75,14 @@ module Projectsendend+ def all_available_custom_fields+ if user.admin?+ model.all_available_custom_fields+ else+ model.all_available_custom_fields.reject(&:admin_only?)+ end+ end+delegate :assignable_versions, to: :modeldef assignable_status_codesdiff --git a/app/contracts/projects/update_contract.rb b/app/contracts/projects/update_contract.rbindex e0d6eebf512..d3397ed1d94 100644--- a/app/contracts/projects/update_contract.rb+++ b/app/contracts/projects/update_contract.rb@@ -32,11 +32,13 @@ module Projectsclass UpdateContract < BaseContractdef writable_attributesif allow_project_attributes_only- with_custom_fields_only(super)+ with_available_custom_fields_only(super)elsif allow_edit_attributes_onlywithout_custom_fields(super)elsif allow_all_attributes- super+ # When all attributes are updated (API-only case), allow writing to all available custom+ # fields (including disabled ones) to maintain backward compatibility with the API.+ with_all_available_custom_fields_only(super)else[]end@@ -63,7 +65,13 @@ module Projectsdef without_custom_fields(changes) = changes.grep_v(/^custom_field_/)- def with_custom_fields_only(changes) = changes.grep(/^custom_field_/)+ def with_available_custom_fields_only(changes) = changes & available_custom_fields.map(&:attribute_name)++ def with_all_available_custom_fields_only(changes)+ allowed_attributes = changes.grep_v(/^custom_field_/)+ allowed_attributes += changes & all_available_custom_fields.map(&:attribute_name)+ allowed_attributes+ enddef manage_permissionif changed_by_user == ["active"]diff --git a/spec/contracts/projects/update_contract_spec.rb b/spec/contracts/projects/update_contract_spec.rbindex e61a8921c8b..1f2fac270e6 100644--- a/spec/contracts/projects/update_contract_spec.rb+++ b/spec/contracts/projects/update_contract_spec.rb@@ -41,14 +41,31 @@ RSpec.describe Projects::UpdateContract doendend+ let(:admin_only_custom_field) do+ build_stubbed(:integer_project_custom_field, admin_only: true).tap do |cf|+ cf.id = "1002"+ allow_any_instance_of(CustomValue) # rubocop:disable RSpec/AnyInstance+ .to receive(:custom_field).and_return(cf)+ end+ end++ let(:not_enabled_custom_field) do+ build_stubbed(:integer_project_custom_field).tap do |cf|+ cf.id = "1003"+ 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])+ allow(p).to receive_messages(available_custom_fields: [custom_field, admin_only_custom_field],+ all_available_custom_fields: [custom_field, admin_only_custom_field,+ not_enabled_custom_field])next unless project_changed# in order to actually have something changed@@ -296,6 +313,92 @@ RSpec.describe Projects::UpdateContract doit_behaves_like "can not write", :nameendend++ context "with admin-only custom fields" do+ shared_examples "admin-only custom field behavior" do+ context "when user is admin" do+ let(:current_user) { build_stubbed(:admin) }+ let(:project_permissions) { %i(edit_project_attributes) }++ it_behaves_like "can write", :custom_field_1002+ end++ context "when user is not admin" do+ let(:current_user) { build_stubbed(:user) }+ let(:project_permissions) { %i(edit_project_attributes) }++ it_behaves_like "can not write", :custom_field_1002++ context "with all permissions" do+ let(:project_permissions) { %i(edit_project edit_project_attributes) }++ it_behaves_like "can not write", :custom_field_1002+ end+ end+ end++ context "when project_attributes_only is true" do+ let(:options) { { project_attributes_only: true } }++ it_behaves_like "admin-only custom field behavior"+ end++ context "when project_attributes_only is false" do+ let(:options) { { project_attributes_only: false } }++ it_behaves_like "admin-only custom field behavior"+ end+ end++ context "with not enabled custom fields" do+ context "when project_attributes_only is true" do+ let(:options) { { project_attributes_only: true } }++ context "when user is admin" do+ let(:current_user) { build_stubbed(:admin) }+ let(:project_permissions) { %i(edit_project_attributes) }++ it_behaves_like "can not write", :custom_field_1003+ end++ context "when user is not admin" do+ let(:current_user) { build_stubbed(:user) }+ let(:project_permissions) { %i(edit_project_attributes) }++ it_behaves_like "can not write", :custom_field_1003++ context "with all permissions" do+ let(:project_permissions) { %i(edit_project edit_project_attributes) }++ it_behaves_like "can not write", :custom_field_1003+ end+ end+ end++ context "when project_attributes_only is false (for API backward compatibility)" do+ let(:options) { { project_attributes_only: false } }++ context "when user is admin" do+ let(:current_user) { build_stubbed(:admin) }+ let(:project_permissions) { %i(edit_project edit_project_attributes) }++ it_behaves_like "can write", :custom_field_1003+ end++ context "when user is not admin" do+ let(:current_user) { build_stubbed(:user) }+ let(:project_permissions) { %i(edit_project_attributes) }++ it_behaves_like "can not write", :custom_field_1003++ context "with all permissions" do+ let(:project_permissions) { %i(edit_project edit_project_attributes) }++ it_behaves_like "can write", :custom_field_1003+ end+ end+ end+ endendendenddiff --git a/spec/requests/api/v3/projects/update_resource_spec.rb b/spec/requests/api/v3/projects/update_resource_spec.rbindex 2205417ec19..da3e637d13f 100644--- a/spec/requests/api/v3/projects/update_resource_spec.rb+++ b/spec/requests/api/v3/projects/update_resource_spec.rb@@ -46,7 +46,7 @@ RSpec.describe "API v3 Project resource update", content_type: :json dolet(:custom_field) docreate(:text_project_custom_field)end- let(:invisible_custom_field) do+ let(:admin_only_custom_field) docreate(:text_project_custom_field, admin_only: true)endlet(:permissions) { %i[edit_project view_project_attributes edit_project_attributes] }@@ -113,10 +113,10 @@ RSpec.describe "API v3 Project resource update", content_type: :json doendend- context "with an invisible custom field" do+ context "with an admin only custom field" dolet(:body) do{- invisible_custom_field.attribute_name(:camel_case) => {+ admin_only_custom_field.attribute_name(:camel_case) => {raw: "CF text"}}@@ -130,13 +130,13 @@ RSpec.describe "API v3 Project resource update", content_type: :json doendit "sets the cf value" do- expect(project.reload.send(invisible_custom_field.attribute_getter))+ expect(project.reload.send(admin_only_custom_field.attribute_getter)).to eql("CF text")endit "automatically activates the cf for project if the value was provided" doexpect(project.reload.project_custom_fields)- .to contain_exactly(invisible_custom_field)+ .to contain_exactly(admin_only_custom_field)endend@@ -153,11 +153,11 @@ RSpec.describe "API v3 Project resource update", content_type: :json docontext "when the hidden field has a value already" doit "does not change the cf value" do- project.custom_field_values = { invisible_custom_field.id => "1234" }+ project.custom_field_values = { admin_only_custom_field.id => "1234" }project.savepatch path, body.to_json- expect(project.reload.custom_values.find_by(custom_field: invisible_custom_field).value)+ expect(project.reload.custom_values.find_by(custom_field: admin_only_custom_field).value).to eq "1234"endend