FIX: Staged user creation loses user custom field data (#22206)

Don't cache user_fields on users separately from custom_fields, since they can get out of sync.

---------

Co-authored-by: Daniel Waterworth <me@danielwaterworth.com>
This commit is contained in:
Penar Musaraj 2023-06-21 13:35:24 -04:00 committed by GitHub
parent 114a9a10b7
commit 47ab7eb49a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 10 deletions

View File

@ -592,7 +592,6 @@ class User < ActiveRecord::Base
@unread_pms = nil
@unread_bookmarks = nil
@unread_high_prios = nil
@user_fields_cache = nil
@ignored_user_ids = nil
@muted_user_ids = nil
@belonging_to_group_ids = nil
@ -1515,15 +1514,7 @@ class User < ActiveRecord::Base
def user_fields(field_ids = nil)
field_ids = (@all_user_field_ids ||= UserField.pluck(:id)) if field_ids.nil?
@user_fields_cache ||= {}
# Memoize based on requested fields
@user_fields_cache[field_ids.join(":")] ||= {}.tap do |hash|
field_ids.each do |fid|
# The hash keys are strings for backwards compatibility
hash[fid.to_s] = custom_fields["#{USER_FIELD_PREFIX}#{fid}"]
end
end
field_ids.map { |fid| [fid.to_s, custom_fields["#{USER_FIELD_PREFIX}#{fid}"]] }.to_h
end
def validatable_user_fields_values

View File

@ -1630,6 +1630,33 @@ RSpec.describe UsersController do
expect(created_user.email).to eq("staged@account.com")
expect(response.status).to eq(403)
end
it "works with custom fields" do
tennis_field = Fabricate(:user_field, show_on_profile: true, name: "Favorite tennis player")
post "/u.json",
params:
honeypot_magic(
email: staged.email,
username: "dude",
password: "P4ssw0rd$$",
user_fields: {
[tennis_field.id] => "Nadal",
},
)
expect(response.status).to eq(200)
result = response.parsed_body
expect(result["success"]).to eq(true)
created_user = User.find_by_email(staged.email)
expect(created_user.staged).to eq(false)
expect(created_user.active).to eq(false)
expect(created_user.registration_ip_address).to be_present
expect(!!created_user.custom_fields["from_staged"]).to eq(true)
expect(created_user.custom_fields["user_field_#{tennis_field.id}"]).to eq("Nadal")
end
end
end