mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 19:00:32 -06:00
PERF: N+1 queries admin users pages.
This commit is contained in:
parent
d6d4a5ba4a
commit
c5808a8a25
@ -69,6 +69,11 @@ class User < ActiveRecord::Base
|
||||
has_many :oauth2_user_infos, dependent: :destroy
|
||||
has_one :instagram_user_info, dependent: :destroy
|
||||
has_many :user_second_factors, dependent: :destroy
|
||||
|
||||
has_many :totps, -> {
|
||||
where(method: UserSecondFactor.methods[:totp], enabled: true)
|
||||
}, class_name: "UserSecondFactor"
|
||||
|
||||
has_one :user_stat, dependent: :destroy
|
||||
has_one :user_profile, dependent: :destroy, inverse_of: :user
|
||||
has_one :single_sign_on_record, dependent: :destroy
|
||||
|
@ -119,7 +119,9 @@ class AdminUserListSerializer < BasicUserSerializer
|
||||
end
|
||||
|
||||
def include_second_factor_enabled?
|
||||
object.totp_enabled?
|
||||
!SiteSetting.enable_sso &&
|
||||
SiteSetting.enable_local_logins &&
|
||||
object.totps.present?
|
||||
end
|
||||
|
||||
def second_factor_enabled
|
||||
|
@ -60,12 +60,15 @@ class AdminUserIndexQuery
|
||||
order << "users.username"
|
||||
end
|
||||
|
||||
if params[:stats].present? && params[:stats] == false
|
||||
klass.order(order.reject(&:blank?).join(","))
|
||||
else
|
||||
klass.includes(:user_stat, :user_second_factors)
|
||||
query = klass
|
||||
.includes(:totps)
|
||||
.order(order.reject(&:blank?).join(","))
|
||||
|
||||
unless params[:stats].present? && params[:stats] == false
|
||||
query = query.includes(:user_stat)
|
||||
end
|
||||
|
||||
query
|
||||
end
|
||||
|
||||
def filter_by_trust
|
||||
|
@ -2,17 +2,25 @@ require 'rails_helper'
|
||||
require_dependency 'user'
|
||||
|
||||
describe AdminUserListSerializer do
|
||||
|
||||
context "emails" do
|
||||
let(:admin) { Fabricate(:user_single_email, admin: true, email: "admin@email.com") }
|
||||
let(:moderator) { Fabricate(:user_single_email, moderator: true, email: "moderator@email.com") }
|
||||
let(:user) { Fabricate(:user_single_email, email: "user@email.com") }
|
||||
let(:user) { Fabricate(:user_second_factor_totp).user }
|
||||
let(:admin) { Fabricate(:admin) }
|
||||
let(:guardian) { Guardian.new(admin) }
|
||||
|
||||
let(:serializer) do
|
||||
AdminUserListSerializer.new(user, scope: guardian, root: false)
|
||||
end
|
||||
|
||||
it "returns the right values when user has second factor totp enabled" do
|
||||
json = serializer.as_json
|
||||
|
||||
expect(json[:second_factor_enabled]).to eq(true)
|
||||
end
|
||||
|
||||
context "emails" do
|
||||
let(:admin) { Fabricate(:user_single_email, admin: true, email: "admin@email.com") }
|
||||
let(:moderator) { Fabricate(:user_single_email, moderator: true, email: "moderator@email.com") }
|
||||
let(:user) { Fabricate(:user_single_email, email: "user@email.com") }
|
||||
|
||||
def serialize(user, viewed_by, opts = nil)
|
||||
AdminUserListSerializer.new(
|
||||
user,
|
||||
|
Loading…
Reference in New Issue
Block a user