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, _rootdn = '', _passdn = '', _host = 'localhost', _port = 389) ⇒ LdapMapper

constructor with dn_ldap initialisation

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

return a boolean



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

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

Instance Attribute Details

#dn_ldapObject

DN binding point attribut



246
247
248
# File 'lib/ldapmapper.rb', line 246

def dn_ldap
  @dn_ldap
end

#list_attributsObject

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



252
253
254
# File 'lib/ldapmapper.rb', line 252

def list_attributs
  @list_attributs
end

#list_attributs_rollbackObject

Returns the value of attribute list_attributs_rollback.



253
254
255
# File 'lib/ldapmapper.rb', line 253

def list_attributs_rollback
  @list_attributs_rollback
end

#list_attributs_typeObject

Hash of attributes with optional or mandatory aspects in value



248
249
250
# File 'lib/ldapmapper.rb', line 248

def list_attributs_type
  @list_attributs_type
end

#list_objectclassObject

Array of objectclass for the current record



250
251
252
# File 'lib/ldapmapper.rb', line 250

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



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/ldapmapper.rb', line 279

def add_objectclass!(_objectclass = 'top')
  @list_objectclass = @list_objectclass.concat(get_objectclass_list(self.dn_ldap, self.host_ldap, self.port_ldap, self.rootdn_ldap, self.passdn_ldap))
  @list_objectclass.push(_objectclass).uniq!
  @list_attributs_type = get_attributs_list(self.list_objectclass, self.host_ldap, self.port_ldap, self.rootdn_ldap, self.passdn_ldap)
  @list_attributs = map_record(self.dn_ldap, self.host_ldap, self.port_ldap, self.rootdn_ldap, self.passdn_ldap)
  if not @list_attributs.nil? or @list_attributs.empty? then
    @list_attributs.each{|_key,_value|
      @list_attributs_type.each{|_attr,_trash|
 @list_prov = get_alias(_key,self.host_ldap, self.port_ldap, self.rootdn_ldap, self.passdn_ldap) 
        if @list_prov then
          if @list_prov.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)


342
343
344
345
346
347
348
349
# File 'lib/ldapmapper.rb', line 342

def can_create?
  return false if self.is_base? 
  if list_arbitrary_node(self.get_previous,self.host_ldap,self.port_ldap, self.rootdn_ldap, self.passdn_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



434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/ldapmapper.rb', line 434

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

#delete!Object

delete the dn object in LDAP server

return a boolean



423
424
425
426
427
428
429
# File 'lib/ldapmapper.rb', line 423

def delete!
  if self.exist? and not self.is_node? then
	return delete_object(self.dn_ldap, self.host_ldap, self.port_ldap, self.rootdn_ldap, self.passdn_ldap)
  else
    return false
  end
end

#exist?Boolean

existance of an LDAP instance test method

return a boolean

Returns:

  • (Boolean)


313
314
315
316
317
318
319
# File 'lib/ldapmapper.rb', line 313

def exist?
  if list_arbitrary_node(self.dn_ldap, self.host_ldap, self.port_ldap, self.rootdn_ldap, self.passdn_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



399
400
401
402
403
404
405
406
407
408
# File 'lib/ldapmapper.rb', line 399

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)


354
355
356
357
358
359
360
# File 'lib/ldapmapper.rb', line 354

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)


331
332
333
334
335
336
337
# File 'lib/ldapmapper.rb', line 331

def is_node?
  if  list_arbitrary_node(self.dn_ldap, self.host_ldap, self.port_ldap, self.rootdn_ldap, self.passdn_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



413
414
415
416
417
418
# File 'lib/ldapmapper.rb', line 413

def list_node
  _my_res = Array::new
  _my_res = list_arbitrary_node(self.dn_ldap,self.host_ldap,self.port_ldap, self.rootdn_ldap, self.passdn_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



377
378
379
380
381
382
383
# File 'lib/ldapmapper.rb', line 377

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



365
366
367
368
369
370
371
372
# File 'lib/ldapmapper.rb', line 365

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

#rollback!Object

rollback to beggining of transaction

return a boolean



324
325
326
# File 'lib/ldapmapper.rb', line 324

def rollback!
  @list_attributs = @list_attributs_rollback
end

#valid?Boolean

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

return a boolean

Returns:

  • (Boolean)


388
389
390
391
392
393
394
# File 'lib/ldapmapper.rb', line 388

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