From 1f88ecf6d85347fdbd1114c5447947f690d0eaef Mon Sep 17 00:00:00 2001 From: David Taylor Date: Thu, 31 Oct 2019 15:16:26 +0000 Subject: [PATCH] FIX: Cache `Discourse.system_user` separately for each multisite tenant (#8276) This was not causing any known issue, because the system user ID is always the same across all sites. However, we should cache this on a per-site basis to be safe. --- lib/discourse.rb | 4 +++- spec/components/discourse_spec.rb | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/discourse.rb b/lib/discourse.rb index b7b65762fb1..561a4e979fa 100644 --- a/lib/discourse.rb +++ b/lib/discourse.rb @@ -536,7 +536,9 @@ module Discourse SYSTEM_USER_ID ||= -1 def self.system_user - @system_user ||= User.find_by(id: SYSTEM_USER_ID) + @system_users ||= {} + current_db = RailsMultisite::ConnectionManagement.current_db + @system_users[current_db] ||= User.find_by(id: SYSTEM_USER_ID) end def self.store diff --git a/spec/components/discourse_spec.rb b/spec/components/discourse_spec.rb index 76a832c6d08..70559a5038d 100644 --- a/spec/components/discourse_spec.rb +++ b/spec/components/discourse_spec.rb @@ -130,6 +130,12 @@ describe Discourse do end + context '#system_user' do + it 'returns the system user' do + expect(Discourse.system_user.id).to eq(-1) + end + end + context "#store" do it "returns LocalStore by default" do