Module: Ldapter::Methods

Defined in:
lib/ldapter/methods.rb

Overview

These methods are accessible directly from the Ldapter object.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Object) adapter (readonly)

Returns the value of attribute adapter



49
50
51
# File 'lib/ldapter/methods.rb', line 49

def adapter
  @adapter
end

Instance Method Details

- (Object) /(*args)

Find an RDN relative to the base. This method is experimental.

class L < Ldapter::Class(:base => "DC=ruby-lang,DC=org", ...)
end

(L/{:cn => "Matz"}).dn #=> "CN=Matz,DC=ruby-lang,DC=org"


72
73
74
# File 'lib/ldapter/methods.rb', line 72

def /(*args)
  find(base.send(:/,*args))
end

- (Object) [](*args)

Like #/, only the search results are cached. This method is experimental.

L[:cn=>"Why"].bacon = "chunky"
L[:cn=>"Why"].bacon #=> "chunky"
L[:cn=>"Why"].save


82
83
84
85
86
87
88
# File 'lib/ldapter/methods.rb', line 82

def [](*args)
  if args.empty?
    @self ||= find(base)
  else
    self[][*args]
  end
end

- (Object) []=(*args)

Like Ldapter::Entry#[]= for the root node. Only works for assigning children. This method is experimental.

MyCompany[:cn=>"New Employee"] = MyCompany::User.new


94
95
96
# File 'lib/ldapter/methods.rb', line 94

def []=(*args) #:nodoc:
  self[].send(:[]=,*args)
end

- (Object) attribute_syntax(attribute)

Returns an Ldapter::Schema::LdapSyntax object encapsulating server provided information about the syntax of an attribute.

L.attribute_syntax(:cn).desc #=> "Directory String"


288
289
290
291
292
293
294
295
# File 'lib/ldapter/methods.rb', line 288

def attribute_syntax(attribute)
  type   = attribute_type(attribute)
  syntax = nil
  until type.nil? || syntax = type.syntax
    type = attribute_type(type.sup)
  end
  syntax
end

- (Object) attribute_type(attribute)

Returns an Ldapter::Schema::AttibuteType object encapsulating server provided information about an attribute type.

L.attribute_type(:cn).desc #=> "RFC2256: common name..."


281
282
283
# File 'lib/ldapter/methods.rb', line 281

def attribute_type(attribute)
  adapter.attribute_types[LDAP.encode(attribute)]
end

- (Object) authenticate(dn, password)

Verifies the given credentials are authorized to connect to the server by temporarily binding with them. Returns a boolean.



299
300
301
# File 'lib/ldapter/methods.rb', line 299

def authenticate(dn, password)
  adapter.authenticate(dn, password)
end

- (Object) base Also known as: dn

Access the base DN.



57
58
59
# File 'lib/ldapter/methods.rb', line 57

def base
  @base ||= LDAP::DN(adapter.default_base_dn,self)
end

- (Object) base=(dn)

Set a new base DN. Generally, the base DN should be set when the namespace is created and left unchanged.



53
54
55
# File 'lib/ldapter/methods.rb', line 53

def base=(dn)
  @base = LDAP::DN(dn,self)
end

- (Object) fetch(dn = self.dn, options = {})

A potential replacement or addition to find. Does not handle array arguments or do any of the active record monkey business.



159
160
161
# File 'lib/ldapter/methods.rb', line 159

def fetch(dn = self.dn, options = {}) #:nodoc:
  find_one(dn, options)
end

- (Object) filter(controller = nil)

Convenience method for use with Rails. Allows the singleton to be used as a before filter, an after filter, or an around filter.

class ApplicationController < ActionController::Base
  prepend_around_filter MyCompany
end

When invoked, the filter clears cached children. This operation is cheap and quite necessary if you care to avoid stale data.



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/ldapter/methods.rb', line 312

def filter(controller = nil)
  if controller
    reload
    if block_given?
      begin
        yield
      ensure
        reload
      end
    end
  else
    yield if block_given?
  end
  self
end

- (Object) find(dn = self.dn, options = {})

Find an absolute DN, raising an error when no results are found.

L.find("CN=Matz,DC=ruby-lang,DC=org")

A hash is treated as an RDN relative to the default base.

L.find(:cn=>"Matz")

Equivalent to

L.search(:base => dn, :scope => :base, :limit => true) or raise ...


169
170
171
172
173
174
175
176
177
# File 'lib/ldapter/methods.rb', line 169

def find(dn = self.dn, options = {})
  # Some misguided attempts to emulate active record.
  case dn
  when :all   then search({:limit => false}.merge(options))
  when :first then first(options)
  when Array  then dn.map {|d| fetch(d,options)}
  else             fetch(dn,options)
  end
end

- (Object) first(options = {})

Like #search, but only returns one entry.



180
181
182
# File 'lib/ldapter/methods.rb', line 180

def first(options = {})
  search(options.merge(:limit => true))
end

- (Object) logger



62
63
64
# File 'lib/ldapter/methods.rb', line 62

def logger
  @logger ||= adapter.logger
end

- (Object) object_class(klass)

Returns the object class for a given name or OID.

L.object_class("top") #=> L::Top


273
274
275
# File 'lib/ldapter/methods.rb', line 273

def object_class(klass)
  @object_classes[klass.to_s.tr('-','_').downcase]
end

- (Object) reload

Clears the cache of children. This cache is automatically populated when a child is accessed through #[].



100
101
102
103
104
105
# File 'lib/ldapter/methods.rb', line 100

def reload
  if @self
    @self.reload rescue nil
    @self = nil
  end
end

- (Object) root_dse(attrs = nil)

Retrieves attributes from the Root DSE. If attrs is an array, a hash is returned keyed on the attribute.

L.root_dse(:subschemaSubentry) #=> ["cn=Subschema"]


251
252
253
254
255
256
257
258
259
# File 'lib/ldapter/methods.rb', line 251

def root_dse(attrs = nil) #:nodoc:
  search(
    :base => "",
    :scope => :base,
    :attributes => attrs,
    :limit => true,
    :instantiate => false
  )
end

- (Object) schema(attrs = nil)

:nodoc:



261
262
263
264
265
266
267
268
# File 'lib/ldapter/methods.rb', line 261

def schema(attrs = nil) #:nodoc:
  search(
    :base => Array(root_dse(:subschemaSubentry)).first,
    :scope => :base,
    :attributes => attrs,
    :limit => true
  )
end

- (Object) search(options = {}, &block)

This is the core method for LDAP searching.

  • :base: The base DN of the search. The default is derived from either the :base option of the adapter configuration or by querying the server.

  • :scope: The scope of the search. Valid values are :base (find the base only), :onelevel (children of the base), and :subtree (the base, children, and all descendants). The default is :subtree.

  • :filter: A standard LDAP filter. This can be a string, an LDAP::Filter object, or parameters for LDAP::Filter().

  • :limit: Maximum number of results to return. If the value is a literal true, the first item is returned directly (or nil if nothing was found). For a literal false, an array always returned (the default).

  • :attributes: Specifies an Array of attributes to return. When unspecified, all attributes are returned. If this is not an Array but rather a String or a Symbol, an array of attributes is returned rather than an array of objects.

  • :instantiate: If this is false, a raw hash is returned rather than an Ldapter object. Combined with a String or Symbol argument to :attributes, a false value here causes the attribute not to be typecast.

Option examples:

# Returns all people.
MyCompany.search(:filter => {:objectClass => "person"})
# Returns an array of strings because givenName is marked as a singular value on this server.
MyCompany.search(:attribute => :givenName)
# Returns an array of arrays of strings.
MyCompany.search(:attribute => :givenName, :instantiate => false)
# Returns the first object found.
MyCompany.search(:limit => true)


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/ldapter/methods.rb', line 216

def search(options = {},&block)
  ary = []
  one_attribute = options[:attributes]
  if one_attribute.respond_to?(:to_ary)
    one_attribute = nil
  end
  options = search_options(options)
  if options[:limit] == true
    options[:limit] = 1
    first = true
  end
  adapter.search(options) do |entry|
    if options[:instantiate]
      klass = const_get("Top")
      entry = klass.instantiate(entry)
    end
    if one_attribute
      if entry.respond_to?(:read_attribute)
        entry = entry.send(:read_attribute,LDAP.encode(one_attribute))
      else
        entry = entry[LDAP.encode(one_attribute)]
      end
    end
    ary << entry
    block.call(entry) if block_given?
    return entry if first == true
    return ary   if options[:limit] == ary.size
  end
  first ? ary.first : ary
end

- (Object) to_ldapter

For duck typing.



7
8
9
# File 'lib/ldapter/methods.rb', line 7

def to_ldapter
  self
end