REFACTOR: Serve auth provider information in the site serializer.

At the moment core providers are hard-coded in Javascript, and plugin providers get added to the JS payload at compile time. This refactor means that we only ship enabled providers to the client.
This commit is contained in:
David Taylor
2018-07-31 16:18:50 +01:00
parent 4e11811321
commit 812add18bd
24 changed files with 184 additions and 215 deletions

View File

@@ -1,31 +0,0 @@
class Plugin::AuthProvider
def self.auth_attributes
[:glyph, :background_color, :pretty_name, :title, :message, :frame_width, :frame_height, :authenticator,
:pretty_name_setting, :title_setting, :enabled_setting, :full_screen_login, :full_screen_login_setting,
:custom_url]
end
attr_accessor(*auth_attributes)
def name
authenticator.name
end
def to_json
result = { name: name }
result['customUrl'] = custom_url if custom_url
result['prettyNameOverride'] = pretty_name || name
result['titleOverride'] = title if title
result['titleSetting'] = title_setting if title_setting
result['prettyNameSetting'] = pretty_name_setting if pretty_name_setting
result['enabledSetting'] = enabled_setting if enabled_setting
result['messageOverride'] = message if message
result['frameWidth'] = frame_width if frame_width
result['frameHeight'] = frame_height if frame_height
result['fullScreenLogin'] = full_screen_login if full_screen_login
result['fullScreenLoginSetting'] = full_screen_login_setting if full_screen_login_setting
result.to_json
end
end

View File

@@ -1,7 +1,7 @@
require 'digest/sha1'
require 'fileutils'
require_dependency 'plugin/metadata'
require_dependency 'plugin/auth_provider'
require_dependency 'lib/auth'
class Plugin::CustomEmoji
def self.cache_key
@@ -393,38 +393,6 @@ class Plugin::Instance
css = styles.join("\n")
js = javascripts.join("\n")
auth_providers.each do |auth|
auth_json = auth.to_json
hash = Digest::SHA1.hexdigest(auth_json)
js << <<JS
define("discourse/initializers/login-method-#{hash}",
["discourse/models/login-method", "exports"],
function(module, __exports__) {
"use strict";
__exports__["default"] = {
name: "login-method-#{hash}",
after: "inject-objects",
initialize: function(container) {
if (Ember.testing) { return; }
var authOpts = #{auth_json};
authOpts.siteSettings = container.lookup('site-settings:main');
module.register(authOpts);
}
};
});
JS
if auth.glyph
css << ".btn-social.#{auth.name}:before{ content: '#{auth.glyph}'; }\n"
end
if auth.background_color
css << ".btn-social.#{auth.name}{ background: #{auth.background_color}; }\n"
end
end
# Generate an IIFE for the JS
js = "(function(){#{js}})();" if js.present?
@@ -495,9 +463,9 @@ JS
end
def auth_provider(opts)
provider = Plugin::AuthProvider.new
provider = Auth::AuthProvider.new
Plugin::AuthProvider.auth_attributes.each do |sym|
Auth::AuthProvider.auth_attributes.each do |sym|
provider.send "#{sym}=", opts.delete(sym)
end