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

[#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.rb
index ec4da6ebe73..6a8c861eb32 100644
--- a/app/contracts/projects/base_contract.rb
+++ b/app/contracts/projects/base_contract.rb
@@ -75,6 +75,14 @@ module Projects
end
end
+ 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: :model
def assignable_status_codes
diff --git a/app/contracts/projects/update_contract.rb b/app/contracts/projects/update_contract.rb
index e0d6eebf512..d3397ed1d94 100644
--- a/app/contracts/projects/update_contract.rb
+++ b/app/contracts/projects/update_contract.rb
@@ -32,11 +32,13 @@ module Projects
class UpdateContract < BaseContract
def writable_attributes
if allow_project_attributes_only
- with_custom_fields_only(super)
+ with_available_custom_fields_only(super)
elsif allow_edit_attributes_only
without_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 Projects
def 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
+ end
def manage_permission
if changed_by_user == ["active"]
diff --git a/spec/contracts/projects/update_contract_spec.rb b/spec/contracts/projects/update_contract_spec.rb
index 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 do
end
end
+ 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) do
build_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 do
it_behaves_like "can not write", :name
end
end
+
+ 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
+ end
end
end
end
diff --git a/spec/requests/api/v3/projects/update_resource_spec.rb b/spec/requests/api/v3/projects/update_resource_spec.rb
index 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 do
let(:custom_field) do
create(:text_project_custom_field)
end
- let(:invisible_custom_field) do
+ let(:admin_only_custom_field) do
create(:text_project_custom_field, admin_only: true)
end
let(:permissions) { %i[edit_project view_project_attributes edit_project_attributes] }
@@ -113,10 +113,10 @@ RSpec.describe "API v3 Project resource update", content_type: :json do
end
end
- context "with an invisible custom field" do
+ context "with an admin only custom field" do
let(: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 do
end
it "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")
end
it "automatically activates the cf for project if the value was provided" do
expect(project.reload.project_custom_fields)
- .to contain_exactly(invisible_custom_field)
+ .to contain_exactly(admin_only_custom_field)
end
end
@@ -153,11 +153,11 @@ RSpec.describe "API v3 Project resource update", content_type: :json do
context "when the hidden field has a value already" do
it "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.save
patch 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"
end
end