module Eco module API module Organization class LoginProviders < Eco::Language::Models::Collection # build the shortcuts of Collection attr_collection :id, :name def initialize(login_providers = [], klass: Ecoportal::API::Internal::LoginProvider, factory: nil) @klass = Ecoportal::API::Internal::LoginProvider @caches_init = false super(login_providers, klass: @klass) init_caches end def password_id to_id("password") end def active select {|lp| lp != "disabled"} end def any_optional? active.any? do |lp| lp.enabled_for = "some" end end def ids @by_id.keys end def names @by_name.keys end def to_id(name) case name when Enumerable name.map do |n| login_provider(n)&.id end.compact else login_provider(name)&.id end end def to_name(id) case name when Enumerable id.map do |i| login_provider(i)&.name end.compact else login_provider(id)&.name end end # @param id_name_type [String, Symbol] # - `String` to refer to the login provider `name` or `id` # - `Symbol` to refer to the login providr type (i.e. `:password`, `:sso`) def login_provider(id_name_type) @by_id[login_provider_id(id_name_type)] end private def login_provider_id(value) case value when Symbol value = value.to_s.downcase.to_sym @by_type[value]&.id when String value = value.strip.downcase @by_name[value]&.id || @by_id[value]&.id end end def init_caches # rubocop:disable Metrics/AbcSize return if @caches_init enabled_lps = reject {|lp| lp.enabled_for == 'disabled'} @by_id = enabled_lps.each_with_object({}) do |lp, hash| hash[lp.id.downcase] = lp end @by_name = enabled_lps.each_with_object({}) do |lp, hash| hash[lp.name.downcase] = lp hash['magic link'] = lp if lp.type == 'onetimepassword' end @by_type = enabled_lps.each_with_object({}) do |lp, hash| type = lp.type.downcase.to_sym hash[type] = lp unless hash.key?(type) if lp.type == 'saml' # only map the first :sso hash[:sso] = lp unless hash.key?(:sso) hash['sso'] = lp unless hash.key?('sso') end next unless lp.type == 'onetimepassword' hash[:magic_link] = lp hash[:magiclink] = lp hash['magic_link'] = lp hash['magiclink'] = lp end @caches_init = true end end end end end