Skip to content

A platform for community discussion. Free, open, simple.

UX: Add admin report for associated accounts by login provider (#34187)

Penar Musaraj33f8438bd687
diff --git a/app/models/concerns/reports/associated_accounts_by_provider.rb b/app/models/concerns/reports/associated_accounts_by_provider.rb
new file mode 100644
index 00000000000..ba105e630f4
--- /dev/null
+++ b/app/models/concerns/reports/associated_accounts_by_provider.rb
@@ -0,0 +1,74 @@
+# frozen_string_literal: true
+
+module Reports::AssociatedAccountsByProvider
+ extend ActiveSupport::Concern
+
+ class_methods do
+ def report_associated_accounts_by_provider(report)
+ report.data = []
+ report.modes = [Report::MODES[:table]]
+
+ report.dates_filtering = false
+
+ report.labels = [
+ {
+ property: :provider,
+ title: I18n.t("reports.associated_accounts_by_provider.labels.provider"),
+ },
+ { property: :count, type: :number, title: I18n.t("reports.default.labels.count") },
+ ]
+
+ enabled_authenticators = Discourse.enabled_authenticators.map(&:name)
+
+ if !enabled_authenticators.empty?
+ query =
+ UserAssociatedAccount
+ .joins(:user)
+ .where("provider_name IN (?)", enabled_authenticators)
+ .where(users: { active: true })
+ .group(:provider_name)
+ .count
+
+ # Add all enabled authenticators, including those with zero counts
+ enabled_authenticators.each do |provider_name|
+ count = query[provider_name] || 0
+
+ report.data << {
+ key: provider_name,
+ provider: I18n.t("js.login.#{provider_name}.name"),
+ count: count,
+ }
+ end
+ end
+
+ # Add total users count
+ total_users = Statistics.users[:count]
+ report.data << {
+ key: "total_users",
+ provider: I18n.t("reports.associated_accounts_by_provider.labels.total_users"),
+ count: total_users,
+ }
+
+ # Add users with no associated accounts count (only considering enabled providers)
+ users_with_accounts =
+ if !enabled_authenticators.empty?
+ UserAssociatedAccount
+ .joins(:user)
+ .where("provider_name IN (?)", enabled_authenticators)
+ .where(users: { active: true })
+ .distinct
+ .count(:user_id)
+ else
+ 0
+ end
+ users_without_accounts = total_users - users_with_accounts
+ report.data << {
+ key: "no_accounts",
+ provider: I18n.t("reports.associated_accounts_by_provider.labels.no_accounts"),
+ count: users_without_accounts,
+ }
+
+ report.data.sort_by! { |row| -row[:count] }
+ end
+ end
+end
diff --git a/app/models/report.rb b/app/models/report.rb
index 0330d2b27e0..f134d4e24f4 100644
--- a/app/models/report.rb
+++ b/app/models/report.rb
@@ -44,6 +44,7 @@ class Report
yellow: "#FFCD56",
}
+ include Reports::AssociatedAccountsByProvider
include Reports::Bookmarks
include Reports::ConsolidatedApiRequests
include Reports::ConsolidatedPageViews
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 07455a5e71c..4487fc86633 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -1666,6 +1666,13 @@ en:
anon_views: Anonymous
total_views: Total
description: "The top 100 most viewed topics in a date range, combining logged in and anonymous views. Can be filtered by category."
+ associated_accounts_by_provider:
+ title: "Associated accounts by login method"
+ labels:
+ provider: Login provider
+ no_accounts: "No associated accounts"
+ total_users: "Total number of members"
+ description: "Show number of associated accounts grouped by login provider. Applicable only when social login methods are enabled."
dashboard:
problem:
diff --git a/spec/models/concerns/reports/associated_accounts_by_provider_spec.rb b/spec/models/concerns/reports/associated_accounts_by_provider_spec.rb
new file mode 100644
index 00000000000..31e09335509
--- /dev/null
+++ b/spec/models/concerns/reports/associated_accounts_by_provider_spec.rb
@@ -0,0 +1,131 @@
+# frozen_string_literal: true
+
+RSpec.describe "Reports::AssociatedAccountsByProvider" do
+ describe "associated_accounts_by_provider report" do
+ fab!(:user1) { Fabricate(:user) }
+ fab!(:user2) { Fabricate(:user) }
+ fab!(:user3) { Fabricate(:user) }
+ fab!(:user4) { Fabricate(:user) } # User with no associated accounts
+ fab!(:user5) { Fabricate(:user) } # User with disabled provider
+
+ before do
+ # Mock enabled authenticators to only include specific providers
+ google_auth = instance_double("Auth::Authenticator", name: "google_oauth2")
+ facebook_auth = instance_double("Auth::Authenticator", name: "facebook")
+ github_auth = instance_double("Auth::Authenticator", name: "github") # Provider with no users
+
+ allow(Discourse).to receive(:enabled_authenticators).and_return(
+ [google_auth, facebook_auth, github_auth],
+ )
+
+ Fabricate(:user_associated_account, user: user1, provider_name: "google_oauth2")
+ Fabricate(:user_associated_account, user: user2, provider_name: "google_oauth2")
+ Fabricate(:user_associated_account, user: user3, provider_name: "facebook")
+ Fabricate(:user_associated_account, user: user5, provider_name: "twitter") # twitter is not enabled
+ end
+
+ it "returns data grouped by provider, enabled providers only" do
+ report = Report.find("associated_accounts_by_provider")
+
+ google_data = report.data.find { |d| d[:key] == "google_oauth2" }
+ facebook_data = report.data.find { |d| d[:key] == "facebook" }
+ github_data = report.data.find { |d| d[:key] == "github" }
+ twitter_data = report.data.find { |d| d[:key] == "twitter" }
+
+ expect(google_data[:count]).to eq(2)
+ expect(facebook_data[:count]).to eq(1)
+ # GitHub should appear with 0 users since it's enabled
+ expect(github_data[:count]).to eq(0)
+ # Twitter should not appear since it's not in enabled authenticators
+ expect(twitter_data).to be_nil
+ end
+
+ it "includes enabled providers with zero users" do
+ report = Report.find("associated_accounts_by_provider")
+
+ github_data = report.data.find { |d| d[:key] == "github" }
+ expect(github_data).to be_present
+ expect(github_data[:count]).to eq(0)
+ end
+
+ it "includes total users count" do
+ report = Report.find("associated_accounts_by_provider")
+
+ total_data = report.data.find { |d| d[:key] == "total_users" }
+ expect(total_data).to be_present
+ expect(total_data[:provider]).to eq("Total number of members")
+ expect(total_data[:count]).to be >= 4 # At least our test users
+ end
+
+ it "includes users with no associated accounts from enabled providers" do
+ report = Report.find("associated_accounts_by_provider")
+
+ no_accounts_data = report.data.find { |d| d[:key] == "no_accounts" }
+ expect(no_accounts_data).to be_present
+ expect(no_accounts_data[:provider]).to eq("No associated accounts")
+ # user4 has no accounts, user5 has twitter (disabled), so both should be counted
+ expect(no_accounts_data[:count]).to be >= 2
+ end
+
+ it "sorts data by count descending" do
+ report = Report.find("associated_accounts_by_provider")
+
+ expect(report.data.first[:count]).to be >= report.data.last[:count]
+ end
+
+ it "only includes active users" do
+ user1.update!(active: false)
+
+ report = Report.find("associated_accounts_by_provider")
+
+ google_data = report.data.find { |d| d[:key] == "google_oauth2" }
+ expect(google_data[:count]).to eq(1) # Only user2, not user1
+ end
+
+ it "calculates users without enabled provider accounts correctly" do
+ Fabricate(:user)
+ Fabricate(:user)
+
+ report = Report.find("associated_accounts_by_provider")
+
+ total_data = report.data.find { |d| d[:key] == "total_users" }
+ no_accounts_data = report.data.find { |d| d[:key] == "no_accounts" }
+
+ # 3 users with enabled provider accounts (user1, user2, user3)
+ # 4+ users without enabled provider accounts (user4, user5 with disabled twitter plus the two new users)
+ expect(total_data[:count]).to be >= 7
+ expect(no_accounts_data[:count]).to be >= 4
+ end
+
+ it "handles case when no authenticators are enabled" do
+ allow(Discourse).to receive(:enabled_authenticators).and_return([])
+
+ report = Report.find("associated_accounts_by_provider")
+
+ # Should only have total users and no accounts entries
+ provider_entries = report.data.reject { |d| %w[total_users no_accounts].include?(d[:key]) }
+ expect(provider_entries).to be_empty
+
+ total_data = report.data.find { |d| d[:key] == "total_users" }
+ no_accounts_data = report.data.find { |d| d[:key] == "no_accounts" }
+
+ expect(total_data[:count]).to be >= 5
+ expect(no_accounts_data[:count]).to eq(total_data[:count])
+ end
+
+ it "includes all enabled providers even with mixed zero and non-zero counts" do
+ report = Report.find("associated_accounts_by_provider")
+
+ # Should have 3 enabled providers + total_users + no_accounts = 5 entries
+ provider_entries =
+ report.data.select { |d| %w[google_oauth2 facebook github].include?(d[:key]) }
+ expect(provider_entries.length).to eq(3)
+
+ # Verify we have entries with both zero and non-zero counts
+ counts = provider_entries.map { |entry| entry[:count] }
+ expect(counts).to include(0) # GitHub
+ expect(counts).to include(1) # Facebook
+ expect(counts).to include(2) # Google
+ end
+ end
+end
diff --git a/spec/queries/reports/list_query_spec.rb b/spec/queries/reports/list_query_spec.rb
index d023ad609ce..9a79eb9e473 100644
--- a/spec/queries/reports/list_query_spec.rb
+++ b/spec/queries/reports/list_query_spec.rb
@@ -24,10 +24,11 @@ RSpec.describe Reports::ListQuery do
end
it "sorts reports by title" do
- expect(result.map { |r| r[:title] }[0..4]).to eq(
+ expect(result.map { |r| r[:title] }[0..5]).to eq(
[
I18n.t("reports.staff_logins.title"),
I18n.t("reports.page_view_anon_browser_reqs.title"),
+ I18n.t("reports.associated_accounts_by_provider.title"),
I18n.t("reports.bookmarks.title"),
I18n.t("reports.consolidated_api_requests.title"),
I18n.t("reports.dau_by_mau.title"),