FIX: staged users should not be included in TL groups

staged users should not be included in any automatic groups cause for all
purposes they do not exist.
This commit is contained in:
Sam Saffron 2019-06-17 15:10:47 +10:00
parent 32cd9ba59b
commit fe4f0a4369
3 changed files with 33 additions and 9 deletions

View File

@ -328,13 +328,13 @@ class Group < ActiveRecord::Base
remove_subquery = remove_subquery =
case name case name
when :admins when :admins
"SELECT id FROM users WHERE id <= 0 OR NOT admin" "SELECT id FROM users WHERE id <= 0 OR NOT admin OR staged"
when :moderators when :moderators
"SELECT id FROM users WHERE id <= 0 OR NOT moderator" "SELECT id FROM users WHERE id <= 0 OR NOT moderator OR staged"
when :staff when :staff
"SELECT id FROM users WHERE id <= 0 OR (NOT admin AND NOT moderator)" "SELECT id FROM users WHERE id <= 0 OR (NOT admin AND NOT moderator) OR staged"
when :trust_level_0, :trust_level_1, :trust_level_2, :trust_level_3, :trust_level_4 when :trust_level_0, :trust_level_1, :trust_level_2, :trust_level_3, :trust_level_4
"SELECT id FROM users WHERE id <= 0 OR trust_level < #{id - 10}" "SELECT id FROM users WHERE id <= 0 OR trust_level < #{id - 10} OR staged"
end end
DB.exec <<-SQL DB.exec <<-SQL
@ -348,15 +348,15 @@ class Group < ActiveRecord::Base
insert_subquery = insert_subquery =
case name case name
when :admins when :admins
"SELECT id FROM users WHERE id > 0 AND admin" "SELECT id FROM users WHERE id > 0 AND admin AND NOT staged"
when :moderators when :moderators
"SELECT id FROM users WHERE id > 0 AND moderator" "SELECT id FROM users WHERE id > 0 AND moderator AND NOT staged"
when :staff when :staff
"SELECT id FROM users WHERE id > 0 AND (moderator OR admin)" "SELECT id FROM users WHERE id > 0 AND (moderator OR admin) AND NOT staged"
when :trust_level_1, :trust_level_2, :trust_level_3, :trust_level_4 when :trust_level_1, :trust_level_2, :trust_level_3, :trust_level_4
"SELECT id FROM users WHERE id > 0 AND trust_level >= #{id - 10}" "SELECT id FROM users WHERE id > 0 AND trust_level >= #{id - 10} AND NOT staged"
when :trust_level_0 when :trust_level_0
"SELECT id FROM users WHERE id > 0" "SELECT id FROM users WHERE id > 0 AND NOT staged"
end end
DB.exec <<-SQL DB.exec <<-SQL

View File

@ -131,6 +131,7 @@ class User < ActiveRecord::Base
after_create :set_default_categories_preferences after_create :set_default_categories_preferences
after_update :trigger_user_updated_event, if: :saved_change_to_uploaded_avatar_id? after_update :trigger_user_updated_event, if: :saved_change_to_uploaded_avatar_id?
after_update :trigger_user_automatic_group_refresh, if: :saved_change_to_staged?
before_save :update_usernames before_save :update_usernames
before_save :ensure_password_is_hashed before_save :ensure_password_is_hashed
@ -144,6 +145,7 @@ class User < ActiveRecord::Base
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_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
@ -1416,6 +1418,13 @@ class User < ActiveRecord::Base
private private
def trigger_user_automatic_group_refresh
if !staged
Group.user_trust_level_change!(id, trust_level)
end
true
end
def trigger_user_updated_event def trigger_user_updated_event
DiscourseEvent.trigger(:user_updated, self) DiscourseEvent.trigger(:user_updated, self)
true true

View File

@ -223,6 +223,21 @@ describe Group do
end end
describe '.refresh_automatic_group!' do describe '.refresh_automatic_group!' do
it "does not include staged users in any automatic groups" do
staged = Fabricate(:staged, trust_level: 1)
Group.refresh_automatic_group!(:trust_level_0)
Group.refresh_automatic_group!(:trust_level_1)
expect(GroupUser.where(user_id: staged.id).count).to eq(0)
staged.unstage
staged.save!
expect(GroupUser.where(user_id: staged.id).count).to eq(2)
end
it "makes sure the everyone group is not visible except to staff" do it "makes sure the everyone group is not visible except to staff" do
g = Group.refresh_automatic_group!(:everyone) g = Group.refresh_automatic_group!(:everyone)
expect(g.visibility_level).to eq(Group.visibility_levels[:staff]) expect(g.visibility_level).to eq(Group.visibility_levels[:staff])