Class: Ldap::NetLdapFacade

Inherits:
Object
  • Object
show all
Includes:
Slf4r::Logger
Defined in:
lib/ldap/net_ldap_facade.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ NetLdapFacade

Returns a new instance of NetLdapFacade.

Parameters:

  • config

    Hash for the ldap connection



18
19
20
21
22
23
24
25
# File 'lib/ldap/net_ldap_facade.rb', line 18

def initialize(config)
  @ldap_config = config
  if config.is_a? Hash
    @ldap = Net::LDAP.new( config )
  else
    @ldap = config
  end
end

Class Method Details

.open(config) ⇒ Object

Parameters:

  • config

    Hash for the ldap connection



9
10
11
12
13
# File 'lib/ldap/net_ldap_facade.rb', line 9

def self.open(config)
  Net::LDAP.open( config ) do |ldap|
    yield ldap
  end
end

Instance Method Details

#authenticate(dn, password) ⇒ Object

Parameters:

  • dn

    String for identifying the ldap object

  • password

    String to be used for authenticate to the dn



129
130
131
132
133
134
135
136
# File 'lib/ldap/net_ldap_facade.rb', line 129

def authenticate(dn, password)
  config = @ldap_config.merge(:auth => {
                                :method => :simple,
                                :username => dn,
                                :password => password
                              })
  Net::LDAP.new(config).bind
end

#base(treebase = nil, ldap_base = @ldap.base) ⇒ Object

helper to concat the base from the various parts

Parameters:

  • treebase (defaults to: nil)
  • ldap_base (defaults to: @ldap.base)

    the ldap_base defaulting to connection ldap_base

Returns:

  • the complete base String



150
151
152
# File 'lib/ldap/net_ldap_facade.rb', line 150

def base(treebase = nil, ldap_base = @ldap.base)
  [ treebase, ldap_base ].compact.join(",")
end

#create_object(dn_prefix, treebase, key_field, props, silence = false) ⇒ Object

Returns nil in case of an error or the new id of the created object.

Parameters:

  • dn_prefix

    String the prefix of the dn

  • treebase

    the treebase of the dn or any search

  • key_field

    field which carries the integer unique id of the entity

  • props

    Hash of the ldap attributes of the new ldap object

Returns:

  • nil in case of an error or the new id of the created object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ldap/net_ldap_facade.rb', line 44

def create_object(dn_prefix, treebase, key_field, props, silence = false)
  if @ldap.add( :dn => dn(dn_prefix, treebase),
                :attributes => props) || @ldap.get_operation_result.code.to_s == "0"
    props[key_field.to_sym]
  else
    unless silence
      msg = ldap_error("create",
                         dn(dn_prefix, treebase)) + "\n\t#{props.inspect}"
      # TODO maybe raise always an error
      if @ldap.get_operation_result.code.to_s == "68"
        raise ::DataMapper::PersistenceError.new(msg)
      else
        logger.warn(msg)
      end
    end
    nil
  end
end

#delete_object(dn_prefix, treebase) ⇒ Object

Returns nil in case of an error or true.

Parameters:

  • dn_prefix

    String the prefix of the dn

  • treebase

    the treebase of the dn or any search

Returns:

  • nil in case of an error or true



115
116
117
118
119
120
121
122
123
124
# File 'lib/ldap/net_ldap_facade.rb', line 115

def delete_object(dn_prefix, treebase)
  if @ldap.delete( :dn => dn(dn_prefix, treebase) )
    true
  else
    logger.warn(ldap_error("delete",
                           dn(dn_prefix, treebase)))

    nil
  end
end

#dn(dn_prefix, treebase) ⇒ Object

helper to concat the dn from the various parts

Parameters:

  • dn_prefix

    String the prefix of the dn

  • treebase

    the treebase of the dn or any search

Returns:

  • the complete dn String



142
143
144
# File 'lib/ldap/net_ldap_facade.rb', line 142

def dn(dn_prefix, treebase)
  [ dn_prefix, base(treebase) ].compact.join(",")
end

#read_objects(treebase, key_fields, conditions, field_names, order_field = nil) ⇒ Object

Returns Array of Hashes with a name/values pair for each attribute.

Parameters:

  • treebase

    the treebase of the search

  • key_fields

    Array of fields which carries the integer unique id(s) of the entity

  • Array

    of conditions for the search

Returns:

  • Array of Hashes with a name/values pair for each attribute



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ldap/net_ldap_facade.rb', line 67

def read_objects(treebase, key_fields, conditions, field_names, order_field = nil)

  # If there is a :dn in the filter skip everything and look it up
  if dn = conditions.detect { |c| c[1] == "dn" } then
    searchbase = dn[2]
    filter = nil
  else
    searchbase = base(treebase)
    filter = Conditions2Filter.convert(conditions)
  end

  result = []
  @ldap.search( :base => searchbase,
                :attributes => field_names,
                :filter => filter ) do |res|
    mapp = to_map(field_names, res)

    #puts map[key_field.to_sym]
    # TODO maybe make filter which removes this unless
    # TODO move this into the ldap_Adapter to make it more general, so that
    # all field with Integer gets converted, etc
    result << mapp if key_fields.all? do |key_field|
      mapp.keys.detect {|k| k.to_s.downcase == key_field.downcase }
    end
  end
  result
end

#retrieve_next_id(treebase, key_field) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ldap/net_ldap_facade.rb', line 27

def retrieve_next_id(treebase, key_field)
  id_sym = key_field.downcase.to_sym
  max = 0
  @ldap.search( :base => base(treebase),
                :attributes => [key_field],
                :return_result => false ) do |entry|
    n = entry[id_sym].first.to_i
    max = n if max < n
  end
  max + 1
end

#update_object(dn_prefix, treebase, actions) ⇒ Object

Returns nil in case of an error or true.

Parameters:

  • dn_prefix

    String the prefix of the dn

  • treebase

    the treebase of the dn or any search

  • actions

    the add/replace/delete actions on the attributes

Returns:

  • nil in case of an error or true



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ldap/net_ldap_facade.rb', line 100

def update_object(dn_prefix, treebase, actions)
  if @ldap.modify( :dn => dn(dn_prefix, treebase),
                   :operations => actions ) || @ldap.get_operation_result.code.to_s == "0"
    true
  else
    puts caller.join("\n")
    logger.warn(ldap_error("update",
                           dn(dn_prefix, treebase) + "\n\t#{actions.inspect}"))
    nil
  end
end