Project

General

Profile

Download (5.45 KB) Statistics
| Branch: | Tag: | Revision:
#!/usr/bin/ruby -Ku

#--
# LdapShadows, a Medium-level LDAP Access Library and Tool.
# Copyright (c) 2009 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/>.
#++

# to allow in-place run for test
$: << File.join(File.dirname(__FILE__), "..", "lib")

require 'ldap_shadows'
require 'ldap_shadows/display_utils'
require 'yaml'
require 'cmdparse2'

include LdapShadows

config_str = IO.read(File.join(LdapShadows::Config::CFG_DIR, "test.conf"))
config = YAML.load(config_str)
config_str_prv_filelist = [
File.join(ENV['HOME'], ".shadowwalker"),
File.join(LdapShadows::Config::CFG_DIR, "private.conf")
]
config_str_prv_filelist.each do |file|
if File.exists?(file)
config_str_prv = IO.read(file)
config.merge!(YAML.load(config_str_prv))
break
end
end
config.recursive_symbolize_keys!
ActiveLdap::Base.setup_connection(config[:ldap])

$program_options = {}

cmdparser = CmdParse::CommandParser.new(true)
cmdparser.banner = PRODUCT
cmdparser.program_name = File.basename(__FILE__)
cmdparser.program_version = VERSION.split(".")

cmdparser.options = CmdParse::OptionParserWrapper.new do |opt|
opt.separator "Global options:"
opt.on("--debug", "Output debug info without being formated") {|t| $program_options[:debug] = true }
opt.on("--expert", "Output extra info for expert users") {|t| $program_options[:expert] = true }
opt.on("--handles", "Output with handles (objects/field/... keys used for manipulations)") {|t| $program_options[:handles] = true }
end

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

I18n.load_path += Dir[File.join(LdapShadows::Config::DATA_DIR, "*.yml")]
I18n.default_locale = :en

ldapctl = Controller.new
ldapctl.set_global_config(config[:presentation])
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|
ldapctl.load_object(obj_name, obj_data)
end
ldapctl.load_relations

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

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_hdl = args[0].singularize
obj_klass = ldapctl.find_klass(obj_hdl)
if obj_klass.nil?
STDERR.puts "No such object '#{obj_hdl}'."
exit 2
end

obj_human_name = translate_object_name(obj_hdl)
puts "=== List of #{obj_human_name.pluralize} ==="
obj_klass.find(:all).each do |obj|
str = obj.human_name
str += " [#{obj.name}]" if $program_options[:handles]
str += ": #{obj.description}" unless obj.description.empty?
puts str
end
end
cmdparser.add_command(cmd)

def 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 translate_aspect_name(aspect_name)
I18n.t(aspect_name, :scope => 'aspects', :default => "Aspect '#{aspect_name}'")
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_hdl = args[0]
obj_klass = ldapctl.find_klass(obj_hdl)
if obj_klass.nil?
STDERR.puts "No such object '#{obj_hdl}'."
exit 2
end

item_hdl = args[1]
begin
item = obj_klass.find(item_hdl, :attributes => ["*", "+"])
rescue ActiveLdap::EntryNotFound
STDERR.puts "No such item '#{obj_hdl}/#{item_hdl}'"
exit 2
end

Display.display_item(item, $program_options)
end
cmdparser.add_command(cmd)

cmd = CmdParse::Command.new('tree', false)
cmd.short_desc = "show skeleton objects tree"
cmd.set_execution_block do |args|
puts "Tree:"

base_dn = ActiveLdap::DistinguishedName.parse(LdapObject.base)

gconfig = ldapctl.get_global_config()
if gconfig.has_key?(:tree_objects)
tree = {}

gconfig[:tree_objects].each do |obj_hdl|
obj_klass = ldapctl.find_klass(obj_hdl)
raise "object '#{obj_hdl}' not defined" unless obj_klass

obj_klass.find(:all).each do |obj|
ptree = tree
pdn = nil
(obj.dn_obj - base_dn).to_s.split(',').reverse.each do |pdn|
ptree[pdn] = {} unless ptree.has_key?(pdn)
ptree = ptree[pdn]
end
ptree = nil
end
end
tree = {base_dn => tree}

Display.display_hash_tree(tree, 0)
end
end
cmdparser.add_command(cmd)

cmdparser.parse

    (1-1/1)