FIX: clear the site_contact_username setting if the user's staff privileges are revoked

This commit is contained in:
Neil Lalonde 2018-12-14 16:52:37 -05:00
parent 0d3c1cde90
commit ef0e84e3d9
2 changed files with 50 additions and 0 deletions

View File

@ -122,6 +122,7 @@ class User < ActiveRecord::Base
after_save :badge_grant after_save :badge_grant
after_save :expire_old_email_tokens after_save :expire_old_email_tokens
after_save :index_search after_save :index_search
after_save :check_site_contact_username
after_commit :trigger_user_created_event, on: :create after_commit :trigger_user_created_event, on: :create
after_commit :trigger_user_destroyed_event, on: :destroy after_commit :trigger_user_destroyed_event, on: :destroy
@ -1369,6 +1370,13 @@ class User < ActiveRecord::Base
true true
end end
def check_site_contact_username
if (saved_change_to_admin? || saved_change_to_moderator?) &&
self.username == SiteSetting.site_contact_username && !staff?
SiteSetting.set_and_log(:site_contact_username, SiteSetting.defaults[:site_contact_username])
end
end
def self.ensure_consistency! def self.ensure_consistency!
DB.exec <<~SQL DB.exec <<~SQL
UPDATE users UPDATE users

View File

@ -1924,4 +1924,46 @@ describe User do
expect(user.next_best_title).to eq(nil) expect(user.next_best_title).to eq(nil)
end end
end end
describe 'check_site_contact_username' do
before { SiteSetting.site_contact_username = contact_user.username }
context 'admin' do
let(:contact_user) { Fabricate(:admin) }
it 'clears site_contact_username site setting when admin privilege is revoked' do
contact_user.revoke_admin!
expect(SiteSetting.site_contact_username).to eq(SiteSetting.defaults[:site_contact_username])
end
end
context 'moderator' do
let(:contact_user) { Fabricate(:moderator) }
it 'clears site_contact_username site setting when moderator privilege is revoked' do
contact_user.revoke_moderation!
expect(SiteSetting.site_contact_username).to eq(SiteSetting.defaults[:site_contact_username])
end
end
context 'admin and moderator' do
let(:contact_user) { Fabricate(:moderator, admin: true) }
it 'does not change site_contact_username site setting when admin privilege is revoked' do
contact_user.revoke_admin!
expect(SiteSetting.site_contact_username).to eq(contact_user.username)
end
it 'does not change site_contact_username site setting when moderator privilege is revoked' do
contact_user.revoke_moderation!
expect(SiteSetting.site_contact_username).to eq(contact_user.username)
end
it 'clears site_contact_username site setting when staff privileges are revoked' do
contact_user.revoke_admin!
contact_user.revoke_moderation!
expect(SiteSetting.site_contact_username).to eq(SiteSetting.defaults[:site_contact_username])
end
end
end
end end