A platform for community discussion. Free, open, simple.
FIX: Reuse topic visibility rules for review authorization
Régis Hanol9788ac1b8f5c
diff --git a/lib/guardian/topic_guardian.rb b/lib/guardian/topic_guardian.rbindex 68a19003fa0..4e3cc2fef23 100644--- a/lib/guardian/topic_guardian.rb+++ b/lib/guardian/topic_guardian.rb@@ -221,43 +221,29 @@ module TopicGuardianreturn topic_ids if is_admin? && !SiteSetting.suppress_secured_categories_from_adminreturn [] if topic_ids.blank?- default_scope = Topic.unscoped.where(id: topic_ids)+ visible_topic_scope(+ Topic.unscoped.where(id: topic_ids),+ deleted: hide_deleted ? :visible : :include,+ ).pluck(:id)+ end++ def visible_topic_scope(scope, deleted: :visible)+ return scope if is_admin? && !SiteSetting.suppress_secured_categories_from_admin- # When `hide_deleted` is `true`, hide deleted topics if user is not staff or category moderator- if hide_deleted && !is_staff?+ if deleted == :visible && !is_staff?+ visible = scope.where(deleted_at: nil)if category_group_moderation_allowed?- default_scope = default_scope.where(<<~SQL)- (- deleted_at IS NULL OR- (- deleted_at IS NOT NULL- AND topics.category_id IN (#{category_group_moderator_scope.select(:id).to_sql})- )- )- SQL- else- default_scope = default_scope.where("deleted_at IS NULL")+ visible = visible.or(scope.where(category_id: category_group_moderator_scope.select(:id)))end+ scope = visibleend- # Filter out topics with shared drafts if user cannot see shared drafts- if !can_see_shared_draft?- default_scope =- default_scope.left_outer_joins(:shared_draft).where("shared_drafts.id IS NULL")- end+ scope = scope.where.missing(:shared_draft) if !can_see_shared_draft?- all_topics_scope =- if authenticated?- Topic.unscoped.merge(- secured_regular_topic_scope(default_scope, topic_ids: topic_ids).or(- private_message_topic_scope(default_scope),- ),- )- else- Topic.unscoped.merge(secured_regular_topic_scope(default_scope, topic_ids: topic_ids))- end+ regular_topics = secured_regular_topic_scope(scope)+ return regular_topics if !authenticated?- all_topics_scope.pluck(:id)+ regular_topics.or(private_message_topic_scope(scope.private_messages))enddef can_see_topic?(topic, hide_deleted = true)@@ -384,7 +370,7 @@ module TopicGuardiantopic.posts_count <= 1 && topic.created_at? && topic.created_at > 24.hours.agoend- def secured_regular_topic_scope(scope, topic_ids:)+ def secured_regular_topic_scope(scope)secured_scope = Topic.unscoped.secured(self)# Staged users are allowed to see their own topics in read restricted categories when Category#email_in and@@ -400,12 +386,10 @@ module TopicGuardianAND categories.email_in IS NOT NULLAND categories.email_in_allow_strangersAND topics.user_id = :user_id- AND topics.id IN (:topic_ids))SQL- secured_scope =- secured_scope.or(Topic.unscoped.where(sql, user_id: user.id, topic_ids: topic_ids))+ secured_scope = secured_scope.or(Topic.unscoped.where(sql, user_id: user.id))endscope.listable_topics.merge(secured_scope)diff --git a/spec/lib/guardian/topic_guardian_spec.rb b/spec/lib/guardian/topic_guardian_spec.rbindex d476851c272..e28bf3cd60d 100644--- a/spec/lib/guardian/topic_guardian_spec.rb+++ b/spec/lib/guardian/topic_guardian_spec.rb@@ -424,6 +424,12 @@ RSpec.describe TopicGuardian do# `TopicGuardianCanSeeConsistencyCheck` which we run to ensure that the implementation between `TopicGuardian#can_see_topic_ids`# and `TopicGuardian#can_see_topic?` is consistent.describe "#can_see_topic_ids" do+ it "excludes flagged regular topics in inaccessible categories" do+ Fabricate(:reviewable_flagged_post, topic: private_topic, category: private_category)++ expect(moderator.guardian.can_see_topic_ids(topic_ids: [private_topic.id])).to be_empty+ end+it "returns the topic ids for the topics which a user is allowed to see" doexpect(Guardian.new.can_see_topic_ids(topic_ids: [topic.id, private_message_topic.id]),