From ff53f2c7bcdf947186b83a082594516dee4dc304 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 4 Aug 2022 08:43:19 +1000 Subject: [PATCH] FIX: scope memoization of category moderation per category (#17786) --- lib/guardian.rb | 4 ++-- spec/lib/guardian_spec.rb | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/guardian.rb b/lib/guardian.rb index a285656725d..d92db39807f 100644 --- a/lib/guardian.rb +++ b/lib/guardian.rb @@ -112,8 +112,8 @@ class Guardian def is_category_group_moderator?(category) return false unless category return false unless authenticated? - - @is_category_group_moderator ||= begin + @is_category_group_moderator ||= {} + @is_category_group_moderator[category.id] ||= begin SiteSetting.enable_category_group_moderation? && category.present? && category.reviewable_by_group_id.present? && diff --git a/spec/lib/guardian_spec.rb b/spec/lib/guardian_spec.rb index 833a19e80ca..f07f80b96a7 100644 --- a/spec/lib/guardian_spec.rb +++ b/spec/lib/guardian_spec.rb @@ -3953,4 +3953,21 @@ RSpec.describe Guardian do expect(admin.guardian.can_mention_here?).to eq(true) end end + + describe "#is_category_group_moderator" do + before do + SiteSetting.enable_category_group_moderation = true + end + + fab!(:category) { Fabricate(:category) } + + it "should correctly detect category moderation" do + group.add(user) + category.reviewable_by_group_id = group.id + guardian = Guardian.new(user) + + expect(guardian.is_category_group_moderator?(category)).to eq(true) + expect(guardian.is_category_group_moderator?(plain_category)).to eq(false) + end + end end