Skip to content

Commit

Permalink
FIX: clear the site_contact_username setting if the user's staff priv…
Browse files Browse the repository at this point in the history
…ileges are revoked
  • Loading branch information
nlalonde committed Dec 14, 2018
1 parent 0d3c1cd commit ef0e84e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class User < ActiveRecord::Base
after_save :badge_grant
after_save :expire_old_email_tokens
after_save :index_search
after_save :check_site_contact_username
after_commit :trigger_user_created_event, on: :create
after_commit :trigger_user_destroyed_event, on: :destroy

Expand Down Expand Up @@ -1369,6 +1370,13 @@ def set_skip_validate_email
true
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!
DB.exec <<~SQL
UPDATE users
Expand Down
42 changes: 42 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1924,4 +1924,46 @@ def filter_by(method)
expect(user.next_best_title).to eq(nil)
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

0 comments on commit ef0e84e

Please sign in to comment.