FIX: user directory should not include unapproved users

FIX: user directory should not include unapproved users

When the “must approve users” setting is enabled, new users who were waiting to be approved could show up in the user directory.

diff --git a/app/models/directory_item.rb b/app/models/directory_item.rb
index 34b32ee..89f8daf 100644
--- a/app/models/directory_item.rb
+++ b/app/models/directory_item.rb
@@ -65,7 +65,8 @@ class DirectoryItem < ActiveRecord::Base
                     0
                 FROM users u
                 LEFT JOIN directory_items di ON di.user_id = u.id AND di.period_type = :period_type
-                WHERE di.id IS NULL AND u.id > 0 AND u.silenced_till IS NULL and u.active
+                WHERE di.id IS NULL AND u.id > 0 AND u.silenced_till IS NULL AND u.active
+                #{SiteSetting.must_approve_users ? 'AND u.approved' : ''}
       ", period_type: period_types[period_type]
 
       # Calculate new values and update records
diff --git a/spec/models/directory_item_spec.rb b/spec/models/directory_item_spec.rb
index b2465e0..54f8d72 100644
--- a/spec/models/directory_item_spec.rb
+++ b/spec/models/directory_item_spec.rb
@@ -143,5 +143,17 @@ describe DirectoryItem do
       expect(monthly_directory_item.days_visited).to eq(3)
     end
 
+    context 'must approve users' do
+      before do
+        SiteSetting.must_approve_users = true
+      end
+
+      it "doesn't include user who hasn't been approved" do
+        user = Fabricate(:user, approved: false)
+        DirectoryItem.refresh!
+        expect(DirectoryItem.where(user_id: user.id).count).to eq(0)
+      end
+    end
+
   end
 end

GitHub sha: 7331e086

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there: