From 205be83b07b661f831d6548a58533231936d0a78 Mon Sep 17 00:00:00 2001 From: Thomas Flemming Date: Tue, 21 Feb 2012 22:52:25 +0100 Subject: [PATCH] doc --- examples/update_person_titles/ldap_util.rb | 86 ++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 examples/update_person_titles/ldap_util.rb diff --git a/examples/update_person_titles/ldap_util.rb b/examples/update_person_titles/ldap_util.rb new file mode 100644 index 0000000..7bcb1d5 --- /dev/null +++ b/examples/update_person_titles/ldap_util.rb @@ -0,0 +1,86 @@ +require 'rubygems' +require 'ldap' + +# sudo gem install ruby-ldap +# Returns realname from username +def ldap_realname(username) + + begin + # Workaround for bug in jruby-ldap-0.0.1: + LDAP::load_configuration() + rescue + + end + + ldap_host = 'ldap.uio.no' + conn = LDAP::Conn.new(ldap_host, LDAP::LDAP_PORT) + filter = "(uid=#{username})"; + base_dn = "dc=uio,dc=no" + + if conn.bound? then + conn.unbind() + end + + ansatt = nil + conn.bind do + + conn.search2("dc=uio,dc=no", LDAP::LDAP_SCOPE_SUBTREE, + "(uid=#{username})", nil, false, 0, 0).each do |entry| + + brukernavn = entry.to_hash["uid"][0] + fornavn = entry.to_hash["givenName"][0] + etternavn = entry.to_hash["sn"][0] + # epost = entry.to_hash["mail"][0] + # adresse = entry.to_hash["postalAddress"][0] + + return fornavn + " " + etternavn + end + end + +end + +def org_unit(id) + + require 'pp' + + begin + # Workaround for bug in jruby-ldap-0.0.1: + LDAP::load_configuration() + rescue + + end + + ldap_host = 'ldap.uio.no' + conn = LDAP::Conn.new(ldap_host, LDAP::LDAP_PORT) + filter = "(ou=#{id})"; + base_dn = "dc=uio,dc=no" + + if conn.bound? then + conn.unbind() + end + + ansatt = nil + conn.bind do + + # pp conn + + conn.search2("dc=uio,dc=no", LDAP::LDAP_SCOPE_SUBTREE, + "(ou=#{id})", nil, false, 0, 0).each do |entry| + + pp entry + pp entry.to_hash + return nil + brukernavn = entry.to_hash["uid"][0] + fornavn = entry.to_hash["givenName"][0] + etternavn = entry.to_hash["sn"][0] + # epost = entry.to_hash["mail"][0] + # adresse = entry.to_hash["postalAddress"][0] + + return fornavn + " " + etternavn + end + end + +end + +org_unit("160000") +