Class: Ldaptic::Adapters::AbstractAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/ldaptic/adapters/abstract_adapter.rb

Overview

Subclasse must implement search, add, modify, delete, and rename. These methods should return 0 on success and non-zero on failure. The failure code is intended to be the server error code. If this is unavailable, return -1.

Direct Known Subclasses

LDAPConnAdapter, NetLDAPAdapter

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ AbstractAdapter

Returns a new instance of AbstractAdapter.



24
25
26
# File 'lib/ldaptic/adapters/abstract_adapter.rb', line 24

def initialize(options)
  @options = options
end

Class Method Details

.register_as(name) ⇒ Object

When implementing an adapter, register_as must be called to associate the adapter with a name. The adapter name must mimic the filename. The following might be found in ldaptic/adapters/some_adapter.rb.

class SomeAdapter < AbstractAdapter
  register_as(:some)
end


19
20
21
22
# File 'lib/ldaptic/adapters/abstract_adapter.rb', line 19

def self.register_as(name)
  require 'ldaptic/adapters'
  Ldaptic::Adapters.register(name, self)
end

Instance Method Details

#attribute_type(key = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/ldaptic/adapters/abstract_adapter.rb', line 75

def attribute_type(key = nil)
  if key
    attribute_types[key] || attribute_types.values.detect do |at|
      at.names.map {|n| n.downcase}.include?(key.downcase)
    end
  else
    attribute_types.values.uniq
  end
end

#attribute_typesObject

Returns a hash of attribute types, keyed by both OID and name.



70
71
72
73
# File 'lib/ldaptic/adapters/abstract_adapter.rb', line 70

def attribute_types
  @attribute_types ||= construct_schema_hash('attributeTypes',
    Ldaptic::Schema::AttributeType)
end

#compare(dn, attr, value) ⇒ Object

Default compare operation, emulated with a search.



98
99
100
101
# File 'lib/ldaptic/adapters/abstract_adapter.rb', line 98

def compare(dn, attr, value)
  search(:base => dn, :scope => Ldaptic::SCOPES[:base], :filter => "(#{attr}=#{Ldaptic.escape(value)})") { return true }
  false
end

#dit_content_rulesObject

Returns a hash of DIT content rules, keyed by both OID and name.



86
87
88
89
# File 'lib/ldaptic/adapters/abstract_adapter.rb', line 86

def dit_content_rules
  @dit_content_rules ||= construct_schema_hash('dITContentRules',
    Ldaptic::Schema::DITContentRule)
end

#loggerObject



103
104
105
# File 'lib/ldaptic/adapters/abstract_adapter.rb', line 103

def logger
  @logger || Ldaptic.logger
end

#object_classesObject

Returns a hash of object classes, keyed by both OID and name.



92
93
94
95
# File 'lib/ldaptic/adapters/abstract_adapter.rb', line 92

def object_classes
  @object_classes ||= construct_schema_hash('objectClasses',
    Ldaptic::Schema::ObjectClass)
end

#root_dse(attrs = nil) ⇒ Object

The server’s RootDSE. attrs is an array specifying which attributes to return.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ldaptic/adapters/abstract_adapter.rb', line 30

def root_dse(attrs = nil)
  result = search(
    :base => "",
    :scope => Ldaptic::SCOPES[:base],
    :filter => "(objectClass=*)",
    :attributes => attrs && [attrs].flatten.map {|a| Ldaptic.encode(a)},
    :disable_pagination => true
  ) { |x| break x }
  return if result.kind_of?(Fixnum)
  if attrs.kind_of?(Array) || attrs.nil?
    result
  else
    result[attrs]
  end
end

#schema(attrs = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/ldaptic/adapters/abstract_adapter.rb', line 46

def schema(attrs = nil)
  @subschema_dn ||= root_dse(['subschemaSubentry'])['subschemaSubentry'].first
  search(
    :base => @subschema_dn,
    :scope => Ldaptic::SCOPES[:base],
    :filter => "(objectClass=subschema)",
    :attributes => attrs
  ) { |x| return x }
  nil
end

#server_default_base_dnObject Also known as: default_base_dn

Returns the first of the namingContexts found in the RootDSE.



58
59
60
61
62
63
64
65
# File 'lib/ldaptic/adapters/abstract_adapter.rb', line 58

def server_default_base_dn
  unless defined?(@naming_contexts)
    @naming_contexts = root_dse(%w(namingContexts))
  end
  if @naming_contexts
    @naming_contexts["namingContexts"].to_a.first
  end
end