mirror of
https://github.com/discourse/discourse.git
synced 2025-09-06 10:50:21 +08:00
FIX: Data loss on update of single user_field.
https://meta.discourse.org/t/api-data-loss-caused-by-changed-behaviour-of-custom-user-field-update/74990
This commit is contained in:
parent
ae75c19f4b
commit
6a2bce1931
3 changed files with 26 additions and 1 deletions
|
@ -113,7 +113,10 @@ class UsersController < ApplicationController
|
||||||
fields = UserField.all
|
fields = UserField.all
|
||||||
fields = fields.where(editable: true) unless current_user.staff?
|
fields = fields.where(editable: true) unless current_user.staff?
|
||||||
fields.each do |f|
|
fields.each do |f|
|
||||||
val = params[:user_fields][f.id.to_s]
|
field_id = f.id.to_s
|
||||||
|
next unless params[:user_fields].has_key?(field_id)
|
||||||
|
|
||||||
|
val = params[:user_fields][field_id]
|
||||||
val = nil if val === "false"
|
val = nil if val === "false"
|
||||||
val = val[0...UserField.max_length] if val
|
val = val[0...UserField.max_length] if val
|
||||||
|
|
||||||
|
|
|
@ -312,6 +312,7 @@ class User < ActiveRecord::Base
|
||||||
@unread_notifications = nil
|
@unread_notifications = nil
|
||||||
@unread_total_notifications = nil
|
@unread_total_notifications = nil
|
||||||
@unread_pms = nil
|
@unread_pms = nil
|
||||||
|
@user_fields = nil
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1533,6 +1533,27 @@ describe UsersController do
|
||||||
|
|
||||||
expect(user.user_fields[user_field.id.to_s].size).to eq(UserField.max_length)
|
expect(user.user_fields[user_field.id.to_s].size).to eq(UserField.max_length)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should retain existing user fields" do
|
||||||
|
put :update, params: {
|
||||||
|
username: user.username, name: 'Jim Tom', user_fields: { user_field.id.to_s => 'happy', optional_field.id.to_s => 'feet' }
|
||||||
|
}, format: :json
|
||||||
|
|
||||||
|
expect(response).to be_success
|
||||||
|
expect(user.user_fields[user_field.id.to_s]).to eq('happy')
|
||||||
|
expect(user.user_fields[optional_field.id.to_s]).to eq('feet')
|
||||||
|
|
||||||
|
put :update, params: {
|
||||||
|
username: user.username, name: 'Jim Tom', user_fields: { user_field.id.to_s => 'sad' }
|
||||||
|
}, format: :json
|
||||||
|
|
||||||
|
expect(response).to be_success
|
||||||
|
|
||||||
|
user.reload
|
||||||
|
|
||||||
|
expect(user.user_fields[user_field.id.to_s]).to eq('sad')
|
||||||
|
expect(user.user_fields[optional_field.id.to_s]).to eq('feet')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "uneditable field" do
|
context "uneditable field" do
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue