Clear non-editable custom fields when creating an issue (43161).

This ensures that only editable custom fields are saved on a new issue
when copying an existing one. 

Patch by Holger Just (user:hjust).

git-svn-id: https://svn.redmine.org/redmine/trunk@23945 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Marius Balteanu
2025-09-07 06:18:55 +00:00
parent 686f5b15c0
commit bd034751bf
2 changed files with 29 additions and 0 deletions

View File

@@ -22,6 +22,7 @@ class Issue < ApplicationRecord
include Redmine::Utils::DateCalculation
include Redmine::I18n
before_validation :default_assign, on: :create
before_validation :force_default_value_on_noneditable_custom_fields, on: :create
before_validation :clear_disabled_fields
before_save :set_parent_id
include Redmine::NestedSet::IssueNestedSet
@@ -2100,6 +2101,21 @@ class Issue < ApplicationRecord
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'

View File

@@ -1575,6 +1575,19 @@ 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)