Module: Ldapmapper

Defined in:
lib/ldapmapper.rb

Overview

General module for LDAP CRUD Ojects

Defined Under Namespace

Classes: LdapMapper, LdapTemplate

Constant Summary collapse

LIB_VERSION =

identity lib version of the library

'1.3'
AUTHOR =

name of the author

'Romain GEORGES'
DATE =

date of creation

'30/07/2005'
OBS =

valuable observations

'Generic LDAP class'

Instance Method Summary collapse

Instance Method Details

#add_object(_dn, _record, _rootdn, _basedn, _passdn, _host = 'localhost', _port = 389) ⇒ Object

add an ldap object

_dn, _record, _rootdn, _basedn and _passdn are required, _host and _port are optional

return a boolean



485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/ldapmapper.rb', line 485

def add_object(_dn, _record,  _rootdn, _basedn, _passdn, _host='localhost',_port=389)
  _conn = LDAP::Conn.new(_host, _port)
  _conn.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3)
  _record.delete('dn')
  _conn.bind("#{_rootdn}", "#{_passdn}"){
    begin
	_data = self.list_attributs
	_data.each{|_key,_value|
 _data[_key] = _value.to_a 
	}
	_conn.add("#{_dn}", _data)
	return true
    rescue LDAP::ResultError
	return false
    end
  }
end

#get_alias(_attribute, _host = 'localhost', _port = 389) ⇒ Object

get the alias list of an attribute in Schema

_attribute is required, _host and _port are optionals

return an Array



393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/ldapmapper.rb', line 393

def get_alias(_attribute,_host='localhost',_port=389) 
  _my_list_attributs = Array::new
  begin
    _conn = LDAP::Conn.new(_host, _port)
    _conn.bind{
      _schema = _conn.schema()
	_my_list_attributs = _schema.alias(_attribute)
    }
    
  ensure
    return _my_list_attributs
  end
end

#get_attributs_list(_list_objectclass, _host = 'localhost', _port = 389) ⇒ Object

get the attributs list of an objectclass list

server free method

_list_objectclass is required, _host and _port are optionals

return an Hash



437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/ldapmapper.rb', line 437

def get_attributs_list(_list_objectclass,_host='localhost',_port=389)
  _my_list_attributs = Hash::new
  begin
    _conn = LDAP::Conn.new(_host, _port)
    _conn.bind{
	_schema = _conn.schema()
	_list_objectclass.each{|objectclass|
 if objectclass != 'top' then

   _prov_may = _schema.must(objectclass)
    _prov_must = _schema.may(objectclass)
   _prov_must.each{|attributs| _my_list_attributs[attributs] = 'MUST'} unless _prov_must.nil? or _prov_must.empty?
   _prov_may.each{|attributs| _my_list_attributs[attributs] = 'MAY'} unless _prov_may.nil? or _prov_may.empty?
 end
	}
    }
 ensure
    _my_list_attributs["dn"] = "MUST"
    _my_list_attributs["objectClass"] = "MUST"
    return _my_list_attributs      

 end
end

#get_basedn(_host = 'localhost', _port = 389) ⇒ Object

get the base dn of an LDAP tree

_host and _port are optionals

return a String



375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/ldapmapper.rb', line 375

def get_basedn(_host='localhost',_port=389)
  _my_basedn = String::new('')
  begin
    _conn = LDAP::Conn.new(_host,_port)
    _conn.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3)
    _conn.bind {
	_my_basedn = _conn.root_dse[0]["namingContexts"].to_s
    }
  ensure
    return _my_basedn
  end
end

#get_objectclass_list(_dn, _host = 'localhost', _port = 389, _scope = LDAP::LDAP_SCOPE_BASE, _filter = '(objectClass=*)') ⇒ Object

global method that list objectclass for a speficique dn

server free methode

_dn is required, _host, _port, _scope and _filter are optionals

return an Array



355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/ldapmapper.rb', line 355

def get_objectclass_list(_dn,_host='localhost',_port=389,_scope=LDAP::LDAP_SCOPE_BASE,_filter='(objectClass=*)')
  _table_res = Array::new
  begin
    _conn = LDAP::Conn.new(_host,_port)
    _conn.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3)
    _conn.bind {
	_conn.search(_dn,_scope,_filter){|_e|
  _table_res = _e.to_hash()['objectClass']
	}
    }
  ensure
    return _table_res
  end
end

#list_arbitrary_node(_dn, _host = localhost, _port = 389, _scope = LDAP::LDAP_SCOPE_SUBTREE, _filter = '(objectClass=*)') ⇒ Object

global method that list dn after the precised dn in the LDAP tree

server free methode

_dn id required, _host, _port, _scope, _filter are optionals

return an Array



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/ldapmapper.rb', line 414

def list_arbitrary_node(_dn,_host=localhost,_port=389,_scope=LDAP::LDAP_SCOPE_SUBTREE,_filter='(objectClass=*)')
  _table_res = Array::new
  begin
    _conn = LDAP::Conn.new(_host,_port)
    _conn.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3)
    _conn.bind {
	_conn.search(_dn,_scope,_filter){|_e|
 _table_res.push(_e.dn)
	}
    }
  ensure
    return _table_res
  end
  
end

#map_record(_dn, _host = 'localhost', _port = 389, _scope = LDAP::LDAP_SCOPE_SUBTREE, _filter = '(objectClass=*)') ⇒ Object

map the attributs of class at run time for the current LDAP Object at precise DN

_dn is required, _host, _port, _scope and _filter are optionals

return an Hash



466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/ldapmapper.rb', line 466

def map_record(_dn,_host='localhost',_port=389,_scope=LDAP::LDAP_SCOPE_SUBTREE,_filter='(objectClass=*)')
  begin
    _conn = LDAP::Conn.new(_host,_port)
    _conn.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3)
    _conn.bind {
	_conn.search(_dn,_scope,_filter){|_e|
 return _e.to_hash()
	}
    }
  rescue
    return Hash::new
  end
end

#mod_object(_dn, _record, _rootdn, _basedn, _passdn, _host = 'localhost', _port = 389) ⇒ Object

modify an ldap object

_dn, _record, _rootdn, _basedn and _passdn are required, _host and _port are optional

return a boolean



508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# File 'lib/ldapmapper.rb', line 508

def mod_object(_dn, _record,  _rootdn, _basedn, _passdn, _host='localhost',_port=389)
  _conn = LDAP::Conn.new(_host, _port)
  _conn.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3)
  _record.delete('dn')
  _conn.bind("#{_rootdn}", "#{_passdn}"){
    begin
	_conn.delete("#{_dn}")
	_data = self.list_attributs
      _data.each{|_key,_value|
        _data[_key] = _value.to_a
      }
      _conn.add("#{_dn}", _data)
	return true
    rescue LDAP::ResultError
      return false
    end
  }
end