Add multiple select to authentication providers configuration (#7609)

* Add multiple select to authentication providers configuration

* Remove hardcoded providers from SiteConfig
This commit is contained in:
rhymes 2020-04-30 17:28:08 +02:00 committed by GitHub
parent b7a29c7144
commit 014ec65e6b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 20 deletions

View file

@ -11,7 +11,9 @@ class Internal::ConfigsController < Internal::ApplicationController
clean_up_params
config_params.each do |key, value|
if value.respond_to?(:to_h)
if value.is_a?(Array)
SiteConfig.public_send("#{key}=", value.reject(&:blank?)) unless value.empty?
elsif value.respond_to?(:to_h)
SiteConfig.public_send("#{key}=", value.to_h) unless value.empty?
else
SiteConfig.public_send("#{key}=", value.strip) unless value.nil?
@ -26,7 +28,6 @@ class Internal::ConfigsController < Internal::ApplicationController
def config_params
allowed_params = %i[
authentication_providers
campaign_featured_tags
campaign_hero_html_variant_name
campaign_sidebar_enabled
@ -34,7 +35,6 @@ class Internal::ConfigsController < Internal::ApplicationController
community_description
community_member_description
community_member_label
tagline
favicon_url
ga_view_id ga_fetch_rate
logo_png
@ -59,11 +59,16 @@ class Internal::ConfigsController < Internal::ApplicationController
shop_url
sidebar_tags
suggested_tags
tagline
]
params.require(:site_config).permit(allowed_params,
social_media_handles: SiteConfig.social_media_handles.keys,
email_addresses: SiteConfig.email_addresses.keys,
meta_keywords: SiteConfig.meta_keywords.keys)
params.require(:site_config).permit(
allowed_params,
authentication_providers: [],
social_media_handles: SiteConfig.social_media_handles.keys,
email_addresses: SiteConfig.email_addresses.keys,
meta_keywords: SiteConfig.meta_keywords.keys,
)
end
def extra_authorization_and_confirmation
@ -74,7 +79,6 @@ class Internal::ConfigsController < Internal::ApplicationController
def clean_up_params
config = params[:site_config]
config[:suggested_tags] = config[:suggested_tags].downcase.delete(" ") if config[:suggested_tags]
config[:authentication_providers] = config[:authentication_providers].downcase.delete(" ") if config[:authentication_providers]
config[:sidebar_tags] = config[:sidebar_tags].downcase.delete(" ") if config[:sidebar_tags]
end

View file

@ -3,6 +3,12 @@ module AuthenticationHelper
Authentication::Providers.get!(provider_name)
end
def authentication_available_providers
Authentication::Providers.available.map do |provider_name|
Authentication::Providers.const_get(provider_name.to_s.titleize)
end
end
def authentication_enabled_providers
Authentication::Providers.enabled.map do |provider_name|
Authentication::Providers.get!(provider_name)

View file

@ -44,7 +44,7 @@ class SiteConfig < RailsSettings::Base
}
# Authentication
field :authentication_providers, type: :array, default: %w[twitter github]
field :authentication_providers, type: :array, default: Authentication::Providers.available
# Broadcast
field :welcome_notifications_live_at, type: :date

View file

@ -169,11 +169,16 @@
<div id="authenticationBodyContainer" class="card-body collapse hide" aria-labelledby="authenticationBodyContainer">
<div class="form-group">
<%= f.label :authentication_providers, "Authentication providers" %>
<%= f.text_field :authentication_providers,
class: "form-control",
value: SiteConfig.authentication_providers.join(","),
placeholder: "List of valid providers: comma separated, letters only e.g. twitter,github" %>
<div class="alert alert-info">How can users sign in? (More options coming)</div>
<%= select_tag "site_config[authentication_providers]",
options_from_collection_for_select(
authentication_available_providers,
:provider_name,
:official_name,
authentication_enabled_providers.map(&:provider_name),
),
multiple: true,
class: "form-control selectpicker" %>
<div class="alert alert-info">How can users sign in?</div>
</div>
</div>
</div>

View file

@ -19,6 +19,7 @@
<!-- Bootstrap and FontAwesome -->
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.16/css/bootstrap-select.min.css" integrity="sha256-g19F2KOr/H58l6XdI/rhCdEK3NmB8OILHwP/QYBQ8M4=" crossorigin="anonymous" />
<%= stylesheet_link_tag "internal/layout" %>
@ -49,6 +50,7 @@
<!-- Bootstrap Dependencies -->
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.16/js/bootstrap-select.min.js" integrity="sha256-COIM4OdXvo3jkE0/jD/QIEDe3x0jRuqHhOdGTkno3uM=" crossorigin="anonymous"></script>
<%= javascript_include_tag "internal" %>

View file

@ -307,14 +307,17 @@ RSpec.describe "/internal/config", type: :request do
end
describe "Authentication" do
it "removes space authentication_providers" do
post "/internal/config", params: { site_config: { authentication_providers: "github, twitter" }, confirmation: confirmation_message }
expect(SiteConfig.authentication_providers).to eq(%w[github twitter])
it "updates enabled authentication providers" do
enabled = Array.wrap(Authentication::Providers.available.first.to_s)
post "/internal/config", params: { site_config: { authentication_providers: enabled }, confirmation: confirmation_message }
expect(SiteConfig.authentication_providers).to eq(enabled)
end
it "downcases authentication_providers" do
post "/internal/config", params: { site_config: { authentication_providers: "GitHub, Twitter" }, confirmation: confirmation_message }
expect(SiteConfig.authentication_providers).to eq(%w[github twitter])
it "strips empty elements" do
provider = Authentication::Providers.available.first.to_s
enabled = [provider, "", nil]
post "/internal/config", params: { site_config: { authentication_providers: enabled }, confirmation: confirmation_message }
expect(SiteConfig.authentication_providers).to eq([provider])
end
end
end