Feature #21026 » 0001-Add-an-option-to-allow-groups-in-user-custom-fields-.patch
| app/models/custom_field.rb | ||
|---|---|---|
|
'text_formatting',
|
||
|
'edit_tag_style',
|
||
|
'user_role',
|
||
|
'possible_principals',
|
||
|
'version_status',
|
||
|
'extensions_allowed',
|
||
|
'full_width_layout',
|
||
| app/models/group.rb | ||
|---|---|---|
|
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
|
||
| app/models/principal.rb | ||
|---|---|---|
|
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)
|
||
| app/models/query.rb | ||
|---|---|---|
|
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
|
||
| app/models/user.rb | ||
|---|---|---|
|
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
|
||
| app/views/custom_fields/formats/_user.html.erb | ||
|---|---|---|
|
<p><%= 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 %></p>
|
||
|
<p>
|
||
|
<label><%= l(:label_role) %></label>
|
||
|
<label class="block">
|
||
| config/locales/en.yml | ||
|---|---|---|
|
label_user: User
|
||
|
label_user_plural: Users
|
||
|
label_users_and_groups: Users and groups
|
||
|
label_user_new: New user
|
||
|
label_user_anonymous: Anonymous
|
||
|
label_project: Project
|
||
| config/locales/ja.yml | ||
|---|---|---|
|
label_user: ユーザー
|
||
|
label_user_plural: ユーザー
|
||
|
label_users_and_groups: ユーザーとグループ
|
||
|
label_user_new: 新しいユーザー
|
||
|
label_user_anonymous: 匿名ユーザー
|
||
|
label_profile: プロフィール
|
||
| lib/redmine/field_format.rb | ||
|---|---|---|
|
opts = []
|
||
|
opts << [l(:label_no_change_option), ''] unless custom_field.multiple?
|
||
|
opts << [l(:label_none), '__none__'] unless custom_field.is_required?
|
||
|
opts += possible_values_options(custom_field, objects)
|
||
|
view.select_tag(
|
||
|
tag_name,
|
||
|
view.options_for_select(opts, value),
|
||
|
view.options_for_select(opts, value) + bulk_edit_tag_options(view, custom_field, objects, value),
|
||
|
options.merge(:multiple => custom_field.multiple?)
|
||
|
)
|
||
|
end
|
||
| ... | ... | |
|
blank_option = view.content_tag('option', ' '.html_safe, :value => '')
|
||
|
end
|
||
|
end
|
||
|
options_tags =
|
||
|
blank_option +
|
||
|
view.options_for_select(possible_custom_value_options(custom_value), custom_value.value)
|
||
|
options_tags = blank_option + select_edit_tag_options(view, custom_value)
|
||
|
s =
|
||
|
view.select_tag(
|
||
|
tag_name, options_tags,
|
||
| ... | ... | |
|
s
|
||
|
end
|
||
|
# Returns the option tags of the select edit tag
|
||
|
def select_edit_tag_options(view, custom_value)
|
||
|
view.options_for_select(possible_custom_value_options(custom_value), custom_value.value)
|
||
|
end
|
||
|
# Returns the option tags of the bulk edit tag
|
||
|
def bulk_edit_tag_options(view, custom_field, objects, value)
|
||
|
view.options_for_select(possible_values_options(custom_field, objects), value)
|
||
|
end
|
||
|
# Renders the edit tag as check box or radio tags
|
||
|
def check_box_edit_tag(view, tag_id, tag_name, custom_value, options={})
|
||
|
opts = []
|
||
| ... | ... | |
|
class UserFormat < RecordList
|
||
|
add 'user'
|
||
|
self.form_partial = 'custom_fields/formats/user'
|
||
|
field_attributes :user_role
|
||
|
field_attributes :user_role, :possible_principals
|
||
|
# Maps the possible_principals option to the principal types that can be selected
|
||
|
PRINCIPAL_TYPES = {
|
||
|
'user' => ['User'],
|
||
|
'user_group' => ['User', 'Group'],
|
||
|
'group' => ['Group']
|
||
|
}.freeze
|
||
|
def possible_values_options(custom_field, object=nil)
|
||
|
users = possible_values_records(custom_field, object)
|
||
|
options = users.map {|u| [u.name, u.id.to_s]}
|
||
|
if !custom_field.multiple? && users.include?(User.current)
|
||
|
options = [["<< #{l(:label_me)} >>", User.current.id.to_s]] + options
|
||
|
end
|
||
|
options
|
||
|
principals_options(custom_field, possible_values_records(custom_field, object))
|
||
|
end
|
||
|
def possible_values_records(custom_field, object=nil)
|
||
| ... | ... | |
|
projects = object.filter_map {|o| o.respond_to?(:project) ? o.project : nil}.uniq
|
||
|
projects.map {|project| possible_values_records(custom_field, project)}.reduce(:&) || []
|
||
|
elsif object.respond_to?(:project) && object.project
|
||
|
scope = object.project.users
|
||
|
scope = object.project.principals.where(:type => selectable_principal_types(custom_field))
|
||
|
if custom_field.user_role.is_a?(Array)
|
||
|
role_ids = custom_field.user_role.map(&:to_s).reject(&:blank?).map(&:to_i)
|
||
|
if role_ids.any?
|
||
| ... | ... | |
|
end
|
||
|
end
|
||
|
# Returns the principal types (User and/or Group) that can be
|
||
|
# selected for the given custom field
|
||
|
def selectable_principal_types(custom_field)
|
||
|
PRINCIPAL_TYPES[custom_field.possible_principals.to_s] || PRINCIPAL_TYPES['user']
|
||
|
end
|
||
|
def cast_single_value(custom_field, value, customized=nil)
|
||
|
Principal.find_by_id(value.to_i) if value.present?
|
||
|
end
|
||
|
def possible_custom_value_options(custom_value)
|
||
|
principals_options(custom_value.custom_field, possible_custom_value_records(custom_value))
|
||
|
end
|
||
|
# Returns the principals that can be selected for the given custom value,
|
||
|
# including the principals that are currently set but no longer selectable
|
||
|
def possible_custom_value_records(custom_value)
|
||
|
principals = possible_values_records(custom_value.custom_field, custom_value.customized).to_a
|
||
|
missing = [custom_value.value_was].flatten.reject(&:blank?).map(&:to_i) - principals.map(&:id)
|
||
|
if missing.any?
|
||
|
principals += Principal.where(:id => missing).to_a
|
||
|
end
|
||
|
principals
|
||
|
end
|
||
|
def value_from_keyword(custom_field, keyword, object)
|
||
|
users = possible_values_records(custom_field, object).to_a
|
||
|
principals = possible_values_records(custom_field, object).to_a
|
||
|
parse_keyword(custom_field, keyword) do |k|
|
||
|
Principal.detect_by_keyword(users, k).try(:id)
|
||
|
Principal.detect_by_keyword(principals, k).try(:id)
|
||
|
end
|
||
|
end
|
||
| ... | ... | |
|
end
|
||
|
end
|
||
|
protected
|
||
|
def select_edit_tag_options(view, custom_value)
|
||
|
principals_option_tags(view, custom_value.custom_field,
|
||
|
possible_custom_value_records(custom_value), custom_value.value)
|
||
|
end
|
||
|
def bulk_edit_tag_options(view, custom_field, objects, value)
|
||
|
principals_option_tags(view, custom_field, possible_values_records(custom_field, objects), value)
|
||
|
end
|
||
|
# Returns the [name, id] options for the given principals,
|
||
|
# with the "me" option first when the current user is selectable
|
||
|
def principals_options(custom_field, principals)
|
||
|
me_options(custom_field, principals) + principals_to_options(principals)
|
||
|
end
|
||
|
# Renders the option tags for the given principals. When both users
|
||
|
# and groups are present, they are listed in separate optgroups
|
||
|
def principals_option_tags(view, custom_field, principals, selected)
|
||
|
users, groups = principals.partition {|principal| principal.is_a?(User)}
|
||
|
options = view.options_for_select(me_options(custom_field, principals), selected)
|
||
|
if users.any? && groups.any?
|
||
|
optgroups = [
|
||
|
[l(:label_user_plural), principals_to_options(users)],
|
||
|
[l(:label_group_plural), principals_to_options(groups)]
|
||
|
]
|
||
|
options + view.grouped_options_for_select(optgroups, selected)
|
||
|
else
|
||
|
options + view.options_for_select(principals_to_options(principals), selected)
|
||
|
end
|
||
|
end
|
||
|
def me_options(custom_field, principals)
|
||
|
if !custom_field.multiple? && principals.include?(User.current)
|
||
|
[["<< #{l(:label_me)} >>", User.current.id.to_s]]
|
||
|
else
|
||
|
[]
|
||
|
end
|
||
|
end
|
||
|
def principals_to_options(principals)
|
||
|
principals.map {|principal| [principal.name, principal.id.to_s]}
|
||
|
end
|
||
|
def query_filter_values(custom_field, query)
|
||
|
query.author_values
|
||
|
types = selectable_principal_types(custom_field)
|
||
|
values =
|
||
|
query.principals.select {|p| types.include?(p.type)}.
|
||
|
sort_by {|p| [p.status, p]}.
|
||
|
collect {|p| [p.name, p.id.to_s, l("status_#{User::LABEL_BY_STATUS[p.status]}")]}
|
||
|
values.unshift(["<< #{l(:label_me)} >>", "me"]) if User.current.logged?
|
||
|
values
|
||
|
end
|
||
|
end
|
||
| test/functional/custom_fields_controller_test.rb | ||
|---|---|---|
|
assert_equal [1, 3], field.projects.map(&:id).sort
|
||
|
end
|
||
|
def test_create_user_custom_field
|
||
|
assert_difference 'CustomField.count' do
|
||
|
post(
|
||
|
:create,
|
||
|
:params => {
|
||
|
:type => "IssueCustomField",
|
||
|
:custom_field => {
|
||
|
:name => "foo",
|
||
|
:field_format => "user",
|
||
|
:possible_principals => "user_group"
|
||
|
}
|
||
|
}
|
||
|
)
|
||
|
assert_response :found
|
||
|
end
|
||
|
field = IssueCustomField.order(id: :desc).first
|
||
|
assert_equal 'user_group', field.possible_principals
|
||
|
end
|
||
|
def test_create_with_continue_params
|
||
|
assert_difference 'CustomField.count' do
|
||
|
post(
|
||
| test/unit/group_test.rb | ||
|---|---|---|
|
assert !User.find(8).member_of?(Project.find(5))
|
||
|
end
|
||
|
def test_destroy_should_unassign_and_unwatch_issues
|
||
|
def test_destroy_should_remove_references_from_issues
|
||
|
group = Group.find(10)
|
||
|
Issue.where(:id => 1).update_all(["assigned_to_id = ?", group.id])
|
||
|
issue = Issue.find(2)
|
||
| ... | ... | |
|
issue.save
|
||
|
issue.reload
|
||
|
assert issue.watcher_user_ids.include?(10)
|
||
|
cf = IssueCustomField.create!(:field_format => 'user', :possible_principals => 'user_group', :is_for_all => true, :name => 'User custom field', :tracker_ids => [1])
|
||
|
issue_with_cf = Issue.generate!(:project_id => 5, :tracker_id => 1, :custom_field_values => {cf.id.to_s => group.id.to_s})
|
||
|
assert_equal group.id.to_s, issue_with_cf.reload.custom_field_value(cf)
|
||
|
assert group.destroy
|
||
|
assert group.destroyed?
|
||
| ... | ... | |
|
assert_nil Issue.find(1).assigned_to_id
|
||
|
issue.reload
|
||
|
assert !issue.watcher_user_ids.include?(10)
|
||
|
assert_equal '', issue_with_cf.reload.custom_field_value(cf).to_s
|
||
|
end
|
||
|
def test_builtin_groups_should_be_created_if_missing
|
||
| test/unit/lib/redmine/field_format/user_field_format_test.rb | ||
|---|---|---|
|
field = IssueCustomField.new(:field_format => 'user')
|
||
|
project = Project.find(1)
|
||
|
assert_nil field.possible_principals
|
||
|
assert_equal ['Dave Lopper', 'John Smith'], field.possible_values_options(project).map(&:first)
|
||
|
end
|
||
|
def test_possible_values_options_should_return_project_members_with_selected_role
|
||
|
field = IssueCustomField.new(:field_format => 'user', :user_role => ["2"])
|
||
|
project = Project.find(1)
|
||
|
field = IssueCustomField.new(:field_format => 'user', :possible_principals => 'user_group', :user_role => ["2"])
|
||
|
project = Project.find(5)
|
||
|
assert_equal ['Dave Lopper'], field.possible_values_options(project).map(&:first)
|
||
|
assert_equal ['User Misc', 'A Team'], field.possible_values_options(project).map(&:first)
|
||
|
end
|
||
|
def test_possible_values_options_should_return_project_members_and_me_if_logged_in
|
||
| ... | ... | |
|
assert_equal [2], field.value_from_keyword('jsmith', project)
|
||
|
assert_equal [], field.value_from_keyword('Unknown User', project)
|
||
|
end
|
||
|
def test_possible_values_options_with_groups_only_should_include_groups_only
|
||
|
# The current user is a member of the project but should not be included
|
||
|
User.current = User.find(2)
|
||
|
field = IssueCustomField.new(:field_format => 'user', :possible_principals => 'group')
|
||
|
project = Project.find(5)
|
||
|
assert_equal [['A Team', '10']], field.possible_values_options(project)
|
||
|
end
|
||
|
def test_group_value_should_be_invalid_when_groups_are_not_allowed
|
||
|
field = IssueCustomField.create!(:name => 'Foo', :field_format => 'user', :is_for_all => true, :trackers => Tracker.all)
|
||
|
issue = Issue.new(:project_id => 5, :tracker_id => 1, :author_id => 2, :subject => 'Test',
|
||
|
:custom_field_values => {field.id => '10'})
|
||
|
assert_equal false, issue.valid?
|
||
|
assert_include "Foo #{::I18n.t('activerecord.errors.messages.inclusion')}", issue.errors.full_messages.first
|
||
|
end
|
||
|
def test_existing_group_value_should_remain_valid_after_disallowing_groups
|
||
|
field = IssueCustomField.create!(:name => 'Foo', :field_format => 'user', :possible_principals => 'user_group',
|
||
|
:is_for_all => true, :trackers => Tracker.all)
|
||
|
issue = Issue.generate!(:project_id => 5, :tracker_id => 1, :custom_field_values => {field.id => '10'})
|
||
|
field.update!(:possible_principals => 'user')
|
||
|
issue.reload
|
||
|
assert_equal '10', issue.custom_field_value(field)
|
||
|
assert issue.valid?
|
||
|
assert_equal 'A Team', field.format.formatted_value(self, field, '10', issue).to_s
|
||
|
# Group can still be selected for the issue, but not for other issues
|
||
|
assert_include '10', issue.custom_field_values.detect {|v| v.custom_field == field}.then {|v| field.format.possible_custom_value_options(v)}.map(&:last)
|
||
|
assert_not_include '10', field.possible_values_options(Issue.new(:project_id => 5)).map(&:last)
|
||
|
end
|
||
|
def test_edit_tag_should_not_use_optgroups_with_users_only_or_groups_only
|
||
|
field = IssueCustomField.new(:field_format => 'user', :is_required => false)
|
||
|
value = CustomFieldValue.new(:custom_field => field, :customized => Issue.new(:project_id => 5))
|
||
|
tag = field.format.edit_tag(self, 'abc', 'xyz', value)
|
||
|
assert_select_in tag, 'select[id=abc][name=xyz]' do
|
||
|
assert_select 'optgroup', 0
|
||
|
assert_select 'select > option[value="2"]', :text => 'John Smith'
|
||
|
assert_select 'option[value="10"]', 0
|
||
|
end
|
||
|
field = IssueCustomField.new(:field_format => 'user', :possible_principals => 'group', :is_required => false)
|
||
|
value = CustomFieldValue.new(:custom_field => field, :customized => Issue.new(:project_id => 5))
|
||
|
tag = field.format.edit_tag(self, 'abc', 'xyz', value)
|
||
|
assert_select_in tag, 'select[id=abc][name=xyz]' do
|
||
|
assert_select 'optgroup', 0
|
||
|
assert_select 'select > option[value="10"]', :text => 'A Team'
|
||
|
assert_select 'option[value="2"]', 0
|
||
|
end
|
||
|
end
|
||
|
def test_edit_tag_with_groups_should_group_users_and_groups_in_optgroups
|
||
|
set_language_if_valid 'en'
|
||
|
User.current = User.find(2)
|
||
|
field = IssueCustomField.new(:field_format => 'user', :possible_principals => 'user_group', :is_required => false)
|
||
|
value = CustomFieldValue.new(:custom_field => field, :customized => Issue.new(:project_id => 5))
|
||
|
tag = field.format.edit_tag(self, 'abc', 'xyz', value)
|
||
|
assert_select_in tag, 'select[id=abc][name=xyz]' do
|
||
|
assert_select 'option', :text => '<< me >>', :count => 1
|
||
|
assert_select 'optgroup', 2
|
||
|
assert_select 'optgroup:nth-of-type(1)[label="Users"]' do
|
||
|
assert_select 'option[value="2"]', :text => 'John Smith'
|
||
|
assert_select 'option[value="10"]', 0
|
||
|
end
|
||
|
assert_select 'optgroup:nth-of-type(2)[label="Groups"]' do
|
||
|
assert_select 'option', 1
|
||
|
assert_select 'option[value="10"]', :text => 'A Team'
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
def test_bulk_edit_tag_with_groups_should_group_users_and_groups_in_optgroups
|
||
|
set_language_if_valid 'en'
|
||
|
field = IssueCustomField.new(:field_format => 'user', :possible_principals => 'user_group', :is_required => false)
|
||
|
issues = [Issue.new(:project_id => 5), Issue.new(:project_id => 5)]
|
||
|
tag = field.format.bulk_edit_tag(self, 'abc', 'xyz', field, issues, '')
|
||
|
assert_select_in tag, 'select[name=xyz]' do
|
||
|
assert_select 'select > option[value=""]'
|
||
|
assert_select 'select > option[value="__none__"]'
|
||
|
assert_select 'optgroup[label="Users"] option[value="2"]', :text => 'John Smith'
|
||
|
assert_select 'optgroup[label="Groups"] option[value="10"]', :text => 'A Team'
|
||
|
end
|
||
|
end
|
||
|
def test_value_from_keyword_should_return_group_id_when_groups_are_allowed
|
||
|
project = Project.find(5)
|
||
|
field = IssueCustomField.new(:field_format => 'user', :possible_principals => 'user_group')
|
||
|
assert_equal 10, field.value_from_keyword('A Team', project)
|
||
|
field = IssueCustomField.new(:field_format => 'user')
|
||
|
assert_nil field.value_from_keyword('A Team', project)
|
||
|
end
|
||
|
end
|
||
| test/unit/query_test.rb | ||
|---|---|---|
|
assert_equal issue1, result.first
|
||
|
end
|
||
|
def test_user_custom_field_with_groups_should_be_filterable_on_users_and_groups
|
||
|
User.current = User.find(8)
|
||
|
cf = IssueCustomField.create!(:field_format => 'user', :possible_principals => 'user_group', :is_for_all => true, :is_filter => true, :name => 'User custom field', :tracker_ids => [1])
|
||
|
issue1 = Issue.generate!(:project_id => 5, :tracker_id => 1, :custom_field_values => {cf.id.to_s => '10'})
|
||
|
issue2 = Issue.generate!(:project_id => 5, :tracker_id => 1, :custom_field_values => {cf.id.to_s => '8'})
|
||
|
issue3 = Issue.generate!(:project_id => 5, :tracker_id => 1, :custom_field_values => {cf.id.to_s => '2'})
|
||
|
query = IssueQuery.new(:name => '_', :project => Project.find(5))
|
||
|
filter = query.available_filters["cf_#{cf.id}"]
|
||
|
assert_not_nil filter
|
||
|
values = filter[:values].pluck(1)
|
||
|
assert_include 'me', values
|
||
|
assert_include '2', values
|
||
|
assert_include '10', values
|
||
|
# "me" includes the groups of the current user
|
||
|
query.filters = {"cf_#{cf.id}" => {:operator => '=', :values => ['me']}}
|
||
|
assert_equal [issue1, issue2].map(&:id).sort, query.issues.map(&:id).sort
|
||
|
end
|
||
|
def test_filter_on_chained_user_custom_field
|
||
|
user = User.find(2)
|
||
|
User.current = user
|
||
- « Previous
- 1
- 2
- 3
- Next »