FIX: Delegate silenced_till from anonymous user to main user account (#32416)

When a user is silenced they can, given they have the permissions, enter anonymous mode and keep posting, essentially bypassing the silence that way.

This change delegates the silenced_till attribute to the main user record if the user is anonymous.
This commit is contained in:
Ted Johansson
2025-04-23 16:34:51 +08:00
committed by GitHub
parent 9f280872f8
commit e19e43f1bd
2 changed files with 24 additions and 0 deletions

View File

@ -1316,6 +1316,10 @@ class User < ActiveRecord::Base
!!(suspended_till && suspended_till > Time.zone.now)
end
def silenced_till
main_user_record[:silenced_till]
end
def silenced?
!!(silenced_till && silenced_till > Time.zone.now)
end
@ -2145,6 +2149,10 @@ class User < ActiveRecord::Base
private
def main_user_record
anonymous? ? master_user : self
end
def set_default_sidebar_section_links(update: false)
return if staged? || bot?

View File

@ -2476,6 +2476,22 @@ RSpec.describe User do
end
end
describe "#silenced_till" do
context "when the user is an anonymous shadow" do
let(:main) { Fabricate(:user, silenced_till: 1.day.from_now) }
let(:anon) { Fabricate(:anonymous) }
before do
SiteSetting.allow_anonymous_mode = true
anon.anonymous_user_master.update(master_user_id: main.id)
end
it "delegates the value from the main user record" do
expect(anon.silenced_till).to eq(main.silenced_till)
end
end
end
describe "silenced?" do
it "is not silenced by default" do
expect(Fabricate(:user)).not_to be_silenced