FIX: staged users should not be included in TL groups

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.

diff --git a/app/models/group.rb b/app/models/group.rb
index 72d5a3a..4d50dbe 100644
--- a/app/models/group.rb
+++ b/app/models/group.rb
@@ -328,13 +328,13 @@ class Group < ActiveRecord::Base
     remove_subquery =
       case name
       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
-        "SELECT id FROM users WHERE id <= 0 OR NOT moderator"
+        "SELECT id FROM users WHERE id <= 0 OR NOT moderator OR staged"
       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
-        "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
 
     DB.exec <<-SQL
@@ -348,15 +348,15 @@ class Group < ActiveRecord::Base
     insert_subquery =
       case name
       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
-        "SELECT id FROM users WHERE id > 0 AND moderator"
+        "SELECT id FROM users WHERE id > 0 AND moderator AND NOT staged"
       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
-        "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
-        "SELECT id FROM users WHERE id > 0"
+        "SELECT id FROM users WHERE id > 0 AND NOT staged"
       end
 
     DB.exec <<-SQL
diff --git a/app/models/user.rb b/app/models/user.rb
index 5aac249..c90f412 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -131,6 +131,7 @@ class User < ActiveRecord::Base
   after_create :set_default_categories_preferences
 
   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 :ensure_password_is_hashed
@@ -144,6 +145,7 @@ class User < ActiveRecord::Base
   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
 
@@ -1416,6 +1418,13 @@ class User < ActiveRecord::Base
 
   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
     DiscourseEvent.trigger(:user_updated, self)
     true
diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb
index f48d6f9..0625cba 100644
--- a/spec/models/group_spec.rb
+++ b/spec/models/group_spec.rb
@@ -223,6 +223,21 @@ describe Group do
   end
 
   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
       g = Group.refresh_automatic_group!(:everyone)
       expect(g.visibility_level).to eq(Group.visibility_levels[:staff])

GitHub sha: fe4f0a43

1 Like