Project

General

Profile

Download (5.25 KB) Statistics
| Branch: | Tag: | Revision:
#--
# LdapShadows, a Medium-level LDAP Access Library and Tool.
# Copyright (c) 2009-2010 Marc Dequènes (Duck) <Duck@DuckCorp.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#++

require 'active_support'
require 'ldap_shadows/manipulation_helper'


module LdapShadows
module Translator
I18n.default_locale = :en

def self.translate_object_name(obj_hdl)
I18n.t(obj_hdl, :scope => 'objects', :default => "Object '#{obj_hdl}'")
end

def self.translate_field_name(field_name)
if field_name.index(":")
type, key = field_name.split(":")
case type
when 'rel'
I18n.t(key, :scope => 'relations', :default => field_name)
else
raise "Cannot translate unknown data key type"
end
else
att = ActiveLdap::Base.schema.attribute(field_name)
I18n.t(att.human_attribute_name, :scope => 'attribute_types', :default => att.human_attribute_description)
end
end

def self.translate_aspect_name(aspect_name)
I18n.t(aspect_name, :scope => 'aspects', :default => "Aspect '#{aspect_name}'")
end
end

module Display
def self.display_fields(shadow, attr_data, options = {})
str_lines = []
attr_data.each_pair do |key, val|
next if val[:expert] and not options[:expert]
next if val[:admin] and not options[:admin]

field_name = Translator.translate_field_name(key)

str = field_name
str += " [#{key}]" if options[:handles]
if val[:value].nil?
str += " -> not set"
elsif val[:binary] and options[:skip_binary]
str += " -> #{val[:value].size} file(s) available"
else
str += ": " + (val[:multiple] ?
val[:value].sort.collect{|v| LdapShadows::Manipulation.interpret_field_value(shadow, val[:syntax], v).to_s }.join(", ") :
LdapShadows::Manipulation.interpret_field_value(shadow, val[:syntax], val[:value]).to_s)
end

str_lines << str
end

puts str_lines.sort.join("\n")
end

def self.display_item(item, options = {})
obj_human_name = Translator.translate_object_name(item.class.handle)
name = item.human_name
name += " [#{item.full_handle}]" if options[:handles]
puts "=== #{obj_human_name}: #{name} ==="

if options[:debug]
puts item.to_s
puts "--- Detected Info ---"
puts "aspects: " + item.aspects.sort.join(", ")

puts "--- Relations ---"
item.relations.each do |rel|
rel_data = item.send(rel)
if rel_data.is_a? Enumerable
next if rel_data.empty?
rel_value = rel_data.collect{|g| g.name }.join(", ")
else
# the exists? method also ensure the object is loaded
next unless rel_data.exists?
rel_value = rel_data.name
end
puts "#{rel}: " + rel_value
end

if options[:show_family_members]
puts "--- Family ---"
puts "parent: " + item.family_parent_dn.to_s
puts "siblings: " + item.family_siblings_dn.join(", ")
puts "children: " + item.family_children_dn.join(", ")
end
else
obj_info, obj_aspects = item.organized_data

display_fields(item.class.shadow, obj_info, options)

obj_aspects.each_pair do |aspect_name, aspect_data|
name = Translator.translate_aspect_name(aspect_name)
name += " [#{aspect_name}]" if options[:handles]
puts "--- #{name} ---"
display_fields(item.class.shadow, aspect_data, options)
end

if options[:show_family_members]
puts "--- Family ---"
puts "parent: " + LdapShadows::Manipulation.raw_item_info(item.class.shadow, item.family_parent)[:name]
puts "siblings: " + item.family_siblings.collect{|raw_item| LdapShadows::Manipulation.raw_item_info(item.class.shadow, raw_item)[:name] }.join(", ")
puts "children: " + item.family_children.collect{|raw_item| LdapShadows::Manipulation.raw_item_info(item.class.shadow, raw_item)[:name] }.join(", ")
end
end
end

def self.display_item_list(title, item_list, options)
puts "=== #{title} (#{item_list.size}) ==="
item_list.each do |item|
str = item.human_name
str += " [#{item.full_handle}]" if options[:handles]
str += ": #{item.human_description}" unless item.human_description.empty?
puts str
end
end

def self.display_hash_tree(tree, level)
tree.keys.sort.each do |key|
str = ""
str += " " + "| " * (level -1) + "+-- " if level > 0
str += "<#{key}>"
puts str

display_hash_tree(tree[key], level + 1) if tree[key]
end
end
end
end

(3-3/9)