Skip to content

A platform for community discussion. Free, open, simple.

FIX: Exclude user names from topic search when enable_names is disabled (#38011)

Alan Guo Xiang Tan26fa64aa5363
diff --git a/lib/search.rb b/lib/search.rb
index 245b22fcbe0..b9f37f53b48 100644
--- a/lib/search.rb
+++ b/lib/search.rb
@@ -1270,11 +1270,9 @@ class Search
term_without_quote = $1 if @term =~ /'(.+)'/
posts = posts.joins("JOIN users u ON u.id = posts.user_id")
- posts =
- posts.where(
- "posts.raw || ' ' || post_search_data.raw_data || ' ' || u.username || ' ' || COALESCE(u.name, '') ILIKE ?",
- "%#{term_without_quote}%",
- )
+ user_search_fields = +"posts.raw || ' ' || post_search_data.raw_data || ' ' || u.username"
+ user_search_fields << " || ' ' || COALESCE(u.name, '')" if SiteSetting.enable_names
+ posts = posts.where("#{user_search_fields} ILIKE ?", "%#{term_without_quote}%")
else
posts = posts.where(post_number: 1) if @in_title
posts = posts.where("post_search_data.search_data @@ #{ts_query(weight_filter: weights)}")
diff --git a/spec/lib/search_spec.rb b/spec/lib/search_spec.rb
index c862a610452..fd51990dd3d 100644
--- a/spec/lib/search_spec.rb
+++ b/spec/lib/search_spec.rb
@@ -1420,6 +1420,24 @@ RSpec.describe Search do
results = Search.execute("Example Site Title", search_context: post.topic)
expect(results.posts.map(&:id)).to eq([post.id])
end
+
+ describe "searching for author's real name" do
+ before { topic.user.update!(name: "Jane Searcher") }
+
+ it "does not find posts when the enable_names site setting is disabled" do
+ SiteSetting.enable_names = false
+
+ results = Search.execute("Jane Searcher", search_context: topic)
+ expect(results.posts).to be_empty
+ end
+
+ it "finds posts when the enable_names site setting is enabled" do
+ SiteSetting.enable_names = true
+
+ results = Search.execute("Jane Searcher", search_context: topic)
+ expect(results.posts.map(&:id)).to include(post.id)
+ end
+ end
end
context "when searching the OP" do