powerhome-activeldap 3.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.yardopts +6 -0
- data/COPYING +340 -0
- data/Gemfile +12 -0
- data/LICENSE +59 -0
- data/README.textile +140 -0
- data/TODO +32 -0
- data/benchmark/README.md +64 -0
- data/benchmark/bench-backend.rb +247 -0
- data/benchmark/bench-instantiate.rb +98 -0
- data/benchmark/config.yaml.sample +5 -0
- data/doc/text/development.textile +54 -0
- data/doc/text/news.textile +811 -0
- data/doc/text/rails.textile +144 -0
- data/doc/text/tutorial.textile +1010 -0
- data/examples/config.yaml.example +5 -0
- data/examples/example.der +0 -0
- data/examples/example.jpg +0 -0
- data/examples/groupadd +41 -0
- data/examples/groupdel +35 -0
- data/examples/groupls +49 -0
- data/examples/groupmod +42 -0
- data/examples/lpasswd +55 -0
- data/examples/objects/group.rb +13 -0
- data/examples/objects/ou.rb +4 -0
- data/examples/objects/user.rb +20 -0
- data/examples/ouadd +38 -0
- data/examples/useradd +45 -0
- data/examples/useradd-binary +53 -0
- data/examples/userdel +34 -0
- data/examples/userls +50 -0
- data/examples/usermod +42 -0
- data/examples/usermod-binary-add +50 -0
- data/examples/usermod-binary-add-time +54 -0
- data/examples/usermod-binary-del +48 -0
- data/examples/usermod-lang-add +43 -0
- data/lib/active_ldap.rb +85 -0
- data/lib/active_ldap/action_controller/ldap_benchmarking.rb +55 -0
- data/lib/active_ldap/acts/tree.rb +78 -0
- data/lib/active_ldap/adapter/base.rb +707 -0
- data/lib/active_ldap/adapter/jndi.rb +184 -0
- data/lib/active_ldap/adapter/jndi_connection.rb +185 -0
- data/lib/active_ldap/adapter/ldap.rb +290 -0
- data/lib/active_ldap/adapter/ldap_ext.rb +105 -0
- data/lib/active_ldap/adapter/net_ldap.rb +309 -0
- data/lib/active_ldap/adapter/net_ldap_ext.rb +23 -0
- data/lib/active_ldap/association/belongs_to.rb +47 -0
- data/lib/active_ldap/association/belongs_to_many.rb +58 -0
- data/lib/active_ldap/association/children.rb +21 -0
- data/lib/active_ldap/association/collection.rb +105 -0
- data/lib/active_ldap/association/has_many.rb +31 -0
- data/lib/active_ldap/association/has_many_utils.rb +44 -0
- data/lib/active_ldap/association/has_many_wrap.rb +75 -0
- data/lib/active_ldap/association/proxy.rb +107 -0
- data/lib/active_ldap/associations.rb +205 -0
- data/lib/active_ldap/attribute_methods.rb +23 -0
- data/lib/active_ldap/attribute_methods/before_type_cast.rb +24 -0
- data/lib/active_ldap/attribute_methods/dirty.rb +43 -0
- data/lib/active_ldap/attribute_methods/query.rb +31 -0
- data/lib/active_ldap/attribute_methods/read.rb +44 -0
- data/lib/active_ldap/attribute_methods/write.rb +38 -0
- data/lib/active_ldap/attributes.rb +176 -0
- data/lib/active_ldap/base.rb +1410 -0
- data/lib/active_ldap/callbacks.rb +71 -0
- data/lib/active_ldap/command.rb +49 -0
- data/lib/active_ldap/compatible.rb +44 -0
- data/lib/active_ldap/configuration.rb +147 -0
- data/lib/active_ldap/connection.rb +299 -0
- data/lib/active_ldap/distinguished_name.rb +291 -0
- data/lib/active_ldap/entry_attribute.rb +78 -0
- data/lib/active_ldap/escape.rb +12 -0
- data/lib/active_ldap/get_text.rb +20 -0
- data/lib/active_ldap/get_text/parser.rb +161 -0
- data/lib/active_ldap/helper.rb +92 -0
- data/lib/active_ldap/human_readable.rb +133 -0
- data/lib/active_ldap/ldap_error.rb +74 -0
- data/lib/active_ldap/ldif.rb +930 -0
- data/lib/active_ldap/log_subscriber.rb +50 -0
- data/lib/active_ldap/object_class.rb +95 -0
- data/lib/active_ldap/operations.rb +624 -0
- data/lib/active_ldap/persistence.rb +100 -0
- data/lib/active_ldap/populate.rb +53 -0
- data/lib/active_ldap/railtie.rb +43 -0
- data/lib/active_ldap/railties/controller_runtime.rb +48 -0
- data/lib/active_ldap/schema.rb +701 -0
- data/lib/active_ldap/schema/syntaxes.rb +422 -0
- data/lib/active_ldap/timeout.rb +75 -0
- data/lib/active_ldap/timeout_stub.rb +17 -0
- data/lib/active_ldap/user_password.rb +99 -0
- data/lib/active_ldap/validations.rb +200 -0
- data/lib/active_ldap/version.rb +3 -0
- data/lib/active_ldap/xml.rb +139 -0
- data/lib/rails/generators/active_ldap/model/USAGE +18 -0
- data/lib/rails/generators/active_ldap/model/model_generator.rb +47 -0
- data/lib/rails/generators/active_ldap/model/templates/model_active_ldap.rb +3 -0
- data/lib/rails/generators/active_ldap/scaffold/scaffold_generator.rb +14 -0
- data/lib/rails/generators/active_ldap/scaffold/templates/ldap.yml +19 -0
- data/po/en/active-ldap.po +4029 -0
- data/po/ja/active-ldap.po +4060 -0
- data/test/add-phonetic-attribute-options-to-slapd.ldif +10 -0
- data/test/al-test-utils.rb +428 -0
- data/test/command.rb +111 -0
- data/test/config.yaml.sample +6 -0
- data/test/fixtures/lower_case_object_class_schema.rb +802 -0
- data/test/run-test.rb +34 -0
- data/test/test_acts_as_tree.rb +60 -0
- data/test/test_adapter.rb +121 -0
- data/test/test_associations.rb +701 -0
- data/test/test_attributes.rb +117 -0
- data/test/test_base.rb +1214 -0
- data/test/test_base_per_instance.rb +61 -0
- data/test/test_bind.rb +62 -0
- data/test/test_callback.rb +31 -0
- data/test/test_configuration.rb +40 -0
- data/test/test_connection.rb +82 -0
- data/test/test_connection_per_class.rb +112 -0
- data/test/test_connection_per_dn.rb +112 -0
- data/test/test_dirty.rb +98 -0
- data/test/test_dn.rb +172 -0
- data/test/test_find.rb +176 -0
- data/test/test_groupadd.rb +50 -0
- data/test/test_groupdel.rb +46 -0
- data/test/test_groupls.rb +107 -0
- data/test/test_groupmod.rb +51 -0
- data/test/test_ldif.rb +1890 -0
- data/test/test_load.rb +133 -0
- data/test/test_lpasswd.rb +75 -0
- data/test/test_object_class.rb +74 -0
- data/test/test_persistence.rb +131 -0
- data/test/test_reflection.rb +175 -0
- data/test/test_schema.rb +559 -0
- data/test/test_syntax.rb +444 -0
- data/test/test_user.rb +217 -0
- data/test/test_user_password.rb +108 -0
- data/test/test_useradd-binary.rb +62 -0
- data/test/test_useradd.rb +57 -0
- data/test/test_userdel.rb +48 -0
- data/test/test_userls.rb +91 -0
- data/test/test_usermod-binary-add-time.rb +65 -0
- data/test/test_usermod-binary-add.rb +64 -0
- data/test/test_usermod-binary-del.rb +66 -0
- data/test/test_usermod-lang-add.rb +59 -0
- data/test/test_usermod.rb +58 -0
- data/test/test_validation.rb +274 -0
- metadata +379 -0
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'active_support/core_ext/array/wrap'
|
2
|
+
|
3
|
+
module ActiveLdap
|
4
|
+
module Callbacks
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
CALLBACKS = [
|
8
|
+
:after_initialize, :after_find, :after_touch, :before_validation, :after_validation,
|
9
|
+
:before_save, :around_save, :after_save, :before_create, :around_create,
|
10
|
+
:after_create, :before_update, :around_update, :after_update,
|
11
|
+
:before_destroy, :around_destroy, :after_destroy, :after_commit, :after_rollback
|
12
|
+
]
|
13
|
+
|
14
|
+
included do
|
15
|
+
extend ActiveModel::Callbacks
|
16
|
+
include ActiveModel::Validations::Callbacks
|
17
|
+
|
18
|
+
define_model_callbacks :initialize, :find, :touch, :only => :after
|
19
|
+
define_model_callbacks :save, :create, :update, :destroy
|
20
|
+
|
21
|
+
class << self
|
22
|
+
alias_method_chain :instantiate, :callbacks
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module ClassMethods
|
27
|
+
def method_added(meth)
|
28
|
+
super
|
29
|
+
if CALLBACKS.include?(meth.to_sym)
|
30
|
+
ActiveSupport::Deprecation.warn("Base##{meth} has been deprecated, please use Base.#{meth} :method instead", caller[0,1])
|
31
|
+
send(meth.to_sym, meth.to_sym)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
module ClassMethods
|
37
|
+
def instantiate_with_callbacks(record)
|
38
|
+
object = instantiate_without_callbacks(record)
|
39
|
+
object.send(:_run_find_callbacks)
|
40
|
+
object.send(:_run_initialize_callbacks)
|
41
|
+
object
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def initialize(*) #:nodoc:
|
46
|
+
run_callbacks(:initialize) { super }
|
47
|
+
end
|
48
|
+
|
49
|
+
def destroy #:nodoc:
|
50
|
+
run_callbacks(:destroy) { super }
|
51
|
+
end
|
52
|
+
|
53
|
+
def touch(*) #:nodoc:
|
54
|
+
run_callbacks(:touch) { super }
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def create_or_update #:nodoc:
|
60
|
+
run_callbacks(:save) { super }
|
61
|
+
end
|
62
|
+
|
63
|
+
def create #:nodoc:
|
64
|
+
run_callbacks(:create) { super }
|
65
|
+
end
|
66
|
+
|
67
|
+
def update(*) #:nodoc:
|
68
|
+
run_callbacks(:update) { super }
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
module ActiveLdap
|
5
|
+
module Command
|
6
|
+
include GetTextSupport
|
7
|
+
|
8
|
+
module_function
|
9
|
+
def parse_options(argv=nil, version=nil)
|
10
|
+
argv ||= ARGV.dup
|
11
|
+
options = OpenStruct.new
|
12
|
+
opts = OptionParser.new do |opts|
|
13
|
+
yield(opts, options)
|
14
|
+
|
15
|
+
opts.separator ""
|
16
|
+
opts.separator _("Common options:")
|
17
|
+
|
18
|
+
opts.on_tail("--config=CONFIG",
|
19
|
+
_("Specify configuration file written as YAML")) do |file|
|
20
|
+
require 'yaml'
|
21
|
+
config = YAML.load(File.read(file)).symbolize_keys
|
22
|
+
config = Base.prepare_configuration(config)
|
23
|
+
Configuration::DEFAULT_CONFIG.update(config)
|
24
|
+
end
|
25
|
+
|
26
|
+
opts.on_tail("-h", "--help", _("Show this message")) do
|
27
|
+
puts opts
|
28
|
+
exit
|
29
|
+
end
|
30
|
+
|
31
|
+
opts.on_tail("--version", _("Show version")) do
|
32
|
+
puts(version || VERSION)
|
33
|
+
exit
|
34
|
+
end
|
35
|
+
end
|
36
|
+
opts.parse!(argv)
|
37
|
+
[argv, opts, options]
|
38
|
+
end
|
39
|
+
|
40
|
+
def read_password(prompt, input=$stdin, output=$stdout)
|
41
|
+
output.print(prompt)
|
42
|
+
system("/bin/stty -echo") if input.tty?
|
43
|
+
input.gets.chomp
|
44
|
+
ensure
|
45
|
+
system("/bin/stty echo") if input.tty?
|
46
|
+
output.puts
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module ActiveLdap
|
2
|
+
module Compatible
|
3
|
+
module_function
|
4
|
+
if "".respond_to?(:force_encoding)
|
5
|
+
def convert_to_utf8_encoded_object(object)
|
6
|
+
case object
|
7
|
+
when Array
|
8
|
+
object.collect {|element| convert_to_utf8_encoded_object(element)}
|
9
|
+
when Hash
|
10
|
+
encoded = {}
|
11
|
+
object.each do |key, value|
|
12
|
+
key = convert_to_utf8_encoded_object(key)
|
13
|
+
value = convert_to_utf8_encoded_object(value)
|
14
|
+
encoded[key] = value
|
15
|
+
end
|
16
|
+
encoded
|
17
|
+
when String
|
18
|
+
encoded = object.dup
|
19
|
+
encoded.force_encoding("utf-8")
|
20
|
+
encoded = object unless encoded.valid_encoding?
|
21
|
+
encoded
|
22
|
+
else
|
23
|
+
object
|
24
|
+
end
|
25
|
+
end
|
26
|
+
else
|
27
|
+
def convert_to_utf8_encoded_object(object)
|
28
|
+
object
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
if "".respond_to?(:lines)
|
33
|
+
def string_to_lines(string)
|
34
|
+
string.lines.to_a
|
35
|
+
end
|
36
|
+
else
|
37
|
+
def string_to_lines(string)
|
38
|
+
ary = []
|
39
|
+
string.each_line{|l| ary << l}
|
40
|
+
ary
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,147 @@
|
|
1
|
+
require 'uri'
|
2
|
+
begin
|
3
|
+
require 'uri/ldaps'
|
4
|
+
rescue LoadError
|
5
|
+
module URI
|
6
|
+
class LDAPS < LDAP
|
7
|
+
DEFAULT_PORT = 636
|
8
|
+
end
|
9
|
+
@@schemes['LDAPS'] = LDAPS
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module ActiveLdap
|
14
|
+
# Configuration
|
15
|
+
#
|
16
|
+
# Configuration provides the default settings required for
|
17
|
+
# ActiveLdap to work with your LDAP server. All of these
|
18
|
+
# settings can be passed in at initialization time.
|
19
|
+
module Configuration
|
20
|
+
def self.included(base)
|
21
|
+
base.extend(ClassMethods)
|
22
|
+
end
|
23
|
+
|
24
|
+
DEFAULT_CONFIG = {}
|
25
|
+
DEFAULT_CONFIG[:host] = '127.0.0.1'
|
26
|
+
DEFAULT_CONFIG[:port] = nil
|
27
|
+
DEFAULT_CONFIG[:method] = :plain # :ssl, :tls, :plain allowed
|
28
|
+
|
29
|
+
DEFAULT_CONFIG[:bind_dn] = nil
|
30
|
+
DEFAULT_CONFIG[:password_block] = nil
|
31
|
+
DEFAULT_CONFIG[:password] = nil
|
32
|
+
DEFAULT_CONFIG[:store_password] = true
|
33
|
+
DEFAULT_CONFIG[:allow_anonymous] = true
|
34
|
+
DEFAULT_CONFIG[:sasl_quiet] = true
|
35
|
+
DEFAULT_CONFIG[:try_sasl] = false
|
36
|
+
DEFAULT_CONFIG[:sasl_options] = nil
|
37
|
+
# See http://www.iana.org/assignments/sasl-mechanisms
|
38
|
+
DEFAULT_CONFIG[:sasl_mechanisms] = ["GSSAPI", "DIGEST-MD5",
|
39
|
+
"CRAM-MD5", "EXTERNAL"]
|
40
|
+
|
41
|
+
DEFAULT_CONFIG[:retry_limit] = 1
|
42
|
+
DEFAULT_CONFIG[:retry_wait] = 1
|
43
|
+
DEFAULT_CONFIG[:timeout] = 0 # in seconds; 0 <= Never timeout
|
44
|
+
# Whether or not to retry on timeouts
|
45
|
+
DEFAULT_CONFIG[:retry_on_timeout] = true
|
46
|
+
|
47
|
+
DEFAULT_CONFIG[:logger] = nil
|
48
|
+
|
49
|
+
module ClassMethods
|
50
|
+
@@defined_configurations = {}
|
51
|
+
|
52
|
+
def default_configuration
|
53
|
+
DEFAULT_CONFIG.dup
|
54
|
+
end
|
55
|
+
|
56
|
+
def ensure_configuration(config=nil)
|
57
|
+
if config.nil?
|
58
|
+
if defined?(LDAP_ENV)
|
59
|
+
config = LDAP_ENV
|
60
|
+
elsif defined?(Rails)
|
61
|
+
config = Rails.env
|
62
|
+
else
|
63
|
+
config = {}
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
if config.is_a?(Symbol) or config.is_a?(String)
|
68
|
+
_config = configurations[config.to_s]
|
69
|
+
unless _config
|
70
|
+
raise ConnectionError,
|
71
|
+
_("%s connection is not configured") % config
|
72
|
+
end
|
73
|
+
config = _config
|
74
|
+
end
|
75
|
+
|
76
|
+
config
|
77
|
+
end
|
78
|
+
|
79
|
+
def configuration(key=nil)
|
80
|
+
@@defined_configurations[key || active_connection_name]
|
81
|
+
end
|
82
|
+
|
83
|
+
def define_configuration(key, config)
|
84
|
+
@@defined_configurations[key] = config
|
85
|
+
end
|
86
|
+
|
87
|
+
def defined_configurations
|
88
|
+
@@defined_configurations
|
89
|
+
end
|
90
|
+
|
91
|
+
def remove_configuration_by_configuration(config)
|
92
|
+
@@defined_configurations.delete_if {|key, value| value == config}
|
93
|
+
end
|
94
|
+
|
95
|
+
CONNECTION_CONFIGURATION_KEYS = [:uri, :base, :adapter]
|
96
|
+
def remove_connection_related_configuration(config)
|
97
|
+
config.reject do |key, value|
|
98
|
+
CONNECTION_CONFIGURATION_KEYS.include?(key)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def merge_configuration(user_configuration, target=self)
|
103
|
+
configuration = default_configuration
|
104
|
+
prepare_configuration(user_configuration).each do |key, value|
|
105
|
+
case key
|
106
|
+
when :base
|
107
|
+
# Scrub before inserting
|
108
|
+
target.base = value.gsub(/['}{#]/, '')
|
109
|
+
when :scope, :ldap_scope
|
110
|
+
if key == :ldap_scope
|
111
|
+
message = _(":ldap_scope configuration option is deprecated. " \
|
112
|
+
"Use :scope instead.")
|
113
|
+
ActiveSupport::Deprecation.warn(message)
|
114
|
+
end
|
115
|
+
target.scope = value
|
116
|
+
configuration[:scope] = value
|
117
|
+
else
|
118
|
+
configuration[key] = value
|
119
|
+
end
|
120
|
+
end
|
121
|
+
configuration
|
122
|
+
end
|
123
|
+
|
124
|
+
def prepare_configuration(configuration)
|
125
|
+
configuration = configuration.symbolize_keys
|
126
|
+
uri = configuration.delete(:uri)
|
127
|
+
return configuration unless uri
|
128
|
+
|
129
|
+
begin
|
130
|
+
uri = URI.parse(uri)
|
131
|
+
rescue URI::InvalidURIError
|
132
|
+
raise ConfigurationError.new(_("invalid URI: %s") % uri)
|
133
|
+
end
|
134
|
+
unless uri.is_a?(URI::LDAP)
|
135
|
+
raise ConfigurationError.new(_("not a LDAP URI: %s") % uri.to_s)
|
136
|
+
end
|
137
|
+
|
138
|
+
uri_configuration = {:port => uri.port}
|
139
|
+
uri_configuration[:host] = uri.host if uri.host
|
140
|
+
uri_configuration[:bind_dn] = uri.dn if uri.dn
|
141
|
+
uri_configuration[:scope] = uri.scope if uri.scope
|
142
|
+
uri_configuration[:method] = :ssl if uri.is_a?(URI::LDAPS)
|
143
|
+
uri_configuration.merge(configuration)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
@@ -0,0 +1,299 @@
|
|
1
|
+
module ActiveLdap
|
2
|
+
module Connection
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
@@active_connections = {}
|
9
|
+
@@allow_concurrency = false
|
10
|
+
|
11
|
+
def thread_safe_active_connections
|
12
|
+
@@active_connections[Thread.current.object_id] ||= {}
|
13
|
+
end
|
14
|
+
|
15
|
+
def single_threaded_active_connections
|
16
|
+
@@active_connections
|
17
|
+
end
|
18
|
+
|
19
|
+
if @@allow_concurrency
|
20
|
+
alias_method :active_connections, :thread_safe_active_connections
|
21
|
+
else
|
22
|
+
alias_method :active_connections, :single_threaded_active_connections
|
23
|
+
end
|
24
|
+
|
25
|
+
def allow_concurrency=(threaded) #:nodoc:
|
26
|
+
logger.debug {"allow_concurrency=#{threaded}"} if logger
|
27
|
+
return if @@allow_concurrency == threaded
|
28
|
+
clear_all_cached_connections!
|
29
|
+
@@allow_concurrency = threaded
|
30
|
+
method_prefix = threaded ? "thread_safe" : "single_threaded"
|
31
|
+
sing = (class << self; self; end)
|
32
|
+
[:active_connections].each do |method|
|
33
|
+
sing.send(:alias_method, method, "#{method_prefix}_#{method}")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def active_connection_name
|
38
|
+
@active_connection_name ||= determine_active_connection_name
|
39
|
+
end
|
40
|
+
|
41
|
+
def remove_active_connections!
|
42
|
+
active_connections.keys.each do |key|
|
43
|
+
remove_connection(key)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def clear_active_connections!
|
48
|
+
connections = active_connections
|
49
|
+
connections.each do |key, connection|
|
50
|
+
connection.disconnect!
|
51
|
+
end
|
52
|
+
connections.clear
|
53
|
+
end
|
54
|
+
|
55
|
+
def clear_active_connection_name
|
56
|
+
@active_connection_name = nil
|
57
|
+
ObjectSpace.each_object(Class) do |klass|
|
58
|
+
if klass < self and !klass.name.blank?
|
59
|
+
klass.instance_variable_set("@active_connection_name", nil)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def connection
|
65
|
+
conn = nil
|
66
|
+
@active_connection_name ||= nil
|
67
|
+
if @active_connection_name
|
68
|
+
conn = active_connections[@active_connection_name]
|
69
|
+
end
|
70
|
+
unless conn
|
71
|
+
conn = retrieve_connection
|
72
|
+
active_connections[@active_connection_name] = conn
|
73
|
+
end
|
74
|
+
conn
|
75
|
+
end
|
76
|
+
|
77
|
+
def connection=(adapter)
|
78
|
+
if adapter.is_a?(Adapter::Base)
|
79
|
+
active_connections[active_connection_name] = adapter
|
80
|
+
elsif adapter.is_a?(Hash)
|
81
|
+
config = adapter
|
82
|
+
self.connection = instantiate_adapter(config)
|
83
|
+
elsif adapter.nil?
|
84
|
+
raise ConnectionNotSetup
|
85
|
+
else
|
86
|
+
setup_connection(adapter)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def instantiate_adapter(config)
|
91
|
+
adapter = (config[:adapter] || default_adapter)
|
92
|
+
normalized_adapter = adapter.downcase.gsub(/-/, "_")
|
93
|
+
adapter_method = "#{normalized_adapter}_connection"
|
94
|
+
unless Adapter::Base.respond_to?(adapter_method)
|
95
|
+
raise AdapterNotFound.new(adapter)
|
96
|
+
end
|
97
|
+
if config.has_key?(:ldap_scope)
|
98
|
+
message = _(":ldap_scope connection option is deprecated. " \
|
99
|
+
"Use :scope instead.")
|
100
|
+
ActiveSupport::Deprecation.warn(message)
|
101
|
+
config[:scope] ||= config.delete(:ldap_scope)
|
102
|
+
end
|
103
|
+
config = remove_connection_related_configuration(config)
|
104
|
+
Adapter::Base.send(adapter_method, config)
|
105
|
+
end
|
106
|
+
|
107
|
+
def default_adapter
|
108
|
+
@@default_adapter ||= guess_available_adapter
|
109
|
+
end
|
110
|
+
|
111
|
+
def connected?
|
112
|
+
active_connections[active_connection_name] ? true : false
|
113
|
+
end
|
114
|
+
|
115
|
+
def retrieve_connection
|
116
|
+
conn = nil
|
117
|
+
name = active_connection_name
|
118
|
+
raise ConnectionNotSetup unless name
|
119
|
+
conn = active_connections[name]
|
120
|
+
if conn.nil?
|
121
|
+
config = configuration(name)
|
122
|
+
raise ConnectionNotSetup unless config
|
123
|
+
self.connection = config
|
124
|
+
conn = active_connections[name]
|
125
|
+
end
|
126
|
+
raise ConnectionNotSetup if conn.nil?
|
127
|
+
conn
|
128
|
+
end
|
129
|
+
|
130
|
+
def remove_connection(klass_or_key=self)
|
131
|
+
if klass_or_key.is_a?(Module)
|
132
|
+
key = active_connection_key(klass_or_key)
|
133
|
+
else
|
134
|
+
key = klass_or_key
|
135
|
+
end
|
136
|
+
config = configuration(key)
|
137
|
+
conn = active_connections[key]
|
138
|
+
remove_configuration_by_configuration(config)
|
139
|
+
active_connections.delete_if {|_key, value| value == conn}
|
140
|
+
conn.disconnect! if conn
|
141
|
+
config
|
142
|
+
end
|
143
|
+
|
144
|
+
def setup_connection(config=nil)
|
145
|
+
config = ensure_configuration(config)
|
146
|
+
remove_connection
|
147
|
+
|
148
|
+
clear_active_connection_name
|
149
|
+
key = active_connection_key
|
150
|
+
@active_connection_name = key
|
151
|
+
define_configuration(key, merge_configuration(config))
|
152
|
+
end
|
153
|
+
|
154
|
+
def establish_connection(config=nil)
|
155
|
+
message =
|
156
|
+
_("ActiveLdap::Connection.establish_connection has been deprecated " \
|
157
|
+
"since 1.1.0. " \
|
158
|
+
"Please use ActiveLdap::Connection.setup_connection instead.")
|
159
|
+
ActiveSupport::Deprecation.warn(message)
|
160
|
+
setup_connection(config)
|
161
|
+
end
|
162
|
+
|
163
|
+
# Return the schema object
|
164
|
+
def schema
|
165
|
+
connection.schema
|
166
|
+
end
|
167
|
+
|
168
|
+
def reset_runtime
|
169
|
+
active_connections.inject(0) do |result, (name, connection)|
|
170
|
+
_ = name # for suppress a warning on Ruby 1.9.3
|
171
|
+
result + connection.reset_runtime
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
private
|
176
|
+
def active_connection_key(k=self)
|
177
|
+
k.name.blank? ? k.object_id : k.name
|
178
|
+
end
|
179
|
+
|
180
|
+
def determine_active_connection_name
|
181
|
+
key = active_connection_key
|
182
|
+
if active_connections[key] or configuration(key)
|
183
|
+
key
|
184
|
+
elsif self == ActiveLdap::Base
|
185
|
+
nil
|
186
|
+
else
|
187
|
+
superclass.active_connection_name
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
def clear_all_cached_connections!
|
192
|
+
if @@allow_concurrency
|
193
|
+
@@active_connections.each_value do |connection_hash_for_thread|
|
194
|
+
connection_hash_for_thread.each_value {|conn| conn.disconnect!}
|
195
|
+
connection_hash_for_thread.clear
|
196
|
+
end
|
197
|
+
else
|
198
|
+
@@active_connections.each_value {|conn| conn.disconnect!}
|
199
|
+
end
|
200
|
+
@@active_connections.clear
|
201
|
+
end
|
202
|
+
|
203
|
+
def guess_available_adapter
|
204
|
+
if Object.respond_to?(:java)
|
205
|
+
"jndi"
|
206
|
+
else
|
207
|
+
ruby_ldap_available = false
|
208
|
+
$LOAD_PATH.each do |path|
|
209
|
+
if File.exist?(File.join(path, "ldap", "ldif.rb"))
|
210
|
+
ruby_ldap_available = true
|
211
|
+
break
|
212
|
+
end
|
213
|
+
end
|
214
|
+
if !ruby_ldap_available and Object.const_defined?(:Gem)
|
215
|
+
ruby_ldap_available = gem_available?("ruby-ldap")
|
216
|
+
end
|
217
|
+
if ruby_ldap_available
|
218
|
+
"ldap"
|
219
|
+
else
|
220
|
+
"net-ldap"
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
def gem_available?(name)
|
226
|
+
not Gem::Specification.find_all_by_name(name).empty?
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
def setup_connection(config=nil)
|
231
|
+
config = self.class.ensure_configuration(config)
|
232
|
+
config = self.class.configuration.merge(config)
|
233
|
+
config = self.class.merge_configuration(config, self)
|
234
|
+
|
235
|
+
remove_connection
|
236
|
+
self.class.define_configuration(dn, config)
|
237
|
+
end
|
238
|
+
|
239
|
+
def establish_connection(config=nil)
|
240
|
+
message =
|
241
|
+
_("ActiveLdap::Connection#establish_connection has been deprecated " \
|
242
|
+
"since 1.1.0. " \
|
243
|
+
"Please use ActiveLdap::Connection#setup_connection instead.")
|
244
|
+
ActiveSupport::Deprecation.warn(message)
|
245
|
+
setup_connection(config)
|
246
|
+
end
|
247
|
+
|
248
|
+
def remove_connection
|
249
|
+
self.class.remove_connection(dn)
|
250
|
+
@connection = nil
|
251
|
+
end
|
252
|
+
|
253
|
+
def connection
|
254
|
+
conn = @connection
|
255
|
+
return conn if conn
|
256
|
+
|
257
|
+
have_dn = !@dn.nil?
|
258
|
+
if !have_dn and attribute_name_resolvable_without_connection?
|
259
|
+
begin
|
260
|
+
have_dn = !get_attribute_before_type_cast(dn_attribute)[1].nil?
|
261
|
+
rescue DistinguishedNameInvalid
|
262
|
+
end
|
263
|
+
end
|
264
|
+
conn = self.class.active_connections[dn] || retrieve_connection if have_dn
|
265
|
+
conn || self.class.connection
|
266
|
+
end
|
267
|
+
|
268
|
+
def connected?
|
269
|
+
connection != self.class.connection
|
270
|
+
end
|
271
|
+
|
272
|
+
def connection=(adapter)
|
273
|
+
if adapter.nil? or adapter.is_a?(Adapter::Base)
|
274
|
+
@connection = adapter
|
275
|
+
elsif adapter.is_a?(Hash)
|
276
|
+
config = adapter
|
277
|
+
@connection = self.class.instantiate_adapter(config)
|
278
|
+
else
|
279
|
+
setup_connection(adapter)
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
def retrieve_connection
|
284
|
+
conn = self.class.active_connections[dn]
|
285
|
+
return conn if conn
|
286
|
+
|
287
|
+
config = self.class.configuration(dn)
|
288
|
+
return nil unless config
|
289
|
+
|
290
|
+
conn = self.class.instantiate_adapter(config)
|
291
|
+
@connection = self.class.active_connections[dn] = conn
|
292
|
+
conn
|
293
|
+
end
|
294
|
+
|
295
|
+
def schema
|
296
|
+
connection.schema
|
297
|
+
end
|
298
|
+
end
|
299
|
+
end
|