diff --git a/app/models/discourse_single_sign_on.rb b/app/models/discourse_single_sign_on.rb index 83c3d431f..b4582172c 100644 --- a/app/models/discourse_single_sign_on.rb +++ b/app/models/discourse_single_sign_on.rb @@ -115,9 +115,8 @@ class DiscourseSingleSignOn < SingleSignOn end if SiteSetting.sso_overrides_username && - user.username != username && - user.username != UserNameSuggester.fix_username(username || name || email) - user.username = UserNameSuggester.suggest(username || name || email) + user.username != username + user.username = UserNameSuggester.suggest(username || name || email, user.username) end if SiteSetting.sso_overrides_name && user.name != name diff --git a/lib/user_name_suggester.rb b/lib/user_name_suggester.rb index 3da14fdb8..09e4db698 100644 --- a/lib/user_name_suggester.rb +++ b/lib/user_name_suggester.rb @@ -1,9 +1,9 @@ module UserNameSuggester - def self.suggest(name) + def self.suggest(name, allow_username = nil) return unless name.present? name = parse_name_from_email(name) - find_available_username_based_on(name) + find_available_username_based_on(name, allow_username) end def self.parse_name_from_email(name) @@ -16,11 +16,11 @@ module UserNameSuggester name end - def self.find_available_username_based_on(name) + def self.find_available_username_based_on(name, allow_username = nil) name = fix_username(name) i = 1 attempt = name - until User.username_available?(attempt) + until attempt == allow_username || User.username_available?(attempt) suffix = i.to_s max_length = User.username_length.end - suffix.length - 1 attempt = "#{name[0..max_length]}#{suffix}"