From eddcb4953ef0b3c94454ffc1a5c14f0188e2beb5 Mon Sep 17 00:00:00 2001 From: rhymes Date: Mon, 24 Aug 2020 21:02:05 +0200 Subject: [PATCH] Display all authentication providers in mailer footer (#9961) * Display all authentication providers in mailer footer * Refactor into Authentication::Providers.enabled_for_user --- app/helpers/authentication_helper.rb | 7 ++--- app/mailers/application_mailer.rb | 1 + app/services/authentication/providers.rb | 9 ++++++ app/views/layouts/mailer.html.erb | 8 ++--- app/views/users/_account.html.erb | 2 +- .../_account_providers_settings.html.erb | 2 +- spec/helpers/authentication_helper_spec.rb | 30 +++++++++++++++++++ 7 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 spec/helpers/authentication_helper_spec.rb diff --git a/app/helpers/authentication_helper.rb b/app/helpers/authentication_helper.rb index 9a4ea0652..74041fa2a 100644 --- a/app/helpers/authentication_helper.rb +++ b/app/helpers/authentication_helper.rb @@ -15,10 +15,7 @@ module AuthenticationHelper end end - def current_user_authentication_enabled_providers - providers = Authentication::Providers.enabled & current_user.identities.pluck(:provider).map(&:to_sym) - providers.sort.map do |provider_name| - Authentication::Providers.get!(provider_name) - end + def authentication_enabled_providers_for_user(user = current_user) + Authentication::Providers.enabled_for_user(user) end end diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 6de404ff7..2547c8788 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -5,6 +5,7 @@ class ApplicationMailer < ActionMailer::Base # an example is the user_url helper Rails.application.routes.url_helpers helper ApplicationHelper + helper AuthenticationHelper default( from: -> { email_from("Community") }, diff --git a/app/services/authentication/providers.rb b/app/services/authentication/providers.rb index 5781375e7..db9602d99 100644 --- a/app/services/authentication/providers.rb +++ b/app/services/authentication/providers.rb @@ -38,6 +38,15 @@ module Authentication SiteConfig.authentication_providers.map(&:to_sym).sort end + def self.enabled_for_user(user) + return [] unless user + + providers = enabled & user.identities.pluck(:provider).map(&:to_sym) + providers.sort.map do |provider_name| + get!(provider_name) + end + end + def self.enabled?(provider_name) enabled.include?(provider_name.to_sym) end diff --git a/app/views/layouts/mailer.html.erb b/app/views/layouts/mailer.html.erb index eee0c2acc..22e59ec5a 100644 --- a/app/views/layouts/mailer.html.erb +++ b/app/views/layouts/mailer.html.erb @@ -9,11 +9,9 @@ <% if @user %> - <% if @user.twitter_username.blank? %> - Reminder: You used GitHub to authenticate your account, so use that to sign in if prompted. - <% else %> - Reminder: You used Twitter to authenticate your account, so use that to sign in if prompted. - <% end %> + <% providers = authentication_enabled_providers_for_user(@user).map(&:official_name) %> + Reminder: You used <%= providers.to_sentence %> to authenticate your account, so use <%= providers.size == 1 ? "that" : "any of those" %> to sign in if prompted. +

<% if @unsubscribe %> diff --git a/app/views/users/_account.html.erb b/app/views/users/_account.html.erb index fa7ebee97..9fbdf2927 100644 --- a/app/views/users/_account.html.erb +++ b/app/views/users/_account.html.erb @@ -66,7 +66,7 @@

- <% current_user_authentication_enabled_providers.each do |provider| %> + <% authentication_enabled_providers_for_user.each do |provider| %> <% onsubmit = "return confirm('Are you absolutely sure you want to remove your #{provider.official_name} account?');" %> <%= form_tag users_remove_identity_path, method: :delete, onsubmit: onsubmit do %> <%= hidden_field_tag :provider, provider.provider_name %> diff --git a/app/views/users/_account_providers_settings.html.erb b/app/views/users/_account_providers_settings.html.erb index f15c29a8d..01c3fb7f7 100644 --- a/app/views/users/_account_providers_settings.html.erb +++ b/app/views/users/_account_providers_settings.html.erb @@ -1,5 +1,5 @@ diff --git a/spec/helpers/authentication_helper_spec.rb b/spec/helpers/authentication_helper_spec.rb new file mode 100644 index 000000000..3f9442c82 --- /dev/null +++ b/spec/helpers/authentication_helper_spec.rb @@ -0,0 +1,30 @@ +require "rails_helper" + +RSpec.describe AuthenticationHelper, type: :helper do + let(:user) { create(:user, :with_identity) } + + before do + omniauth_mock_providers_payload + end + + describe "#authentication_enabled_providers_for_user" do + it "returns an enabled provider" do + provider = Authentication::Providers.available.first + allow(Authentication::Providers).to receive(:enabled).and_return([provider]) + allow(user).to receive(:identities).and_return(user.identities.where(provider: provider)) + + expected_result = Authentication::Providers.get!(provider) + expect(helper.authentication_enabled_providers_for_user(user)).to match_array([expected_result]) + end + + it "does not return a disabled provider" do + disabled_provider = %i[github] + providers = Authentication::Providers.available - disabled_provider + allow(Authentication::Providers).to receive(:enabled).and_return(providers) + user = create(:user, :with_identity) + + provider_names = helper.authentication_enabled_providers_for_user(user).map(&:provider_name) + expect(provider_names).not_to include(disabled_provider) + end + end +end