FIX: Respect unicode whitelist when suggesting username

FIX: Respect unicode whitelist when suggesting username

diff --git a/lib/user_name_suggester.rb b/lib/user_name_suggester.rb
index 38ee8be..d849816 100644
--- a/lib/user_name_suggester.rb
+++ b/lib/user_name_suggester.rb
@@ -115,7 +115,7 @@ module UserNameSuggester
     end
 
     name.gsub!(UsernameValidator.invalid_char_pattern, '_')
-    name.chars.map! { |c| UsernameValidator.whitelisted_char?(c) ? c : '_' } if UsernameValidator.char_whitelist_exists?
+    name = apply_whitelist(name) if UsernameValidator.char_whitelist_exists?
     name.gsub!(UsernameValidator::INVALID_LEADING_CHAR_PATTERN, '')
     name.gsub!(UsernameValidator::CONFUSING_EXTENSIONS, "_")
     name.gsub!(UsernameValidator::INVALID_TRAILING_CHAR_PATTERN, '')
@@ -123,6 +123,12 @@ module UserNameSuggester
     name
   end
 
+  def self.apply_whitelist(name)
+    name.grapheme_clusters
+      .map { |c| UsernameValidator.whitelisted_char?(c) ? c : '_' }
+      .join
+  end
+
   def self.rightsize_username(name)
     name = truncate(name, User.username_length.end)
     name.gsub!(UsernameValidator::INVALID_TRAILING_CHAR_PATTERN, '')
diff --git a/spec/components/user_name_suggester_spec.rb b/spec/components/user_name_suggester_spec.rb
index d319f78..a7e9b19 100644
--- a/spec/components/user_name_suggester_spec.rb
+++ b/spec/components/user_name_suggester_spec.rb
@@ -170,6 +170,17 @@ describe UserNameSuggester do
         expect(UserNameSuggester.suggest('য়া-য়া-য়া-য়া-য়া-য়া-য়া-য়া-য়া-য়া-য়া-য়া-য়া-য়া-য়া-য়া-য়া-য়া-য়া'))
           .to eq('য়া-য়া-য়া-য়া-য়া-য়া-য়া-য়া-য়া-য়া-য়া-য়া-য়া-য়া-য়া')
       end
+
+      it "uses whitelist" do
+        SiteSetting.unicode_username_character_whitelist = "[äöüßÄÖÜẞ]"
+
+        expect(UserNameSuggester.suggest('πουλί')).to eq('111')
+        expect(UserNameSuggester.suggest('a鳥b')).to eq('a_b')
+        expect(UserNameSuggester.suggest('Löwe')).to eq('Löwe')
+
+        SiteSetting.unicode_username_character_whitelist = "[য়া]"
+        expect(UserNameSuggester.suggest('aয়াb鳥c')).to eq('aয়াb_c')
+      end
     end
   end
 end

GitHub sha: 9e4fb262

1 Like