Project

General

Profile

Download (9.51 KB) Statistics
| Branch: | Tag: | Revision:
02029fe9 Marc Dequènes (Duck)
#!/usr/bin/ruby -Ku

$KCODE = 'UTF8'
require 'jcode'
require 'yaml'
require 'active_ldap'
require 'cmdparse2'
require 'pathname'

c26f67e5 Marc Dequènes (Duck)
class Hash
def recursive_symbolize_keys!
symbolize_keys!
values.select { |v| v.is_a?(Hash) }.each { |h| h.recursive_symbolize_keys! }
self
end
end

02029fe9 Marc Dequènes (Duck)
config_str_prv = IO.read("config/private.conf")
config_str = IO.read("config/test.conf")
c26f67e5 Marc Dequènes (Duck)
config = YAML.load(config_str_prv).merge(YAML.load(config_str)).recursive_symbolize_keys!
ActiveLdap::Base.setup_connection(config[:ldap])
02029fe9 Marc Dequènes (Duck)
cmdparser = CmdParse::CommandParser.new(true)
cmdparser.program_name = ""
cmdparser.program_version = [0, 0, 1]

cmdparser.options = CmdParse::OptionParserWrapper.new do |opt|
opt.separator "Global options:"
opt.on("--debug", "Output debug info without being formated") {|t| $debug_opt = true }
opt.on("--expert", "Output extra info for expert users") {|t| $expert_opt = true }
end

cmdparser.add_command(CmdParse::HelpCommand.new)
cmdparser.add_command(CmdParse::VersionCommand.new)



module LdapMapper
class LdapObject < ActiveLdap::Base
class_inheritable_accessor :presentation, :mapper

def name
self[dn_attribute].is_a?(Array) ? self[dn_attribute][0] : self[dn_attribute]
end

def description
[self.class.presentation[:desc_attribute], 'displayName', 'cn', 'description'].each do |attr|
if self.has_attribute?(attr) and self.attribute_present?(attr)
return self[attr].is_a?(Array) ? self[attr][0] : self[attr]
end
end
return ""
end

def aspects
present_aspects = {}
self.class.presentation[:allowed_aspects].each do |aspect|
aspect_data = self.class.mapper.get_aspect(aspect)
c26f67e5 Marc Dequènes (Duck)
aspect_mapping = aspect_data[:mapping]
present_aspects[aspect] = aspect_data if self.classes & aspect_mapping[:classes] == aspect_mapping[:classes]
02029fe9 Marc Dequènes (Duck)
end

present_aspects
end
c26f67e5 Marc Dequènes (Duck)
def objectclasses_attr_list(objectclass_list)
objectclass_list = [objectclass_list] unless objectclass_list.is_a? Array
list = []
objectclass_list.each do |objectclass|
objectclass_obj = ActiveLdap::Base.schema.object_class(objectclass)
attr_list = objectclass_obj.must + objectclass_obj.may
list += attr_list.collect{|attr| attr.human_attribute_name }
end
list
end

def organized_attributes(expert = false)
ignored_attrs = self.class.presentation[:hidden_attributes] || []
ignored_attrs += (self.class.presentation[:expert_attributes] || []) unless expert
attr_list = self.attributes.keys - ignored_attrs

obj_aspects = {}
present_aspects_data = self.aspects
self.class.presentation[:allowed_aspects].each do |aspect|
aspect_data = present_aspects_data[aspect]
next unless aspect_data.has_key?(:presentation) and aspect_data[:presentation] and aspect_data[:presentation].has_key?(:associated_attributes)

taken_attr_list = aspect_data[:presentation][:associated_attributes] & attr_list
taken_attr_data = self.attributes.select {|key, val| taken_attr_list.include?(key) }
obj_aspects[aspect] = Hash[taken_attr_data]
attr_list -= taken_attr_list
end

obj_info = {}
if self.class.presentation.has_key?(:associated_attributes)
taken_attr_list = self.class.presentation[:associated_attributes] & attr_list
elsif self.class.presentation[:associate_unclaimed_attributes]
taken_attr_list = attr_list
else
taken_attr_list = (objectclasses_attr_list(self.required_classes + (self.class.presentation[:optional_classes] || [])) & attr_list)
end
taken_attr_data = self.attributes.select {|key, val| taken_attr_list.include?(key) }
obj_info = Hash[taken_attr_data]

[obj_info, obj_aspects]
end
02029fe9 Marc Dequènes (Duck)
end

class Controller
def initialize(mod_container = LdapMapper::Objects)
@mod_container = mod_container
@object_definitions = {}
@aspects = {}
end

def set_aspect(aspect_name, aspect_def)
@aspects[aspect_name] = aspect_def
end

def get_aspect(aspect_name)
c26f67e5 Marc Dequènes (Duck)
@aspects[aspect_name.to_sym]
02029fe9 Marc Dequènes (Duck)
end

def self.object_name_to_klass_name(obj_name)
c26f67e5 Marc Dequènes (Duck)
"LdapObject" + obj_name.to_s.capitalize
02029fe9 Marc Dequènes (Duck)
end

def load_object(obj_name, obj_def)
c26f67e5 Marc Dequènes (Duck)
obj_mapping = obj_def[:mapping]
02029fe9 Marc Dequènes (Duck)
klass_name = self.class.object_name_to_klass_name(obj_name)

# create class
@mod_container.module_eval(<<-EOS)
class #{klass_name} < LdapMapper::LdapObject; end
EOS

# configure class
klass = find_klass(obj_name)
c26f67e5 Marc Dequènes (Duck)
klass.presentation = obj_def[:presentation]
02029fe9 Marc Dequènes (Duck)
klass.mapper = self
klass.ldap_mapping obj_mapping.reject {|key, val| not ActiveLdap::Base::VALID_LDAP_MAPPING_OPTIONS.include?(key) }

# store definition for later associations processing
@object_definitions[obj_name] = obj_def
end

def find_klass(obj_name)
klass_name = self.class.object_name_to_klass_name(obj_name)
return nil unless @mod_container.const_defined?(klass_name)
@mod_container.const_get(klass_name)
end

# run it _once_ when all objects are loaded
def load_associations
@object_definitions.each_pair do |obj_name, obj_def|
next unless obj_def.include?(:associations)
obj_assoc = obj_def[:associations]

klass = find_klass(obj_name)

obj_assoc.each_pair do |field_name, assoc|
foreign_klass = find_klass(assoc[:object])
assoc[:class_name] = foreign_klass.to_s

case assoc[:type]
when :belongs_to
klass.belongs_to field_name, assoc.reject {|key, val| not ActiveLdap::Associations::ClassMethods::VALID_BELONGS_TO_OPTIONS.include?(key) }
when :has_many
klass.has_many field_name, assoc.reject {|key, val| not ActiveLdap::Associations::ClassMethods::VALID_HAS_MANY_OPTIONS.include?(key) }
else
raise "bug in '#{obj_name}' object associations (wrong type)"
end
end
end
end
end

# default location for mapped objects
module Objects
end
end


I18n.load_path += Dir[File.join(Pathname.new(".").realpath, "locale", "*.yml")]
I18n.default_locale = :en

ldapctl = LdapMapper::Controller.new
c26f67e5 Marc Dequènes (Duck)
config[:aspects].each_pair do |aspect_name, aspect_data|
02029fe9 Marc Dequènes (Duck)
ldapctl.set_aspect(aspect_name, aspect_data)
end
c26f67e5 Marc Dequènes (Duck)
config[:objects].each_pair do |obj_name, obj_data|
02029fe9 Marc Dequènes (Duck)
ldapctl.load_object(obj_name, obj_data)
end
ldapctl.load_associations

cmd = CmdParse::Command.new('list', false)
cmd.short_desc = "list objects"
cmd.set_execution_block do |args|
if args.size != 1
STDERR.puts "syntax error: no object name given"
exit 1
end

obj_name = args[0]
obj_klass = ldapctl.find_klass(obj_name)
if obj_klass.nil?
STDERR.puts "No such object '#{obj_name}'."
exit 2
end

obj_klass.find(:all).each do |obj|
puts "#{obj.name}: #{obj.description}"
end
end
cmdparser.add_command(cmd)

def objectclasses_attr_list(objectclass_list)
objectclass_list = [objectclass_list] unless objectclass_list.is_a? Array
list = []
objectclass_list.each do |objectclass|
objectclass_obj = ActiveLdap::Base.schema.object_class(objectclass)
attr_list = objectclass_obj.must + objectclass_obj.may
list += attr_list.collect{|attr| attr.human_attribute_name }
end
list
end

def display_attributes(item, attr_list = nil)
attr_list = item.attributes.keys.sort if attr_list.nil?
attr_list.each do |key|
next if item.class.presentation[:hidden_attributes].include?(key)

next if not $expert_opt and item.class.presentation[:expert_attributes].include?(key)

att = ActiveLdap::Base.schema.attribute(key)
next if att.binary?

val = item[key]
item_name = I18n.t(att.human_attribute_name, :scope => 'attribute_types', :default => att.human_attribute_description)
puts item_name + ": " + (val.is_a?(Array) ? val.sort.collect{|v| v.to_s }.join(", ") : val.to_s)
end
end

cmd = CmdParse::Command.new('show', false)
cmd.short_desc = "show object information"
cmd.set_execution_block do |args|
if args.size < 1
STDERR.puts "syntax error: no object name given"
exit 1
end
if args.size < 2
STDERR.puts "syntax error: no item name given"
exit 1
end

obj_name = args[0]
obj_klass = ldapctl.find_klass(obj_name)
if obj_klass.nil?
STDERR.puts "No such object '#{obj_name}'."
exit 2
end

item_name = args[1]
begin
item = obj_klass.find(item_name)
rescue ActiveLdap::EntryNotFound
STDERR.puts "No such item '#{obj_name}/#{item_name}'"
exit 2
end

aspects = item.aspects

if $debug_opt
puts item.to_s
puts "=== Detected Info ==="
puts "aspects: " + aspects.keys.sort.join(", ")
else
used_attributes = []

attr_list = (objectclasses_attr_list(item.required_classes + item.class.presentation[:optional_classes]) & item.attributes.keys).sort
display_attributes(item, attr_list)

used_attributes += attr_list
aspects.keys.sort.each do |r|
aspect_display_name = I18n.t(r, :scope => 'aspects', :default => "Aspect: #{r}")
puts "=== #{aspect_display_name} ==="
c26f67e5 Marc Dequènes (Duck)
attr_list = ((objectclasses_attr_list(aspects[r][:mapping][:classes]) & item.attributes.keys) - used_attributes).sort
02029fe9 Marc Dequènes (Duck)
display_attributes(item, attr_list)
used_attributes += attr_list
end
end

puts "=== Associations ==="
item.associations.each do |assoc|
assoc_display_name = I18n.t(assoc, :scope => 'associations', :default => assoc.to_s)
puts "#{assoc_display_name}: " + item.send(assoc).collect{|g| g.name }.join(", ")
end
c26f67e5 Marc Dequènes (Duck)
p item.organized_attributes($expert_opt)
02029fe9 Marc Dequènes (Duck)
end
cmdparser.add_command(cmd)

cmdparser.parse