class LdapFluff::Config

Constants

ATTRIBUTES
DEFAULT_CONFIG

Public Class Methods

new(config) click to toggle source
# File lib/ldap_fluff/config.rb, line 21
def initialize(config)
  raise ArgumentError unless config.respond_to?(:to_hash)
  config = validate(convert(config))

  ATTRIBUTES.each do |attr|
    instance_variable_set(:"@#{attr}", config[attr])
  end
end

Private Instance Methods

all_required_keys?(config) click to toggle source
# File lib/ldap_fluff/config.rb, line 51
def all_required_keys?(config)
  %w[host port base_dn group_base server_type].all? do |key|
    raise ConfigError, "config key #{key} has to be set, it was nil" if config[key].nil?
  end

  %w[service_user service_pass].all? do |key|
    if !config['anon_queries'] && config['server_type'] != :posix && config[key].nil?
      raise ConfigError, "config key #{key} has to be set, it was nil"
    end
  end
end
anon_queries_set?(config) click to toggle source
# File lib/ldap_fluff/config.rb, line 63
def anon_queries_set?(config)
  unless [false, true].include?(config['anon_queries'])
    raise ConfigError, "config key anon_queries has to be true or false but was #{config['anon_queries']}"
  end
end
convert(config) click to toggle source

@param [#to_hash] config

# File lib/ldap_fluff/config.rb, line 33
def convert(config)
  config.to_hash.with_indifferent_access.tap do |conf|
    %w[encryption server_type].each do |key|
      conf[key] = conf[key].to_sym if conf[key]
    end
  end
end
correct_server_type?(config) click to toggle source
# File lib/ldap_fluff/config.rb, line 69
def correct_server_type?(config)
  unless [:posix, :active_directory, :free_ipa].include?(config['server_type'])
    raise ConfigError, 'config key server_type has to be :active_directory, :posix, :free_ipa ' +
      "but was #{config['server_type']}"
  end
end
missing_keys?(config) click to toggle source
# File lib/ldap_fluff/config.rb, line 41
def missing_keys?(config)
  missing_keys = ATTRIBUTES - config.keys
  raise ConfigError, "missing configuration for keys: #{missing_keys.join(',')}" unless missing_keys.empty?
end
unknown_keys?(config) click to toggle source
# File lib/ldap_fluff/config.rb, line 46
def unknown_keys?(config)
  unknown_keys = config.keys - ATTRIBUTES
  raise ConfigError, "unknown configuration keys: #{unknown_keys.join(',')}" unless unknown_keys.empty?
end
validate(config) click to toggle source
# File lib/ldap_fluff/config.rb, line 76
def validate(config)
  config = DEFAULT_CONFIG.merge(config)

  correct_server_type?(config)
  missing_keys?(config)
  unknown_keys?(config)
  all_required_keys?(config)
  anon_queries_set?(config)

  config
end