Module: Ldapmapper

Defined in:
lib/ldapmapper.rb

Overview

General module for LDAP CRUD Ojects

Defined Under Namespace

Classes: LdapMapper, LdapTemplate, LdapmapperAddRecordError, LdapmapperConnectionError, LdapmapperDeleteRecordError, LdapmapperGetAttibutAliasError, LdapmapperGetAttributsListError, LdapmapperGetBaseDnError, LdapmapperGetDnsListError, LdapmapperGetObjectClassListError, LdapmapperGetRecordError, LdapmapperModRecordError

Constant Summary collapse

LIB_NAME =

identity lib Library name

"Ldapmapper"
LIB_VERSION =

version of the library

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

add an ldap object

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

return a boolean



603
604
605
606
607
608
609
610
611
612
613
614
615
616
# File 'lib/ldapmapper.rb', line 603

def add_object(_dn, _record, _host='localhost',_port=389, _rootdn='', _passdn='')
  _record.delete('dn') 
  _data = _record
  _data.each{|_key,_value|
    _data[_key] = _value.to_a 
  }
  begin
    connector(_host,_port,_rootdn,_passdn).add("#{_dn}", _data)
    return true
  rescue LDAP::ResultError
    raise LdapmapperAddRecordError  
    return false
  end
end

#connector(_host = 'localhost', _port = 389, _rootdn = '', _passdn = '') ⇒ Object

global connector for LDAP instanciate at the first request

_host, _port, _rootdn and _passdn are optional

return a global connecter handler



457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/ldapmapper.rb', line 457

def connector(_host='localhost', _port=389, _rootdn='', _passdn='')
  begin 
    if not $connection then
      output "connecting to #{_host} on port : #{_port}" if $verbose
      $connection = LDAP::Conn.new(_host,_port)
      $connection.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3)
    end
      if _rootdn.empty? and not $bind then
        output 'Anonymous binding' if $verbose 
        $connection = $connection.bind
        $bind = true
      elsif not _rootdn.empty? and not $authenticated then
        output 'Authenticated binding' if $verbose
        $connection.unbind if $connection.bound?
        $connection = $connection.bind("#{_rootdn}", "#{_passdn}")
        $authenticated = true
      end
    return $connection
  rescue Exception
    raise LdapmapperConnectionError
  end
end

#delete_object(_dn, _host = 'localhost', _port = 389, _rootdn = '', _passdn = '') ⇒ Object

delete an ldap object

_dn is required, _host, _port, _rootdn and _passdn are optional

return a boolean



643
644
645
646
647
648
649
650
651
# File 'lib/ldapmapper.rb', line 643

def delete_object(_dn, _host='localhost',_port=389, _rootdn='', _passdn='')
  begin
    connector(_host,_port,_rootdn,_passdn).delete("#{_dn}")
    return true
  rescue LDAP::ResultError
    raise LdapmapperDeleteRecordError
    return false
  end
end

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

get the alias list of an attribute in Schema

_attribute is required, _host and _port are optionals

return an Array



520
521
522
523
524
525
526
527
528
529
530
# File 'lib/ldapmapper.rb', line 520

def get_alias(_attribute,_host='localhost',_port=389,_rootdn='',_passdn='') 
  _my_list_attributs = Array::new
  begin
    _schema = connector(_host,_port,_rootdn,_passdn).schema()
    _my_list_attributs = _schema.alias(_attribute)
  rescue
    raise LdapmapperGetAttributAliasError
  ensure
    return _my_list_attributs
  end
end

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

get the attributs list of an objectclass list

server free method

_list_objectclass is required, _host, _port, _rootdn and _passdn are optionals

return an Hash



559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# File 'lib/ldapmapper.rb', line 559

def get_attributs_list(_list_objectclass,_host='localhost',_port=389,_rootdn='',_passdn='')
  _my_list_attributs = Hash::new
  begin
    _schema = connector(_host,_port,_rootdn,_passdn).schema()
    _list_objectclass.each{|objectclass|
      if objectclass != 'top' then
        _prov_must = _schema.must(objectclass)
        _prov_may = _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
    }
  rescue
    raise LdapmapperGetAttributsListError
  ensure
    _my_list_attributs["dn"] = "MUST"
    _my_list_attributs["objectClass"] = "MUST"
    return _my_list_attributs
  end
end

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

get the base dn of an LDAP tree

_host, _port, _rootdn and _passdn are optionals

return a String



504
505
506
507
508
509
510
511
512
513
# File 'lib/ldapmapper.rb', line 504

def get_basedn(_host='localhost',_port=389,_rootdn='',_passdn='')
  _my_basedn = String::new('')
  begin
    _my_basedn = connector(_host,_port,_rootdn,_passdn).root_dse[0]["namingContexts"].to_s
  rescue
    raise LdapmapperGetBaseDnError
  ensure
    return _my_basedn
  end
end

#get_objectclass_list(_dn, _host = 'localhost', _port = 389, _rootdn = '', _passdn = '', _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, _rootdn, _passdn, _scope and _filter are optionals

return an Array



487
488
489
490
491
492
493
494
495
496
497
# File 'lib/ldapmapper.rb', line 487

def get_objectclass_list(_dn,_host='localhost',_port=389,_rootdn='',_passdn='',_scope=LDAP::LDAP_SCOPE_BASE,_filter='(objectClass=*)')
  _table_res = Array::new
  begin
    connector(_host, _port, _rootdn, _passdn).search(_dn,_scope,_filter){|_e|
      _table_res = _e.to_hash()['objectClass']
    }
  rescue
  ensure
    return _table_res
  end
end

#list_arbitrary_node(_dn, _host = localhost, _port = 389, _rootdn = '', _passdn = '', _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, _rootdn, _passdn, _scope, _filter are optionals

return an Array



539
540
541
542
543
544
545
546
547
548
549
550
# File 'lib/ldapmapper.rb', line 539

def list_arbitrary_node(_dn,_host=localhost,_port=389,_rootdn='',_passdn='',_scope=LDAP::LDAP_SCOPE_SUBTREE,_filter='(objectClass=*)')
  _table_res = Array::new
  begin
    connector(_host,_port,_rootdn,_passdn).search(_dn,_scope,_filter){|_e|
      _table_res.push(_e.dn)
    }
  rescue
    raise LdapmapperGetDnsListError
  ensure
    return _table_res
  end
end

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

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

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

return an Hash



585
586
587
588
589
590
591
592
593
594
595
596
# File 'lib/ldapmapper.rb', line 585

def map_record(_dn,_host='localhost',_port=389,_rootdn='',_passdn='',_scope=LDAP::LDAP_SCOPE_BASE,_filter='(objectClass=*)')
  _prov_hash = Hash::new
  begin
    connector(_host,_port,_rootdn,_passdn).search(_dn,_scope,_filter){|_e|
      _prov_hash = _e.to_hash()
    }
  rescue

  ensure
    return _prov_hash
  end
end

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

modify an ldap object

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

return a boolean



623
624
625
626
627
628
629
630
631
632
633
634
635
636
# File 'lib/ldapmapper.rb', line 623

def mod_object(_dn, _mod, _host='localhost',_port=389, _rootdn='', _passdn='')
#    begin
    _mod.delete('dn')
    _data = _mod
    _data.each{|_key,_value|
      _data[_key] = _value.to_a
    }
    connector(_host,_port,_rootdn,_passdn).modify("#{_dn}", _data)
    return true
#    rescue LDAP::ResultError
#      raise LdapmapperModRecordError
#      return false
#    end
end

#tests(_dn, _rootdn, _passdn) ⇒ Object

Module method for version display



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/ldapmapper.rb', line 154

def tests(_dn,_rootdn,_passdn)
 output "Running tests on #{_dn}"
 _dn = "ou=toto,#{_dn}"
 output "test on ou=toto in node : #{_dn}"
 record = LdapMapper.new(_dn,_rootdn,_passdn)
 output "- Could create it ? : #{record.can_create?}"
 output "- Already exist ? : #{record.exist?}"
 output "- Is it a node ? : #{record.is_node?}"
 output "- Is it the base ? : #{record.is_base?}"
  if record.can_create?
   output "- Create ou=toto in node : #{_dn}"
   record.add_objectclass!('organizationalUnit')
   record.ou = 'toto'
   record.description = "Test"
   output "- Is it valid ? : #{record.valid?}"
    record.commit!
 end
 if record.exist? then
   output "- ObjectClasses list :"
   record.list_objectclass.each{|objectclass|
     output "  * #{objectclass}"
   }
   output "- Attributes list : "
   record.list_attributs.each{|attribute,value|
     if value.size > 1 then
       output "* #{attribute} ="
       value.each{|val| puts "  - #{val}"
       }
     else
       output "* #{attribute} = #{value}"
     end
   }
   record.description = `date`
   record.commit!
#     output "deleting ou=toto..."
#     record.delete! 
   output ">> test done."
 end
end

#versionObject

Module method for version display



143
144
145
146
147
148
149
150
151
# File 'lib/ldapmapper.rb', line 143

def version
  output "#{LIB_NAME} : file  => #{File::basename(__FILE__)}:"
  output 'this is a RUBY library file'
  output "Copyright (c) Ultragreen Software"
  output "Version : #{LIB_VERSION}"
  output "Author : #{AUTHOR}"
  output "Date release : #{DATE}"
  output "Observation : #{OBS}"
end