Class: ActiveLdap::Adapter::Base

Inherits:
Object
  • Object
show all
Includes:
GetTextSupport
Defined in:
lib/active_ldap/adapter/base.rb,
lib/active_ldap/adapter/jndi.rb,
lib/active_ldap/adapter/ldap.rb,
lib/active_ldap/adapter/net_ldap.rb

Direct Known Subclasses

Jndi, Ldap, NetLdap

Constant Summary collapse

VALID_ADAPTER_CONFIGURATION_KEYS =
[
  :host,
  :port,
  :method,
  :tls_options,
  :timeout,
  :retry_on_timeout,
  :retry_limit,
  :retry_wait,
  :bind_dn,
  :password,
  :password_block,
  :try_sasl,
  :sasl_mechanisms,
  :sasl_quiet,
  :allow_anonymous,
  :store_password,
  :scope,
  :sasl_options,
  :follow_referrals,
  :use_paged_results,
  :page_size,
]
@@row_even =
true

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GetTextSupport

included

Constructor Details

#initialize(configuration = {}) ⇒ Base

Returns a new instance of Base.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/active_ldap/adapter/base.rb', line 39

def initialize(configuration={})
  @connection = nil
  @disconnected = false
  @bound = false
  @bind_tried = false
  @entry_attributes = {}
  @follow_referrals = nil
  @page_size = nil
  @configuration = configuration.dup
  @logger = @configuration.delete(:logger)
  @configuration.assert_valid_keys(VALID_ADAPTER_CONFIGURATION_KEYS)
  VALID_ADAPTER_CONFIGURATION_KEYS.each do |name|
    instance_variable_set("@#{name}", configuration[name])
  end
  @follow_referrals = true if @follow_referrals.nil?
  @page_size ||= Configuration::DEFAULT_CONFIG[:page_size]
  @instrumenter = ActiveSupport::Notifications.instrumenter
end

Class Method Details

.jndi_connection(options) ⇒ Object



7
8
9
10
# File 'lib/active_ldap/adapter/jndi.rb', line 7

def jndi_connection(options)
  require 'active_ldap/adapter/jndi_connection'
  Jndi.new(options)
end

.ldap_connection(options) ⇒ Object



7
8
9
10
# File 'lib/active_ldap/adapter/ldap.rb', line 7

def ldap_connection(options)
  require 'active_ldap/adapter/ldap_ext'
  Ldap.new(options)
end

.net_ldap_connection(options) ⇒ Object



9
10
11
12
# File 'lib/active_ldap/adapter/net_ldap.rb', line 9

def net_ldap_connection(options)
  require 'active_ldap/adapter/net_ldap_ext'
  NetLdap.new(options)
end

Instance Method Details

#add(dn, entries, options = {}) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/active_ldap/adapter/base.rb', line 234

def add(dn, entries, options={})
  dn = ensure_dn_string(dn)
  begin
    operation(options) do
      yield(dn, entries)
    end
  rescue LdapError::NoSuchObject
    raise EntryNotFound, _("No such entry: %s") % dn
  rescue LdapError::InvalidDnSyntax
    raise DistinguishedNameInvalid.new(dn)
  rescue LdapError::AlreadyExists
    raise EntryAlreadyExist, _("%s: %s") % [$!.message, dn]
  rescue LdapError::StrongAuthRequired
    raise StrongAuthenticationRequired, _("%s: %s") % [$!.message, dn]
  rescue LdapError::ObjectClassViolation
    raise RequiredAttributeMissed, _("%s: %s") % [$!.message, dn]
  rescue LdapError::UnwillingToPerform
    raise OperationNotPermitted, _("%s: %s") % [$!.message, dn]
  end
end

#bind(options = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/active_ldap/adapter/base.rb', line 82

def bind(options={})
  @bind_tried = true

  bind_dn = ensure_dn_string(options[:bind_dn] || @bind_dn)
  try_sasl = options.has_key?(:try_sasl) ? options[:try_sasl] : @try_sasl
  if options.has_key?(:allow_anonymous)
    allow_anonymous = options[:allow_anonymous]
  else
    allow_anonymous = @allow_anonymous
  end
  options = options.merge(:allow_anonymous => allow_anonymous)

  # Rough bind loop:
  # Attempt 1: SASL if available
  # Attempt 2: SIMPLE with credentials if password block
  # Attempt 3: SIMPLE ANONYMOUS if 1 and 2 fail (or pwblock returns '')
  if try_sasl and sasl_bind(bind_dn, options)
    @logger.info {_('Bound to %s by SASL as %s') % [target, bind_dn]}
  elsif simple_bind(bind_dn, options)
    @logger.info {_('Bound to %s by simple as %s') % [target, bind_dn]}
  elsif allow_anonymous and bind_as_anonymous(options)
    @logger.info {_('Bound to %s as anonymous') % target}
  else
    message = yield if block_given?
    message ||= _('All authentication methods for %s exhausted.') % target
    raise AuthenticationError, message
  end

  @bound = true
  @bound
end

#bind_as_anonymous(options = {}) ⇒ Object



119
120
121
# File 'lib/active_ldap/adapter/base.rb', line 119

def bind_as_anonymous(options={})
  yield
end

#bound?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/active_ldap/adapter/base.rb', line 127

def bound?
  connecting? and @bound
end

#connect(options = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/active_ldap/adapter/base.rb', line 58

def connect(options={})
  host = options[:host] || @host
  method = options[:method] || @method || :plain
  port = options[:port] || @port || ensure_port(method)
  method = ensure_method(method)
  @disconnected = false
  @bound = false
  @bind_tried = false
  @connection, @uri, @with_start_tls = yield(host, port, method)
  prepare_connection(options)
  bind(options)
end

#connecting?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/active_ldap/adapter/base.rb', line 123

def connecting?
  !@connection.nil? and !@disconnected
end

#delete(targets, options = {}) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/active_ldap/adapter/base.rb', line 215

def delete(targets, options={})
  targets = [targets] unless targets.is_a?(Array)
  return if targets.empty?
  begin
    operation(options) do
      targets.each do |target|
        target = ensure_dn_string(target)
        begin
          yield(target)
        rescue LdapError::UnwillingToPerform, LdapError::InsufficientAccess
          raise OperationNotPermitted, _("%s: %s") % [$!.message, target]
        end
      end
    end
  rescue LdapError::NoSuchObject
    raise EntryNotFound, _("No such entry: %s") % target
  end
end

#disconnect!(options = {}) ⇒ Object



71
72
73
74
75
# File 'lib/active_ldap/adapter/base.rb', line 71

def disconnect!(options={})
  unbind(options)
  @connection = @uri = @with_start_tls = nil
  @disconnected = true
end

#entry_attribute(object_classes) ⇒ Object



170
171
172
173
# File 'lib/active_ldap/adapter/base.rb', line 170

def entry_attribute(object_classes)
  @entry_attributes[object_classes.uniq.sort] ||=
    EntryAttribute.new(schema, object_classes)
end

#modify(dn, entries, options = {}) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/active_ldap/adapter/base.rb', line 255

def modify(dn, entries, options={})
  dn = ensure_dn_string(dn)
  begin
    operation(options) do
      begin
        yield(dn, entries)
      rescue LdapError::UnwillingToPerform, LdapError::InsufficientAccess
        raise OperationNotPermitted, _("%s: %s") % [$!.message, target]
      end
    end
  rescue LdapError::UndefinedType
    raise
  rescue LdapError::ObjectClassViolation
    raise RequiredAttributeMissed, _("%s: %s") % [$!.message, dn]
  end
end

#modify_rdn(dn, new_rdn, delete_old_rdn, new_superior, options = {}) ⇒ Object



272
273
274
275
276
277
# File 'lib/active_ldap/adapter/base.rb', line 272

def modify_rdn(dn, new_rdn, delete_old_rdn, new_superior, options={})
  dn = ensure_dn_string(dn)
  operation(options) do
    yield(dn, new_rdn, delete_old_rdn, new_superior)
  end
end

#naming_contextsObject



161
162
163
# File 'lib/active_ldap/adapter/base.rb', line 161

def naming_contexts
  root_dse_values('namingContexts')
end

#rebind(options = {}) ⇒ Object



77
78
79
80
# File 'lib/active_ldap/adapter/base.rb', line 77

def rebind(options={})
  unbind(options) if bound?
  connect(options)
end

#schema(options = {}) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/active_ldap/adapter/base.rb', line 131

def schema(options={})
  @schema ||= operation(options) do
    base = options[:base]
    attrs = options[:attributes]

    attrs ||= [
      'objectClasses',
      'attributeTypes',
      'matchingRules',
      'matchingRuleUse',
      'dITStructureRules',
      'dITContentRules',
      'nameForms',
      'ldapSyntaxes',
      #'extendedAttributeInfo', # if we need RANGE-LOWER/UPPER.
    ]
    base ||= root_dse_values('subschemaSubentry', options)[0]
    base ||= 'cn=schema'
    schema = nil
    search(:base => base,
           :scope => :base,
           :filter => '(objectClass=subschema)',
           :attributes => attrs,
           :limit => 1) do |dn, attributes|
      schema = Schema.new(attributes)
    end
    schema || Schema.new([])
  end
end

#search(options = {}) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/active_ldap/adapter/base.rb', line 175

def search(options={})
  base = options[:base]
  base = ensure_dn_string(base)
  attributes = options[:attributes] || []
  attributes = attributes.to_a # just in case
  limit = options[:limit] || 0
  limit = nil if limit <= 0
  use_paged_results = options[:use_paged_results]
  use_paged_results = @use_paged_results if use_paged_results.nil?
  if use_paged_results
    use_paged_results = limit != 1 && supported_control.paged_results?
  end
  search_options = {
    base: base,
    scope: ensure_scope(options[:scope] || @scope),
    filter: parse_filter(options[:filter]) || 'objectClass=*',
    attributes: attributes,
    limit: limit,
    use_paged_results: use_paged_results,
    page_size: options[:page_size] || @page_size,
  }

  begin
    operation(options) do
      yield(search_options)
    end
  rescue LdapError::NoSuchObject, LdapError::InvalidDnSyntax => error
    # Do nothing on failure
    @logger.info do
      args = [
        error.class.class,
        error.message,
        search_options[:filter],
        search_options[:attributes].inspect,
      ]
      _("Ignore error %s(%s): filter %s: attributes: %s") % args
    end
  end
end

#supported_controlObject



165
166
167
168
# File 'lib/active_ldap/adapter/base.rb', line 165

def supported_control
  @supported_control ||=
    SupportedControl.new(root_dse_values("supportedControl"))
end

#unbind(options = {}) ⇒ Object



114
115
116
117
# File 'lib/active_ldap/adapter/base.rb', line 114

def unbind(options={})
  yield if @connection and (@bind_tried or bound?)
  @bind_tried = @bound = false
end