mirror of
https://github.com/discourse/discourse.git
synced 2025-03-23 22:58:53 +08:00
FIX: Do not suggest similar topics from secure categories you can't see.
This commit is contained in:
parent
2c68dd1c12
commit
77b218a142
@ -84,7 +84,7 @@ class TopicsController < ApplicationController
|
|||||||
raise Discourse::InvalidParameters.new(:title) if title.length < SiteSetting.min_title_similar_length
|
raise Discourse::InvalidParameters.new(:title) if title.length < SiteSetting.min_title_similar_length
|
||||||
raise Discourse::InvalidParameters.new(:raw) if raw.length < SiteSetting.min_body_similar_length
|
raise Discourse::InvalidParameters.new(:raw) if raw.length < SiteSetting.min_body_similar_length
|
||||||
|
|
||||||
topics = Topic.similar_to(title, raw)
|
topics = Topic.similar_to(title, raw, current_user)
|
||||||
render_serialized(topics, BasicTopicSerializer)
|
render_serialized(topics, BasicTopicSerializer)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -98,16 +98,19 @@ class Topic < ActiveRecord::Base
|
|||||||
|
|
||||||
scope :secured, lambda {|guardian|
|
scope :secured, lambda {|guardian|
|
||||||
ids = guardian.secure_category_ids if guardian
|
ids = guardian.secure_category_ids if guardian
|
||||||
|
|
||||||
|
# Query conditions
|
||||||
condition =
|
condition =
|
||||||
if ids.present?
|
if ids.present?
|
||||||
["NOT c.secure or c.id in (:cats)", cats: ids]
|
["NOT c.secure or c.id in (:cats)", cats: ids]
|
||||||
else
|
else
|
||||||
["NOT c.secure"]
|
["NOT c.secure"]
|
||||||
end
|
end
|
||||||
where("category_id IS NULL OR category_id IN (
|
|
||||||
SELECT c.id FROM categories c
|
where("category_id IS NULL OR category_id IN (
|
||||||
WHERE #{condition[0]})", condition[1])
|
SELECT c.id FROM categories c
|
||||||
}
|
WHERE #{condition[0]})", condition[1])
|
||||||
|
}
|
||||||
|
|
||||||
# Helps us limit how many favorites can be made in a day
|
# Helps us limit how many favorites can be made in a day
|
||||||
class FavoriteLimiter < RateLimiter
|
class FavoriteLimiter < RateLimiter
|
||||||
@ -234,7 +237,7 @@ class Topic < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Search for similar topics
|
# Search for similar topics
|
||||||
def self.similar_to(title, raw)
|
def self.similar_to(title, raw, user=nil)
|
||||||
return [] unless title.present?
|
return [] unless title.present?
|
||||||
return [] unless raw.present?
|
return [] unless raw.present?
|
||||||
|
|
||||||
@ -242,6 +245,7 @@ class Topic < ActiveRecord::Base
|
|||||||
Topic.select(sanitize_sql_array(["topics.*, similarity(topics.title, :title) AS similarity", title: title]))
|
Topic.select(sanitize_sql_array(["topics.*, similarity(topics.title, :title) AS similarity", title: title]))
|
||||||
.visible
|
.visible
|
||||||
.where(closed: false, archived: false)
|
.where(closed: false, archived: false)
|
||||||
|
.secured(Guardian.new(user))
|
||||||
.listable_topics
|
.listable_topics
|
||||||
.limit(SiteSetting.max_similar_results)
|
.limit(SiteSetting.max_similar_results)
|
||||||
.order('similarity desc')
|
.order('similarity desc')
|
||||||
|
@ -162,10 +162,19 @@ describe TopicsController do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "delegates to Topic.similar_to" do
|
it "delegates to Topic.similar_to" do
|
||||||
Topic.expects(:similar_to).with(title, raw).returns([Fabricate(:topic)])
|
Topic.expects(:similar_to).with(title, raw, nil).returns([Fabricate(:topic)])
|
||||||
xhr :get, :similar_to, title: title, raw: raw
|
xhr :get, :similar_to, title: title, raw: raw
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "logged in" do
|
||||||
|
let(:user) { log_in }
|
||||||
|
|
||||||
|
it "passes a user throught if logged in" do
|
||||||
|
Topic.expects(:similar_to).with(title, raw, user).returns([Fabricate(:topic)])
|
||||||
|
xhr :get, :similar_to, title: title, raw: raw
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -175,6 +175,26 @@ describe Topic do
|
|||||||
Topic.similar_to("has evil trout made any topics?", "i am wondering has evil trout made any topics?").should == [topic]
|
Topic.similar_to("has evil trout made any topics?", "i am wondering has evil trout made any topics?").should == [topic]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "secure categories" do
|
||||||
|
|
||||||
|
let(:user) { Fabricate(:user) }
|
||||||
|
let(:category) { Fabricate(:category, secure: true) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
topic.category = category
|
||||||
|
topic.save
|
||||||
|
end
|
||||||
|
|
||||||
|
it "doesn't return topics from private categories" do
|
||||||
|
expect(Topic.similar_to("has evil trout made any topics?", "i am wondering has evil trout made any topics?", user)).to be_blank
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return the cat since the user can see it" do
|
||||||
|
Guardian.any_instance.expects(:secure_category_ids).returns([category.id])
|
||||||
|
expect(Topic.similar_to("has evil trout made any topics?", "i am wondering has evil trout made any topics?", user)).to include(topic)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user