docbrown/app/services/settings/authentication/upsert.rb
Lewis Sparlin 71e90a41f5
Disable all providers when providers_to_enable param is blank (#15260)
* Disable all providers when providers_to_enable param is blank

* Remove guard, a couple more spec examples

* Update app/services/settings/authentication/upsert.rb

Co-authored-by: Fernando Valverde <fernando@visualcosita.com>

Co-authored-by: Fernando Valverde <fernando@visualcosita.com>
2021-11-02 21:20:26 -04:00

39 lines
1.3 KiB
Ruby

module Settings
class Authentication
module Upsert
attr_reader :errors
def self.call(settings)
auth_providers_to_enable = settings.delete("auth_providers_to_enable")
result = Settings::Upsert.call(settings, ::Settings::Authentication)
update_enabled_providers(auth_providers_to_enable)
result
end
def self.update_enabled_providers(value)
enabled_providers = value.to_s.split(",").filter_map do |entry|
entry unless invalid_provider_entry(entry)
end
return if email_login_disabled_with_one_or_less_auth_providers(enabled_providers)
Settings::Authentication.providers = enabled_providers
end
def self.invalid_provider_entry(entry)
entry.blank? ||
::Authentication::Providers.available.map(&:to_s).exclude?(entry) ||
provider_keys_missing(entry)
end
def self.provider_keys_missing(entry)
::Settings::Authentication.public_send("#{entry}_key").blank? ||
::Settings::Authentication.public_send("#{entry}_secret").blank?
end
def self.email_login_disabled_with_one_or_less_auth_providers(enabled_providers)
!::Settings::Authentication.allow_email_password_login &&
enabled_providers.count <= 1
end
end
end
end