DEV: Relax auth provider registration restrictions for plugins (#24095)

In the past we would build the stack of Omniauth providers at boot, which meant that plugins had to register any authenticators in the root of their plugin.rb (i.e. not in an `after_initialize` block). This could be frustrating because many features are not available that early in boot (e.g. Zeitwerk autoloading).

Now that we build the omniauth strategy stack 'just in time', it is safe for plugins to register their auth methods in an `after_initialize` block. This commit relaxes the old restrictions so that plugin authors have the option to move things around.
This commit is contained in:
David Taylor
2023-10-26 10:54:30 +01:00
committed by GitHub
parent 8438aed727
commit c88303bb27
3 changed files with 5 additions and 20 deletions

View File

@ -50,7 +50,6 @@ class Plugin::Instance
%i[
assets
color_schemes
before_auth_initializers
initializers
javascripts
locales
@ -510,13 +509,6 @@ class Plugin::Instance
@git_repo ||= GitRepo.new(directory, name)
end
def before_auth(&block)
if @before_auth_complete
raise "Auth providers must be registered before omniauth middleware. after_initialize is too late!"
end
before_auth_initializers << block
end
# A proxy to `DiscourseEvent.on` which does nothing if the plugin is disabled
def on(event_name, &block)
DiscourseEvent.on(event_name) { |*args, **kwargs| block.call(*args, **kwargs) if enabled? }
@ -541,11 +533,6 @@ class Plugin::Instance
end
end
def notify_before_auth
before_auth_initializers.each { |callback| callback.call(self) }
@before_auth_complete = true
end
# Applies to all sites in a multisite environment. Ignores plugin.enabled?
def register_category_custom_field_type(name, type)
reloadable_patch { |plugin| Category.register_custom_field_type(name, type) }
@ -793,7 +780,7 @@ class Plugin::Instance
end
def auth_provider(opts)
before_auth do
after_initialize do
provider = Auth::AuthProvider.new
Auth::AuthProvider.auth_attributes.each do |sym|