Module: ActiveLdap::Configuration::ClassMethods

Defined in:
lib/active_ldap/configuration.rb

Defined Under Namespace

Classes: URIConfigurationMerger

Constant Summary collapse

CONNECTION_CONFIGURATION_KEYS =
[:uri, :base, :adapter]
@@defined_configurations =
{}

Instance Method Summary collapse

Instance Method Details

#configuration(key = nil) ⇒ Object



92
93
94
# File 'lib/active_ldap/configuration.rb', line 92

def configuration(key=nil)
  @@defined_configurations[key || active_connection_name]
end

#default_configurationObject



65
66
67
# File 'lib/active_ldap/configuration.rb', line 65

def default_configuration
  DEFAULT_CONFIG.dup
end

#define_configuration(key, config) ⇒ Object



96
97
98
# File 'lib/active_ldap/configuration.rb', line 96

def define_configuration(key, config)
  @@defined_configurations[key] = config
end

#defined_configurationsObject



100
101
102
# File 'lib/active_ldap/configuration.rb', line 100

def defined_configurations
  @@defined_configurations
end

#ensure_configuration(config = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/active_ldap/configuration.rb', line 69

def ensure_configuration(config=nil)
  if config.nil?
    if defined?(LDAP_ENV)
      config = LDAP_ENV
    elsif defined?(Rails)
      config = Rails.env
    else
      config = {}
    end
  end

  if config.is_a?(Symbol) or config.is_a?(String)
    _config = configurations[config.to_s]
    unless _config
      raise ConnectionError,
            _("%s connection is not configured") % config
    end
    config = _config
  end

  config
end

#merge_configuration(user_configuration, target = self) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/active_ldap/configuration.rb', line 129

def merge_configuration(user_configuration, target=self)
  configuration = parent_configuration(target).dup
  prepare_configuration(user_configuration).each do |key, value|
    case key
    when :base
      # Scrub before inserting
      target.base = value.gsub(/['}{#]/, '')
    when :scope, :ldap_scope
      if key == :ldap_scope
        message = _(":ldap_scope configuration option is deprecated. " \
                    "Use :scope instead.")
        ActiveSupport::Deprecation.warn(message)
      end
      target.scope = value
      configuration[:scope] = value
    else
      configuration[key] = value
    end
  end
  configuration
end

#parent_configuration(target) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/active_ldap/configuration.rb', line 115

def parent_configuration(target)
  if target.is_a?(Base)
    target = target.class
  else
    target = target.superclass
  end
  while target <= Base
    config = defined_configurations[target.active_connection_key]
    return config if config
    target = target.superclass
  end
  default_configuration
end

#prepare_configuration(configuration) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/active_ldap/configuration.rb', line 151

def prepare_configuration(configuration)
  configuration = configuration.symbolize_keys
  uri = configuration.delete(:uri)
  return configuration unless uri

  begin
    uri = URI.parse(uri)
  rescue URI::InvalidURIError
    raise ConfigurationError.new(_("invalid URI: %s") % uri)
  end
  unless uri.is_a?(URI::LDAP)
    raise ConfigurationError.new(_("not a LDAP URI: %s") % uri.to_s)
  end

  merger = URIConfigurationMerger.new(uri)
  merger.merge(configuration)
end

#remove_configuration_by_key(key) ⇒ Object



104
105
106
# File 'lib/active_ldap/configuration.rb', line 104

def remove_configuration_by_key(key)
  @@defined_configurations.delete(key)
end


109
110
111
112
113
# File 'lib/active_ldap/configuration.rb', line 109

def remove_connection_related_configuration(config)
  config.reject do |key, value|
    CONNECTION_CONFIGURATION_KEYS.include?(key)
  end
end