FIX: Mods weren't able to see emails in admin user list

FIX: Mods weren’t able to see emails in admin user list

diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb
index 20dafe4..8f0afd9 100644
--- a/app/controllers/admin/users_controller.rb
+++ b/app/controllers/admin/users_controller.rb
@@ -34,7 +34,7 @@ class Admin::UsersController < Admin::AdminController
 
     opts = {}
     if params[:show_emails] == "true"
-      StaffActionLogger.new(current_user).log_show_emails(users)
+      StaffActionLogger.new(current_user).log_show_emails(users, context: request.path)
       opts[:emails_desired] = true
     end
 
diff --git a/app/serializers/admin_user_list_serializer.rb b/app/serializers/admin_user_list_serializer.rb
index 81d953a..4bf2c17 100644
--- a/app/serializers/admin_user_list_serializer.rb
+++ b/app/serializers/admin_user_list_serializer.rb
@@ -39,7 +39,7 @@ class AdminUserListSerializer < BasicUserSerializer
   def include_email?
     # staff members can always see their email
     (scope.is_staff? && (object.id == scope.user.id || object.staged?)) ||
-      (scope.is_admin? && @options[:emails_desired])
+      (@options[:emails_desired] && scope.can_check_emails?(object))
   end
 
   alias_method :include_secondary_emails?, :include_email?
diff --git a/spec/serializers/admin_user_list_serializer_spec.rb b/spec/serializers/admin_user_list_serializer_spec.rb
index 60c7dac..7f50c37 100644
--- a/spec/serializers/admin_user_list_serializer_spec.rb
+++ b/spec/serializers/admin_user_list_serializer_spec.rb
@@ -41,13 +41,22 @@ describe AdminUserListSerializer do
       expect(json[:secondary_emails]).to eq(nil)
     end
 
-    it "doesn't return emails for a moderator request" do
+    it "doesn't return emails for a moderator request when show_email_on_profile is disabled" do
+      SiteSetting.show_email_on_profile = false
       fabricate_secondary_emails_for(user)
       json = serialize(user, moderator, emails_desired: true)
       expect(json[:email]).to eq(nil)
       expect(json[:secondary_emails]).to eq(nil)
     end
 
+    it "returns emails for a moderator request when show_email_on_profile is enabled" do
+      SiteSetting.show_email_on_profile = true
+      fabricate_secondary_emails_for(user)
+      json = serialize(user, moderator, emails_desired: true)
+      expect(json[:email]).to eq("user@email.com")
+      expect(json[:secondary_emails]).to contain_exactly("first@email.com", "second@email.com")
+    end
+
     it "returns emails for admins when emails_desired is true" do
       fabricate_secondary_emails_for(user)
       json = serialize(user, admin, emails_desired: true)

GitHub sha: 457e6c7b

1 Like

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