Skip to content

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

FIX: Correctly handle deletion of anonymous master_user (#37983)

David Taylorbba215f0c7d3
diff --git a/app/models/user.rb b/app/models/user.rb
index 54ff9f51540..a49128ca482 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -2197,7 +2197,7 @@ class User < ActiveRecord::Base
private
def main_user_record
- anonymous? ? master_user : self
+ (anonymous? && master_user) ? master_user : self
end
def set_default_sidebar_section_links(update: false)
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index 53719a58c3a..9101de6579b 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -2544,6 +2544,18 @@ RSpec.describe User do
it "delegates the value from the main user record" do
expect(anon.silenced_till).to be_within(1.second).of(main.silenced_till)
end
+
+ it "returns nil when the master user has been destroyed" do
+ main.destroy!
+ anon.reload
+ expect(anon.silenced_till).to be_nil
+ end
+
+ it "returns nil when the master user has been deleted without hooks" do
+ User.where(id: main.id).delete_all
+ anon.reload
+ expect(anon.silenced_till).to be_nil
+ end
end
end