From 12f01696df35881b627709773a9f7c33fd8db0fa Mon Sep 17 00:00:00 2001 From: Marius Balteanu Date: Sun, 7 Sep 2025 06:39:34 +0000 Subject: [PATCH] Backport r23945 from trunk to 5.1-stable (#43161). git-svn-id: https://svn.redmine.org/redmine/branches/5.1-stable@23952 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/issue.rb | 15 +++++++++++++++ test/unit/issue_test.rb | 11 +++++++++++ 2 files changed, 26 insertions(+) diff --git a/app/models/issue.rb b/app/models/issue.rb index 823bf90e8..3f841cffc 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -108,6 +108,7 @@ class Issue < ActiveRecord::Base end) before_validation :default_assign, on: :create + before_validation :force_default_value_on_noneditable_custom_fields, on: :create before_validation :clear_disabled_fields before_save :close_duplicates, :update_done_ratio_from_issue_status, :force_updated_on_change, :update_closed_on @@ -2074,6 +2075,20 @@ class Issue < ActiveRecord::Base end end + # Forcefully set the default value to any custom field values which are not + # editable by the current user when creating a new issue. This may overwrite + # existing custom values of a copied issue which are not editable by the + # current user. + def force_default_value_on_noneditable_custom_fields + return unless custom_field_values_changed? + editable_custom_field_ids = editable_custom_fields(author).map(&:id) + custom_field_values.each do |field_value| + unless editable_custom_field_ids.include?(field_value.custom_field_id) + field_value.value = field_value.custom_field.default_value + end + end + end + def filter_projects_scope(scope=nil) case scope when 'system' diff --git a/test/unit/issue_test.rb b/test/unit/issue_test.rb index c15660146..240728ca8 100644 --- a/test/unit/issue_test.rb +++ b/test/unit/issue_test.rb @@ -1575,6 +1575,17 @@ class IssueTest < ActiveSupport::TestCase assert_equal 'relation', j.details[0].property end + def test_copy_should_only_copy_editable_custom_fields + cf = CustomField.find 1 + cf.roles << Role.find(1) + cf.visible = false + cf.save! + User.current = User.find(3) + issue = Issue.new.copy_from(Issue.find(3)) + assert issue.save + assert_equal '', issue.custom_field_value(1) + end + def test_should_not_call_after_project_change_on_creation issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1, :subject => 'Test', :author_id => 1)