Project

General

Profile

« Previous | Next » 

Revision bbe89518

Added by Marc Dequènes about 15 years ago

  • ID bbe8951879f7c0b7b74df85f94c7de5b712cf78c

[cleanup] internal rework #1

View differences:

bin/shadowwalker
config = LdapShadows::Config.instance
config.load_global_config([File.join(ENV['HOME'], ".shadowwalker")])
begin
$ldapctl = config.load_shadow($program_options[:shadow_name])
$shadow = config.load_shadow($program_options[:shadow_name])
rescue ActiveLdap::Error => e
STDERR.puts _("LDAP connection error: %s") % e.to_s
exit 2
......
raise SyntaxError, _("no object name given") if args.empty?
obj_hdl = args.shift.downcase.singularize
obj_klass = $ldapctl.find_klass(obj_hdl)
obj_klass = $shadow.find_klass(obj_hdl)
raise PreProcessingError, _("No such object '%s'") % obj_hdl if obj_klass.nil?
obj_klass
end
......
obj_hdl = args.shift.downcase.singularize
case obj_hdl
when ':object'
list = $ldapctl.objects
list = $shadow.objects
puts "=== List of LDAP objects (#{list.size}) ==="
list.each do |obj_name|
puts " - #{obj_name}"
......
end
else
dn_list = []
sconfig = $ldapctl.get_shadow_config()
sconfig = $shadow.get_config()
(sconfig[:tree_objects] || []).each do |obj_hdl|
obj_klass = $ldapctl.find_klass(obj_hdl.downcase)
(sconfig[:presentation][:tree_objects] || []).each do |obj_hdl|
obj_klass = $shadow.find_klass(obj_hdl.downcase)
unless obj_klass
raise PreProcessingError, _("Location object '%s' not defined") % obj_hdl
end
......
end
end
base_dn = ActiveLdap::DistinguishedName.parse(LdapObject.base)
base_dn = ActiveLdap::DistinguishedName.parse(Elements::LdapObject.base)
tree = {}
dn_list.each do |dn|
......
raise SyntaxError, _("no search arguments") if args.empty?
res = LdapObject.items_find_from_strings(args)
res = Elements::LdapObject.items_find_from_strings($shadow, args)
unless res.empty?
display_lines = []
res.each do |raw_item|
display_lines << LdapObject.raw_item_info(raw_item)[:name]
display_lines << Elements::LdapObject.raw_item_info($shadow, raw_item)[:name]
end
puts display_lines.join("\n")
end
lib/ldap_shadows/config.rb
:config_path => shadow_config_path,
:shadow => shadow
}
shadow.set_shadow_config(config)
shadow.set_config(config)
load_config_components(shadow_config_path, 'aspects') do |c_name, c_config|
shadow.add_aspect(c_name, c_config)
lib/ldap_shadows/elements/object.rb
end
def organized_data
ignored_attrs = self.class.shadow.get_shadow_config[:hidden_attributes] || []
ignored_attrs = self.class.shadow.get_config[:presentation][:hidden_attributes] || []
ignored_attrs += self.class.presentation[:hidden_attributes] || []
attr_list = self.nonempty_attributes - ignored_attrs
......
true
end
def self.items_find_from_strings(str_list)
def self.items_find_from_strings(shadow, str_list)
ldap_search_objects = "(objectClass=*)"
ldap_search_aspects = ""
ldap_search_fields = []
......
when ''
case field
when 'objects'
ldap_search_objects = ldap_search_string_objects(field, op, val)
ldap_search_objects = ldap_search_string_objects(shadow, field, op, val)
when 'aspects'
ldap_search_aspects = ldap_search_string_aspects(field, op, val)
ldap_search_aspects = ldap_search_string_aspects(shadow, field, op, val)
else
raise PreProcessingError, _("Unknown core field '%s'") % field
end
......
end
end
def self.ldap_search_string_objects(field, op, val_list)
def self.ldap_search_string_objects(shadow, field, op, val_list)
ldap_search_parts = val_list.split(",").collect do |val|
obj_hdl = val.downcase.singularize
obj_klass = $ldapctl.find_klass(obj_hdl)
obj_klass = shadow.find_klass(obj_hdl)
raise PreProcessingError, _("No such object '%s'") % val if obj_klass.nil?
ldap_classes = obj_klass.required_classes
......
"(|" + ldap_search_parts.join + ")"
end
def self.ldap_search_string_aspects(field, op, val_list)
def self.ldap_search_string_aspects(shadow, field, op, val_list)
ldap_search_parts = val_list.split(",").collect do |val|
aspect_data = self.class.shadow.get_aspect(val)
aspect_data = shadow.get_aspect(val)
raise PreProcessingError, _("No such aspect '%s'") % val if aspect_data.nil?
ldap_classes = aspect_data[:mapping][:classes]
......
"(&" + ldap_search_parts.join + ")"
end
def self.find_raw_item_object(raw_item)
self.class.shadow.objects.each do |obj_hdl|
obj_klass = $ldapctl.find_klass(obj_hdl)
def self.find_raw_item_object(shadow, raw_item)
shadow.objects.each do |obj_hdl|
obj_klass = shadow.find_klass(obj_hdl)
ldap_classes = obj_klass.required_classes
return obj_hdl if raw_item.classes & ldap_classes == ldap_classes
end
nil
end
def self.raw_item_info(raw_item)
obj_hdl = self.find_raw_item_object(raw_item)
def self.raw_item_info(shadow, raw_item)
obj_hdl = self.find_raw_item_object(shadow, raw_item)
if obj_hdl
obj_klass = $ldapctl.find_klass(obj_hdl)
obj_klass = shadow.find_klass(obj_hdl)
item = obj_klass.new(raw_item.dn)
{:name => "#{obj_hdl}/#{item.name}", :item => item, :object => obj_klass}
else
lib/ldap_shadows/shadow.rb
@aspects = {}
end
def set_shadow_config(shadow_def_raw)
def set_config(shadow_def_raw)
@shadow_config = @config.parse_and_validate(@name, 'shadow', shadow_def_raw)
end
def get_shadow_config
# TODO: remove this workaround
@shadow_config[:presentation]
def get_config
@shadow_config
end
def add_aspect(aspect_name, aspect_def_raw)
e_type = 'aspect'
klass = add_container_element(e_type, aspect_name)
aspect_def = @config.parse_and_validate(aspect_name, e_type, aspect_def_raw)
klass = add_container_element(e_type, aspect_name, aspect_def_raw)
# TODO: remove this crap
aspect_def = @config.parse_and_validate(aspect_name, e_type, aspect_def_raw)
@aspects[aspect_name.to_sym] = aspect_def
end
......
def add_object(object_name, object_def_raw)
e_type = 'object'
klass = add_container_element(e_type, object_name)
object_def = @config.parse_and_validate(object_name, 'object', object_def_raw)
klass.presentation = object_def[:presentation]
klass.ldap_mapping object_def[:mapping]
klass = add_container_element(e_type, object_name, object_def_raw)
# TODO: remove this crap
object_def = @config.parse_and_validate(object_name, e_type, object_def_raw)
klass.presentation = object_def[:presentation]
klass.ldap_mapping object_def[:mapping]
@object_definitions[object_name.to_sym] = object_def
end
......
protected
def add_container_element(e_type, e_name)
def add_container_element(e_type, e_name, e_def_raw)
superklass_location = LdapShadows::Elements
superklass_name = 'Ldap' + e_type.classify
unless superklass_location.const_defined?(superklass_name)
......
raise PreProcessingError, _("Element '%s' for model '%s' as already been defined") % [e_name, e_type]
end
begin
klass = @container.module_eval(<<-EOS)
class #{klass_name} < #{superklass_location}::#{superklass_name}; self; end
EOS
klass.instance_variable_set(:@handle, e_name)
klass.instance_variable_set(:@shadow, self)
klass.meta_eval do
attr_reader :handle, :shadow
end
klass = @container.module_eval(<<-EOS)
class #{klass_name} < #{superklass_location}::#{superklass_name}; self; end
EOS
klass.instance_variable_set(:@handle, e_name)
klass.instance_variable_set(:@shadow, self)
klass.meta_eval do
attr_reader :handle, :shadow
end
e_def = @config.parse_and_validate(e_name, e_type, e_def_raw)
klass.instance_variable_set(:@parameters, e_def)
begin
klass_content = @config.load_hook_content(@name, e_type, e_name)
klass.class_eval(klass_content) unless klass_content.nil?
rescue
......
@container_elements[e_type][e_name] = klass
klass
rescue
raise PreProcessingError, _("Could not create element '%s' for model '%s': %s") % [e_type, e_name, $!]
end
end
end

Also available in: Unified diff