Class: Ldapmapper::LdapMapper

Inherits:
LdapTemplate show all
Defined in:
lib/ldapmapper.rb

Overview

Mapping LDAP object class

This is the real CRUD Class

contructor arguments :

_dn and _passdn are required, _rootdn, _host and _port are optionals

Instance Attribute Summary collapse

Attributes inherited from LdapTemplate

#basedn_ldap, #filter_ldap, #host_ldap, #passdn_ldap, #port_ldap, #rootdn_ldap, #scope_ldap

Instance Method Summary collapse

Constructor Details

#initialize(_dn, _passdn, _rootdn = 'cn=root', _host = 'localhost', _port = 389) ⇒ LdapMapper

constructor with dn_ldap initialisation

_dn and _passdn are required, _rootdn, _host and _port are optionals

return a boolean



176
177
178
179
180
181
182
183
184
185
# File 'lib/ldapmapper.rb', line 176

def initialize(_dn,_passdn, _rootdn='cn=root',_host = 'localhost', _port = 389)
  _scope = LDAP::LDAP_SCOPE_SUBTREE
  _filter = '(objectClass=*)'
  super(_passdn, _rootdn, _host, _filter, _port, _scope )
  @dn_ldap = _dn
  @list_objectclass = Array::new
  @list_attributs_type = Hash::new
  @list_attributs = Hash::new
  add_objectclass!
end

Instance Attribute Details

#dn_ldapObject

DN binding point attribut



163
164
165
# File 'lib/ldapmapper.rb', line 163

def dn_ldap
  @dn_ldap
end

#list_attributsObject

Hash of attributes in LDIF mapping, value should be an array in case of multivalue data



169
170
171
# File 'lib/ldapmapper.rb', line 169

def list_attributs
  @list_attributs
end

#list_attributs_typeObject

Hash of attributes with optional or mandatory aspects in value



165
166
167
# File 'lib/ldapmapper.rb', line 165

def list_attributs_type
  @list_attributs_type
end

#list_objectclassObject

Array of objectclass for the current record



167
168
169
# File 'lib/ldapmapper.rb', line 167

def list_objectclass
  @list_objectclass
end

Instance Method Details

#add_objectclass!(_objectclass = 'top') ⇒ Object

add an objectclass in the list and map attribut

_objectclass is optional

return an Hash



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/ldapmapper.rb', line 192

def add_objectclass!(_objectclass = 'top')
  @list_objectclass = @list_objectclass.concat(get_objectclass_list(self.dn_ldap,self.host_ldap,self.port_ldap))
  @list_objectclass.push(_objectclass).uniq!
  @list_attributs_type = get_attributs_list(self.list_objectclass,self.host_ldap,self.port_ldap)

  @list_attributs = map_record(self.dn_ldap,self.host_ldap,self.port_ldap)
  if not @list_attributs.nil? or @list_attributs.empty? then
    @list_attributs.each{|_key,_value|
      @list_attributs_type.each{|_attr,_trash|
       @list_prov = self.get_alias(_key) 
		if @list_prov then
         if self.get_alias(_key).include?(_attr) then
    	      @list_attributs.delete(_key)
          	      @list_attributs[_attr] = _value
  end	
            end
      }
    }
  end
  @list_attributs["objectClass"] = @list_objectclass
  @list_attributs_type.each_key {|_key|
    eval("
    def #{_key.downcase}
      return @list_attributs['#{_key}']
    end
    def #{_key.downcase}=(_value)
      @list_attributs['#{_key}'] = _value
    end
	")
  }
end

#can_create?Boolean

test methode to check the ability to create the instance, already exist or not bindable

return a boolean

Returns:

  • (Boolean)


249
250
251
252
253
254
255
256
# File 'lib/ldapmapper.rb', line 249

def can_create?
  return false if self.is_base? 
  if list_arbitrary_node(self.get_previous,self.host_ldap,self.port_ldap).length >= 1 and not self.exist? then
	return true
  else
	return false
  end
end

#commit!Object

commit the modification or the adding of the object in LDAP server

return a boolean



330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/ldapmapper.rb', line 330

def commit!
  if self.exist? and self.valid? then
	# case modifying an LDAP object
	mod_object(self.dn_ldap, self.list_attributs,  self.rootdn_ldap, self.basedn_ldap, self.passdn_ldap, self.host_ldap, self.port_ldap)
	return true
  elsif self.can_create? and self.valid? then
	# case creating new object  
	add_object(self.dn_ldap, self.list_attributs,  self.rootdn_ldap, self.basedn_ldap, self.passdn_ldap, self.host_ldap, self.port_ldap)
	return true
  else
	return false
	# case can't commit
  end
end

#exist?Boolean

existance of an LDAP instance test method

return a boolean

Returns:

  • (Boolean)


227
228
229
230
231
232
233
# File 'lib/ldapmapper.rb', line 227

def exist?
  if list_arbitrary_node(self.dn_ldap,self.host_ldap,self.port_ldap).empty? then
    return false
  else
    return true
  end
end

#get_previousObject

get the previous record if exist and if the record is not the basedn

return a String



306
307
308
309
310
311
312
313
314
315
# File 'lib/ldapmapper.rb', line 306

def get_previous
  _rec_res = String::new('')
  if not self.is_base? then
	_rdn = String::new('')
	_dn_table = Array::new
	_rdn,*_dn_table = self.dn_ldap.split(',')
	_rec_res = _dn_table.join(',')
  end
  return _rec_res
end

#is_base?Boolean

return true if the dn to search is the basedn of the tree

return a boolean

Returns:

  • (Boolean)


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

def is_base?
  if self.dn_ldap == self.basedn_ldap then
	return true
  else
	return false
  end
end

#is_node?Boolean

test methode for LDAP instance situation node or termination

return a boolean

Returns:

  • (Boolean)


238
239
240
241
242
243
244
# File 'lib/ldapmapper.rb', line 238

def is_node?
  if  list_arbitrary_node(self.dn_ldap,self.host_ldap,self.port_ldap).length > 1 then
	return true
  else
	return false
  end
end

#list_nodeObject

method to list dn after the node in the the LDAP tree for the first level,

return an Array



320
321
322
323
324
325
# File 'lib/ldapmapper.rb', line 320

def list_node
  _my_res = Array::new
  _my_res = list_arbitrary_node(self.dn_ldap,self.host_ldap,self.port_ldap,LDAP::LDAP_SCOPE_ONELEVEL) 
  _my_res.delete(self.dn_ldap) if _my_res.include?(self.dn_ldap)
  return _my_res
end

#mayObject

return the attributes list how may be present in the record

return an Array



284
285
286
287
288
289
290
# File 'lib/ldapmapper.rb', line 284

def may
  _may_list = Array::new
  self.list_attributs_type.each{|_key,_value|
    _may_list.push(_key) if _value == 'MAY'
  }
  return _may_list
end

#mustObject

return the list of the attributes how must be present for add a record

return an Array



272
273
274
275
276
277
278
279
# File 'lib/ldapmapper.rb', line 272

def must
  _must_list = Array::new
  self.list_attributs_type.each{|_key,_value|
	_must_list.push(_key) if _value == 'MUST'  
  }
  _must_list.delete('dn') if _must_list.include?('dn')
  return _must_list
end

#valid?Boolean

return true if the must attributes is completed in record before commit!

return a boolean

Returns:

  • (Boolean)


295
296
297
298
299
300
301
# File 'lib/ldapmapper.rb', line 295

def valid?
  _result = true
  self.must.each{|attribute|
	_result = false if not self.list_attributs.include?(attribute)
  }
  return _result
end