Class: Ldapter::Adapters::NetLDAPAdapter

Inherits:
AbstractAdapter show all
Defined in:
lib/ldapter/adapters/net_ldap_adapter.rb

Constant Summary

DEFAULT_CAPITALIZATIONS =
objectClass

  objectClasses
  attributeTypes
  matchingRules
  matchingRuleUse
  dITStructureRules
  dITContentRules
  nameForms
  ldapSyntaxes

  configurationNamingContext
  currentTime
  defaultNamingContext
  dn
  dnsHostName
  domainControllerFunctionality
  domainFunctionality
  dsServiceName
  forestFunctionality
  highestCommittedUSN
  isGlobalCatalogReady
  isSynchronized
  ldapServiceName
  namingContexts
  rootDomainNamingContext
  schemaNamingContext
  serverName
  subschemaSubentry
  supportedCapabilities
  supportedControl
  supportedLDAPPolicies
  supportedLDAPVersion
  supportedSASLMechanisms
].inject({}) { |h,k| h[k.downcase] = k; h }

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from AbstractAdapter

#attribute_type, #attribute_types, #compare, #dit_content_rules, #logger, #object_classes, register_as, #root_dse, #schema, #server_default_base_dn

Constructor Details

- (NetLDAPAdapter) initialize(options)

A new instance of NetLDAPAdapter



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ldapter/adapters/net_ldap_adapter.rb', line 9

def initialize(options)
  require 'net/ldap'
  require 'ldapter/adapters/net_ldap_ext'
  if defined?(::Net::LDAP) && options.kind_of?(::Net::LDAP)
    options = {:adapter => :net_ldap, :connection => option}
  else
    options = (options || {}).dup
  end
  if connection = options[:connection]
    auth       = connection.instance_variable_get(:@auth) || {}
    encryption = connection.instance_variable_get(:@encryption)
    options = {
      :adapter => :net_ldap,
      :host => connection.host,
      :port => connection.port,
      :base => connection.base == "dc=com" ? nil : connection.base,
      :username => auth[:username],
      :password => auth[:password]
    }.merge(options)
    if encryption
      options[:encryption] ||= encryption
    end
  else
    if options[:username]
      auth = {:method => :simple, :username => options[:username], :password => options[:password]}
    else
      auth = {:method => :anonymous}
    end
    options[:connection] ||= ::Net::LDAP.new(
      :host => options[:host],
      :port => options[:port],
      :encryption => options[:encryption],
      :auth => auth
    )
  end
  @connection = options.delete(:connection)
  @logger     = options.delete(:logger)
  super(options)
end

Instance Attribute Details

- (Object) connection (readonly)

Returns the value of attribute connection



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

def connection
  @connection
end

Instance Method Details

- (Object) add(dn, attributes)



51
52
53
54
# File 'lib/ldapter/adapters/net_ldap_adapter.rb', line 51

def add(dn, attributes)
  connection.add(:dn => dn, :attributes => attributes)
  handle_errors
end

- (Object) authenticate(dn, password)

Convenience method which returns true if the credentials are valid, and false otherwise. The credentials are discarded afterwards.



129
130
131
132
133
134
135
136
137
# File 'lib/ldapter/adapters/net_ldap_adapter.rb', line 129

def authenticate(dn, password)
  conn = Net::LDAP.new(
    :host => @options[:host],
    :port => @options[:port],
    :encryption => @options[:encryption],
    :auth => {:method => :simple, :username => dn, :password => password}
  )
  conn.bind
end

- (Object) default_base_dn



139
140
141
# File 'lib/ldapter/adapters/net_ldap_adapter.rb', line 139

def default_base_dn
  @options[:base] || server_default_base_dn
end

- (Object) delete(dn)



67
68
69
70
# File 'lib/ldapter/adapters/net_ldap_adapter.rb', line 67

def delete(dn)
  connection.delete(:dn => dn)
  handle_errors
end

- (Object) inspect



143
144
145
# File 'lib/ldapter/adapters/net_ldap_adapter.rb', line 143

def inspect
  "#<#{self.class} #{@connection.inspect}>"
end

- (Object) modify(dn, attributes)



56
57
58
59
60
61
62
63
64
65
# File 'lib/ldapter/adapters/net_ldap_adapter.rb', line 56

def modify(dn, attributes)
  if attributes.kind_of?(Hash)
    attributes = attributes.map {|k,v| [:replace, k, v]}
  end
  connection.modify(
    :dn => dn,
    :operations => attributes
  )
  handle_errors
end

- (Object) rename(dn, new_rdn, delete_old, new_superior = nil)



72
73
74
75
# File 'lib/ldapter/adapters/net_ldap_adapter.rb', line 72

def rename(dn, new_rdn, delete_old, new_superior = nil)
  connection.rename(:olddn => dn, :newrdn => new_rdn, :delete_attributes => delete_old, :newsuperior => new_superior)
  handle_errors
end

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



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ldapter/adapters/net_ldap_adapter.rb', line 114

def search(options = {}, &block)
  options = options.merge(:return_result => false)
  connection.search(options) do |entry|
    hash = {}
    entry.each do |attr,val|
      attr = recapitalize(attr)
      hash[attr] = val
    end
    block.call(hash)
  end
  handle_errors
end