Browse Source

Remove references to deleted user from "user"-Format CustomFields when destroying an user (#32977).

Patch by Jens Krämer.

git-svn-id: http://svn.redmine.org/redmine/trunk@21207 e93f8b46-1217-0410-a6f0-8f06a7374b81
tags/5.0.0
Marius Balteanu 2 years ago
parent
commit
4378b7e1b4

+ 4
- 0
app/models/user.rb View File

@@ -952,6 +952,10 @@ 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').pluck(:id)
if user_custom_field_ids.any?
CustomValue.where(custom_field_id: user_custom_field_ids, value: self.id.to_s).delete_all
end
end

# Singleton class method is public

+ 12
- 0
db/migrate/20210801211024_remove_orphaned_user_custom_values.rb View File

@@ -0,0 +1,12 @@
class RemoveOrphanedUserCustomValues < ActiveRecord::Migration[6.1]
def up
user_custom_field_ids = CustomField.where(field_format: 'user').pluck(:id)
if user_custom_field_ids.any?
user_ids = Principal.pluck(:id)
CustomValue.
where(custom_field_id: user_custom_field_ids).
where.not(value: [nil, ''] + user_ids).
delete_all
end
end
end

+ 35
- 0
test/unit/user_test.rb View File

@@ -1313,6 +1313,41 @@ class UserTest < ActiveSupport::TestCase
assert_equal [], User.find(2).bookmarked_project_ids
end

def test_remove_custom_field_references_upon_destroy
cf1 = IssueCustomField.create(field_format: 'user', name: 'user cf', is_for_all: true, tracker_ids: Tracker.pluck(:id))
cf2 = IssueCustomField.create(field_format: 'user', name: 'users cf', is_for_all: true, multiple: true, tracker_ids: Tracker.pluck(:id))

issue = Issue.first
issue.init_journal(@admin)
assert_difference ->{cf1.custom_values.count} do
assert_difference ->{cf2.custom_values.count}, 2 do
issue.update(custom_field_values:
{
cf1.id => @jsmith.id,
cf2.id => [@dlopper.id, @jsmith.id]
})
end
end
assert cv1 = cf1.custom_values.where(customized_id: issue.id).last
assert_equal @jsmith.id.to_s, cv1.value

assert cv2 = cf2.custom_values.where(customized_id: issue.id)
assert_equal 2, cv2.size
assert cv2a = cv2.detect{|cv| cv.value == @dlopper.id.to_s}
assert cv2b = cv2.detect{|cv| cv.value == @jsmith.id.to_s}

# 2 custom values from the issue and 1 custom value from the user (CustomValue#3)
assert_difference ->{CustomValue.count}, -3 do
@jsmith.destroy
end

assert_raise(ActiveRecord::RecordNotFound){cv1.reload}
assert_raise(ActiveRecord::RecordNotFound){cv2b.reload}

cv2a.reload
assert_equal @dlopper.id.to_s, cv2a.value
end

if Object.const_defined?(:OpenID)
def test_setting_identity_url
normalized_open_id_url = 'http://example.com/'

Loading…
Cancel
Save