FIX: handle new user when logging name change
This commit is contained in:
parent
ddcd060552
commit
e27b1b98d1
|
@ -146,7 +146,7 @@ class StaffActionLogger
|
||||||
|
|
||||||
def log_site_text_change(subject, new_text=nil, old_text=nil, opts={})
|
def log_site_text_change(subject, new_text=nil, old_text=nil, opts={})
|
||||||
raise Discourse::InvalidParameters.new(:subject) unless subject.present?
|
raise Discourse::InvalidParameters.new(:subject) unless subject.present?
|
||||||
UserHistory.create( params(opts).merge({
|
UserHistory.create!( params(opts).merge({
|
||||||
action: UserHistory.actions[:change_site_text],
|
action: UserHistory.actions[:change_site_text],
|
||||||
subject: subject,
|
subject: subject,
|
||||||
previous_value: old_text,
|
previous_value: old_text,
|
||||||
|
|
|
@ -53,6 +53,9 @@ class UserUpdater
|
||||||
user_profile.profile_background = attributes.fetch(:profile_background) { user_profile.profile_background }
|
user_profile.profile_background = attributes.fetch(:profile_background) { user_profile.profile_background }
|
||||||
user_profile.card_background = attributes.fetch(:card_background) { user_profile.card_background }
|
user_profile.card_background = attributes.fetch(:card_background) { user_profile.card_background }
|
||||||
|
|
||||||
|
old_user_name = user.name.present? ? user.name : ""
|
||||||
|
user.name = attributes.fetch(:name) { user.name }
|
||||||
|
|
||||||
user.locale = attributes.fetch(:locale) { user.locale }
|
user.locale = attributes.fetch(:locale) { user.locale }
|
||||||
user.date_of_birth = attributes.fetch(:date_of_birth) { user.date_of_birth }
|
user.date_of_birth = attributes.fetch(:date_of_birth) { user.date_of_birth }
|
||||||
|
|
||||||
|
@ -94,20 +97,21 @@ class UserUpdater
|
||||||
end
|
end
|
||||||
|
|
||||||
User.transaction do
|
User.transaction do
|
||||||
# log name changes
|
|
||||||
if attributes[:name].present? && user.name.downcase != attributes.fetch(:name).downcase
|
|
||||||
StaffActionLogger.new(@actor).log_name_change(user.id, user.name, attributes.fetch(:name))
|
|
||||||
elsif attributes[:name].blank? && user.name.present?
|
|
||||||
StaffActionLogger.new(@actor).log_name_change(user.id, user.name, "")
|
|
||||||
end
|
|
||||||
user.name = attributes.fetch(:name) { user.name }
|
|
||||||
|
|
||||||
if attributes.key?(:muted_usernames)
|
if attributes.key?(:muted_usernames)
|
||||||
update_muted_users(attributes[:muted_usernames])
|
update_muted_users(attributes[:muted_usernames])
|
||||||
end
|
end
|
||||||
|
|
||||||
saved = (!save_options || user.user_option.save) && user_profile.save && user.save
|
saved = (!save_options || user.user_option.save) && user_profile.save && user.save
|
||||||
DiscourseEvent.trigger(:user_updated, user) if saved
|
if saved
|
||||||
|
DiscourseEvent.trigger(:user_updated, user)
|
||||||
|
|
||||||
|
# log name changes
|
||||||
|
if attributes[:name].present? && old_user_name.downcase != attributes.fetch(:name).downcase
|
||||||
|
StaffActionLogger.new(@actor).log_name_change(user.id, old_user_name, attributes.fetch(:name))
|
||||||
|
elsif attributes[:name].blank? && old_user_name.present?
|
||||||
|
StaffActionLogger.new(@actor).log_name_change(user.id, old_user_name, "")
|
||||||
|
end
|
||||||
|
end
|
||||||
saved
|
saved
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -192,9 +192,12 @@ describe UserUpdater do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "logs the action" do
|
it "logs the action" do
|
||||||
|
user_without_name = Fabricate(:user, name: nil)
|
||||||
user = Fabricate(:user, name: 'Billy Bob')
|
user = Fabricate(:user, name: 'Billy Bob')
|
||||||
expect { UserUpdater.new(acting_user, user).update(name: 'Jim Tom') }.to change { UserHistory.count }.by(1)
|
expect { UserUpdater.new(acting_user, user).update(name: 'Jim Tom') }.to change { UserHistory.count }.by(1)
|
||||||
expect { UserUpdater.new(acting_user, user).update(name: 'Jim Tom') }.to change { UserHistory.count }.by(0) # make sure it does not log a dupe
|
expect { UserUpdater.new(acting_user, user).update(name: 'Jim Tom') }.to change { UserHistory.count }.by(0) # make sure it does not log a dupe
|
||||||
|
expect { UserUpdater.new(acting_user, user_without_name).update(bio_raw: 'foo bar') }.to change { UserHistory.count }.by(0) # make sure user without name (name = nil) does not raise an error
|
||||||
|
expect { UserUpdater.new(acting_user, user_without_name).update(name: 'Jim Tom') }.to change { UserHistory.count }.by(1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue