From ecb916f013445fc4486db40ddd62af1373a6e7e6 Mon Sep 17 00:00:00 2001 From: MAEDA Go Date: Mon, 14 Sep 2026 11:10:03 +0900 Subject: [PATCH] Add an option to allow groups in user custom fields (#21026). --- app/models/custom_field.rb | 1 + app/models/group.rb | 1 + app/models/principal.rb | 8 ++ app/models/query.rb | 3 + app/models/user.rb | 5 +- .../custom_fields/formats/_user.html.erb | 7 ++ config/locales/en.yml | 1 + config/locales/ja.yml | 1 + lib/redmine/field_format.rb | 117 +++++++++++++++--- .../custom_fields_controller_test.rb | 19 +++ test/unit/group_test.rb | 6 +- .../field_format/user_field_format_test.rb | 106 +++++++++++++++- test/unit/query_test.rb | 20 +++ 13 files changed, 271 insertions(+), 24 deletions(-) diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index 96758f440..5ac117b63 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -99,6 +99,7 @@ class CustomField < ApplicationRecord 'text_formatting', 'edit_tag_style', 'user_role', + 'possible_principals', 'version_status', 'extensions_allowed', 'full_width_layout', diff --git a/app/models/group.rb b/app/models/group.rb index 300b59b46..980d3239c 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -118,5 +118,6 @@ class Group < Principal Issue.where(['assigned_to_id = ?', id]).update_all('assigned_to_id = NULL') Watcher.where('user_id = ?', id).delete_all + remove_user_custom_field_references end end diff --git a/app/models/principal.rb b/app/models/principal.rb index 0a6c32ba2..48094241d 100644 --- a/app/models/principal.rb +++ b/app/models/principal.rb @@ -211,6 +211,14 @@ class Principal < ApplicationRecord Project.where(default_assigned_to: self).update_all(default_assigned_to_id: nil) end + # Removes the values of user custom fields that reference the principal + def remove_user_custom_field_references + user_custom_field_ids = CustomField.where(field_format: 'user').ids + if user_custom_field_ids.any? + CustomValue.where(custom_field_id: user_custom_field_ids, value: id.to_s).delete_all + end + end + protected # Make sure we don't try to insert NULL values (see #4632) diff --git a/app/models/query.rb b/app/models/query.rb index 824dc027d..f29a2b5db 100644 --- a/app/models/query.rb +++ b/app/models/query.rb @@ -1173,6 +1173,9 @@ class Query < ApplicationRecord if filter[:field].format.target_class && filter[:field].format.target_class <= User if value.delete('me') value.push User.current.id.to_s + if filter[:field].format.try(:selectable_principal_types, filter[:field])&.include?('Group') + value += User.current.group_ids.map(&:to_s) + end end end not_in = nil diff --git a/app/models/user.rb b/app/models/user.rb index 1dfac89e9..417d66fa6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1016,10 +1016,7 @@ class User < Principal Watcher.where('user_id = ?', id).delete_all WikiContent.where(['author_id = ?', id]).update_all(['author_id = ?', substitute.id]) WikiContentVersion.where(['author_id = ?', id]).update_all(['author_id = ?', substitute.id]) - user_custom_field_ids = CustomField.where(field_format: 'user').ids - if user_custom_field_ids.any? - CustomValue.where(custom_field_id: user_custom_field_ids, value: self.id.to_s).delete_all - end + remove_user_custom_field_references end # Singleton class method is public diff --git a/app/views/custom_fields/formats/_user.html.erb b/app/views/custom_fields/formats/_user.html.erb index 3bb4d29c0..5b180cde0 100644 --- a/app/views/custom_fields/formats/_user.html.erb +++ b/app/views/custom_fields/formats/_user.html.erb @@ -1,3 +1,10 @@ +

<%= f.select :possible_principals, + [ + [l(:label_user_plural), 'user'], + [l(:label_users_and_groups), 'user_group'], + [l(:label_group_plural), 'group'] + ], + :label => :field_possible_values %>