lib/active_ldap/configuration.rb in activeldap-4.0.3 vs lib/active_ldap/configuration.rb in activeldap-4.0.4

- old
+ new

@@ -1,5 +1,7 @@ +require "English" +require "cgi" require 'uri' begin require 'uri/ldaps' rescue LoadError module URI @@ -41,10 +43,11 @@ DEFAULT_CONFIG[:retry_limit] = 1 DEFAULT_CONFIG[:retry_wait] = 1 DEFAULT_CONFIG[:timeout] = 0 # in seconds; 0 <= Never timeout # Whether or not to retry on timeouts DEFAULT_CONFIG[:retry_on_timeout] = true + DEFAULT_CONFIG[:follow_referrals] = true DEFAULT_CONFIG[:logger] = nil module ClassMethods @@defined_configurations = {} @@ -94,11 +97,11 @@ CONNECTION_CONFIGURATION_KEYS = [:uri, :base, :adapter] def remove_connection_related_configuration(config) config.reject do |key, value| CONNECTION_CONFIGURATION_KEYS.include?(key) - end + end end def merge_configuration(user_configuration, target=self) configuration = default_configuration prepare_configuration(user_configuration).each do |key, value| @@ -133,15 +136,52 @@ end unless uri.is_a?(URI::LDAP) raise ConfigurationError.new(_("not a LDAP URI: %s") % uri.to_s) end - uri_configuration = {:port => uri.port} - uri_configuration[:host] = uri.host if uri.host - uri_configuration[:bind_dn] = uri.dn if uri.dn - uri_configuration[:scope] = uri.scope if uri.scope - uri_configuration[:method] = :ssl if uri.is_a?(URI::LDAPS) - uri_configuration.merge(configuration) + merger = URIConfigurationMerger.new(uri) + merger.merge(configuration) + end + + class URIConfigurationMerger + def initialize(uri) + @uri = uri + end + + def merge(configuration) + uri_configuration = {:port => @uri.port} + uri_configuration[:host] = @uri.host if @uri.host + uri_configuration[:base] = @uri.dn if @uri.dn + extensions = parse_extensions + bindname_extension = extensions["bindname"] + if bindname_extension + uri_configuration[:bind_dn] = bindname_extension[:value] + uri_configuration[:allow_anonymous] = !bindname_extension[:critical] + end + uri_configuration[:scope] = @uri.scope if @uri.scope + uri_configuration[:method] = :ssl if @uri.is_a?(URI::LDAPS) + uri_configuration.merge(configuration) + end + + private + def parse_extensions + extensions = {} + (@uri.extensions || "").split(",").collect do |extension| + name, value = extension.split("=", 2) + case name + when /\A!/ + critical = true + name = $POSTMATCH + else + critical = false + end + extensions[name] = { + :critical => critical, + :value => CGI.unescape(value || ""), + } + end + extensions + end end end end end