Display all authentication providers in mailer footer (#9961)

* Display all authentication providers in mailer footer

* Refactor into Authentication::Providers.enabled_for_user
This commit is contained in:
rhymes 2020-08-24 21:02:05 +02:00 committed by GitHub
parent a4373346dd
commit eddcb4953e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 47 additions and 12 deletions

View file

@ -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

View file

@ -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") },

View file

@ -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

View file

@ -9,11 +9,9 @@
<% if @user %>
<tr>
<td style="padding:3% 12% 2% 4%;font-size:18px;line-height:22px;color:#7d7d7d">
<% if @user.twitter_username.blank? %>
Reminder: You used <b>GitHub</b> to authenticate your account, so use that to sign in if prompted.
<% else %>
Reminder: You used <b>Twitter</b> 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 <b><%= providers.to_sentence %></b> to authenticate your account, so use <%= providers.size == 1 ? "that" : "any of those" %> to sign in if prompted.
<br /><br />
<div style="font-size:0.78em">
<% if @unsubscribe %>

View file

@ -66,7 +66,7 @@
</p>
</div>
<% 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 %>

View file

@ -1,5 +1,5 @@
<ul>
<% current_user_authentication_enabled_providers.each do |provider| %>
<% authentication_enabled_providers_for_user.each do |provider| %>
<li><a href="<%= provider.settings_url %>"><%= provider.official_name %> profile settings</a></li>
<% end %>
</ul>

View file

@ -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