FIX: raise if staged user creation failed (#6461)

This commit is contained in:
Kyle Zhao 2018-10-08 15:45:23 +08:00 committed by Régis Hanol
parent 3c0ec317ac
commit 9bbc1ae7b2
1 changed files with 9 additions and 4 deletions

View File

@ -144,7 +144,7 @@ module Email
# Lets create a staged user if there isn't one yet. We will try to # Lets create a staged user if there isn't one yet. We will try to
# delete staged users in process!() if something bad happens. # delete staged users in process!() if something bad happens.
if user.nil? if user.nil?
user = find_or_create_user(@from_email, @from_display_name) user = find_or_create_user!(@from_email, @from_display_name)
log_and_validate_user(user) log_and_validate_user(user)
end end
@ -489,7 +489,7 @@ module Email
User.find_by_email(email) User.find_by_email(email)
end end
def find_or_create_user(email, display_name) def find_or_create_user(email, display_name, raise_on_failed_create: false)
user = nil user = nil
User.transaction do User.transaction do
@ -498,8 +498,8 @@ module Email
if user.nil? && SiteSetting.enable_staged_users if user.nil? && SiteSetting.enable_staged_users
raise EmailNotAllowed unless EmailValidator.allowed?(email) raise EmailNotAllowed unless EmailValidator.allowed?(email)
begin
username = UserNameSuggester.sanitize_username(display_name) if display_name.present? username = UserNameSuggester.sanitize_username(display_name) if display_name.present?
begin
user = User.create!( user = User.create!(
email: email, email: email,
username: UserNameSuggester.suggest(username.presence || email), username: UserNameSuggester.suggest(username.presence || email),
@ -507,7 +507,8 @@ module Email
staged: true staged: true
) )
@staged_users << user @staged_users << user
rescue rescue PG::UniqueViolation, ActiveRecord::RecordNotUnique, ActiveRecord::RecordInvalid
raise if raise_on_failed_create
user = nil user = nil
end end
end end
@ -516,6 +517,10 @@ module Email
user user
end end
def find_or_create_user!(email, display_name)
find_or_create_user(email, display_name, raise_on_failed_create: true)
end
def all_destinations def all_destinations
@all_destinations ||= [ @all_destinations ||= [
@mail.destinations, @mail.destinations,