Skip to content

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

FIX: Don't cache `can_act_on_discourse_post_event?` result across events (#40487)

Renato Atilio4c4a55830004
diff --git a/plugins/discourse-calendar/plugin.rb b/plugins/discourse-calendar/plugin.rb
index 0ae32525c8b..16f9221aab1 100644
--- a/plugins/discourse-calendar/plugin.rb
+++ b/plugins/discourse-calendar/plugin.rb
@@ -254,14 +254,10 @@ after_initialize do
end
add_to_class(:user, :can_act_on_discourse_post_event?) do |event|
- return @can_act_on_discourse_post_event if defined?(@can_act_on_discourse_post_event)
- @can_act_on_discourse_post_event =
- begin
- return true if staff?
- can_create_discourse_post_event? && Guardian.new(self).can_edit_post?(event.post)
- rescue StandardError
- false
- end
+ return true if staff?
+ can_create_discourse_post_event? && Guardian.new(self).can_edit_post?(event.post)
+ rescue StandardError
+ false
end
add_to_class(:guardian, :can_act_on_discourse_post_event?) do |event|
diff --git a/plugins/discourse-calendar/spec/models/discourse_post_event/user_spec.rb b/plugins/discourse-calendar/spec/models/discourse_post_event/user_spec.rb
index bb34ae7a9e3..b396b854c34 100644
--- a/plugins/discourse-calendar/spec/models/discourse_post_event/user_spec.rb
+++ b/plugins/discourse-calendar/spec/models/discourse_post_event/user_spec.rb
@@ -82,6 +82,28 @@ describe User do
expect(user_1.can_act_on_discourse_post_event?(post_event_1)).to eq(true)
end
end
+
+ context "with multiple events in the same request" do
+ let(:user_2) { Fabricate(:user) }
+
+ let(:own_topic) { Fabricate(:topic, user: user_1) }
+ let(:own_post) { Fabricate(:post, topic: own_topic, user: user_1) }
+ let(:own_event) { Fabricate(:event, post: own_post) }
+
+ let(:other_topic) { Fabricate(:topic, user: user_2) }
+ let(:other_post) { Fabricate(:post, topic: other_topic, user: user_2) }
+ let(:other_event) { Fabricate(:event, post: other_post) }
+
+ it "does not leak the answer from one event to another" do
+ expect(user_1.can_act_on_discourse_post_event?(own_event)).to eq(true)
+ expect(user_1.can_act_on_discourse_post_event?(other_event)).to eq(false)
+ end
+
+ it "does not leak a negative answer either" do
+ expect(user_1.can_act_on_discourse_post_event?(other_event)).to eq(false)
+ expect(user_1.can_act_on_discourse_post_event?(own_event)).to eq(true)
+ end
+ end
end
context "when user is not in list of allowed groups" do