Revision c26f67e5
Added by Marc Dequènes over 15 years ago
- ID c26f67e5ac2fc7a7b166aea3aa819e7d7c7929f2
config/test.conf | ||
---|---|---|
mapping:
|
||
classes: ['fsUser']
|
||
presentation:
|
||
skipped_attributes: ['loginShell']
|
||
associations:
|
||
shell:
|
||
mapping:
|
||
classes: ['shellUser']
|
||
presentation:
|
||
associated_attributes: ['loginShell']
|
||
associations:
|
||
ftp:
|
||
mapping:
|
||
classes: ['ftpUser']
|
||
presentation:
|
||
skipped_attributes: ['loginShell']
|
||
associations:
|
||
web:
|
||
mapping:
|
test.rb | ||
---|---|---|
require 'cmdparse2'
|
||
require 'pathname'
|
||
|
||
class Hash
|
||
def recursive_symbolize_keys!
|
||
symbolize_keys!
|
||
values.select { |v| v.is_a?(Hash) }.each { |h| h.recursive_symbolize_keys! }
|
||
self
|
||
end
|
||
end
|
||
|
||
config_str_prv = IO.read("config/private.conf")
|
||
config_str = IO.read("config/test.conf")
|
||
config = YAML.load(config_str_prv).merge(YAML.load(config_str))
|
||
ActiveLdap::Base.setup_connection(config['ldap'])
|
||
config = YAML.load(config_str_prv).merge(YAML.load(config_str)).recursive_symbolize_keys!
|
||
ActiveLdap::Base.setup_connection(config[:ldap])
|
||
|
||
cmdparser = CmdParse::CommandParser.new(true)
|
||
cmdparser.program_name = ""
|
||
... | ... | |
present_aspects = {}
|
||
self.class.presentation[:allowed_aspects].each do |aspect|
|
||
aspect_data = self.class.mapper.get_aspect(aspect)
|
||
aspect_mapping = aspect_data['mapping']
|
||
present_aspects[aspect] = aspect_data if self.classes & aspect_mapping['classes'] == aspect_mapping['classes']
|
||
aspect_mapping = aspect_data[:mapping]
|
||
present_aspects[aspect] = aspect_data if self.classes & aspect_mapping[:classes] == aspect_mapping[:classes]
|
||
end
|
||
|
||
present_aspects
|
||
end
|
||
|
||
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
|
||
end
|
||
|
||
class Controller
|
||
... | ... | |
end
|
||
|
||
def get_aspect(aspect_name)
|
||
@aspects[aspect_name]
|
||
@aspects[aspect_name.to_sym]
|
||
end
|
||
|
||
def self.object_name_to_klass_name(obj_name)
|
||
"LdapObject" + obj_name.capitalize
|
||
"LdapObject" + obj_name.to_s.capitalize
|
||
end
|
||
|
||
def load_object(obj_name, obj_def)
|
||
obj_def.symbolize_keys!
|
||
obj_mapping = obj_def[:mapping].symbolize_keys
|
||
obj_mapping = obj_def[:mapping]
|
||
klass_name = self.class.object_name_to_klass_name(obj_name)
|
||
|
||
# create class
|
||
... | ... | |
|
||
# configure class
|
||
klass = find_klass(obj_name)
|
||
klass.presentation = obj_def[:presentation].symbolize_keys
|
||
klass.presentation = obj_def[:presentation]
|
||
klass.mapper = self
|
||
klass.ldap_mapping obj_mapping.reject {|key, val| not ActiveLdap::Base::VALID_LDAP_MAPPING_OPTIONS.include?(key) }
|
||
|
||
... | ... | |
klass = find_klass(obj_name)
|
||
|
||
obj_assoc.each_pair do |field_name, assoc|
|
||
assoc.symbolize_keys!
|
||
|
||
foreign_klass = find_klass(assoc[:object])
|
||
assoc[:class_name] = foreign_klass.to_s
|
||
|
||
... | ... | |
I18n.default_locale = :en
|
||
|
||
ldapctl = LdapMapper::Controller.new
|
||
config['aspects'].each_pair do |aspect_name, aspect_data|
|
||
config[:aspects].each_pair do |aspect_name, aspect_data|
|
||
ldapctl.set_aspect(aspect_name, aspect_data)
|
||
end
|
||
config['objects'].each_pair do |obj_name, obj_data|
|
||
config[:objects].each_pair do |obj_name, obj_data|
|
||
ldapctl.load_object(obj_name, obj_data)
|
||
end
|
||
ldapctl.load_associations
|
||
... | ... | |
aspects.keys.sort.each do |r|
|
||
aspect_display_name = I18n.t(r, :scope => 'aspects', :default => "Aspect: #{r}")
|
||
puts "=== #{aspect_display_name} ==="
|
||
skipped_attributes = defined?(aspects[r]['presentation']['skipped_attributes']) ? aspects[r]['presentation']['skipped_attributes'] : []
|
||
attr_list = ((objectclasses_attr_list(aspects[r]['mapping']['classes']) & item.attributes.keys) - used_attributes - skipped_attributes).sort
|
||
attr_list = ((objectclasses_attr_list(aspects[r][:mapping][:classes]) & item.attributes.keys) - used_attributes).sort
|
||
display_attributes(item, attr_list)
|
||
used_attributes += attr_list
|
||
end
|
||
... | ... | |
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
|
||
|
||
p item.organized_attributes($expert_opt)
|
||
end
|
||
cmdparser.add_command(cmd)
|
||
|
||
cmdparser.parse
|
||
|
||
# TODO: each aspect should be able to "reserve" attributetypes (instead of the ugly 'skipped_attributes' mechanism)
|
||
# if no aspect reserves it, then if the object can take it if it is in its attr_list
|
Also available in: Unified diff
[evol] symbolize all config keys recursively, WIP on attributes classification