root/lib/ldap_shadows/shadow.rb @ adf367f7
1f9bfec2 | Marc Dequènes (Duck) | #--
|
|
# LdapShadows, a Medium-level LDAP Access Library and Tool.
|
|||
bc2c2691 | Marc Dequènes (Duck) | # Copyright (c) 2009-2010 Marc Dequènes (Duck) <Duck@DuckCorp.org>
|
|
1f9bfec2 | Marc Dequènes (Duck) | #
|
|
# 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/>.
|
|||
#++
|
|||
$KCODE = 'UTF8'
|
|||
require 'jcode'
|
|||
require 'active_ldap'
|
|||
70b17bfa | Marc Dequènes (Duck) | require 'active_ldap/user_password'
|
|
1f9bfec2 | Marc Dequènes (Duck) | require 'ldap_shadows/activeldap_fixes'
|
|
c331fe49 | Marc Dequènes (Duck) | require 'facets/metaid'
|
|
70b17bfa | Marc Dequènes (Duck) | require 'ldap_shadows/elements'
|
|
1f9bfec2 | Marc Dequènes (Duck) | ||
module LdapShadows
|
|||
c331fe49 | Marc Dequènes (Duck) | module Shadows
|
|
end
|
|||
1f9bfec2 | Marc Dequènes (Duck) | class Shadow
|
|
attr_reader :name
|
|||
b527ce77 | Marc Dequènes (Duck) | def initialize(name, config)
|
|
1f9bfec2 | Marc Dequènes (Duck) | @name = name
|
|
b527ce77 | Marc Dequènes (Duck) | @config = config
|
|
1f9bfec2 | Marc Dequènes (Duck) | ||
c331fe49 | Marc Dequènes (Duck) | # cannot use anonymous modules/classes, as active_ldap needs named classes for relations
|
|
90809ae4 | Marc Dequènes (Duck) | module_name = "Shadow" + name.camelize
|
|
c331fe49 | Marc Dequènes (Duck) | @container = LdapShadows::Shadows.module_eval(<<-EOS)
|
|
module #{module_name}; self; end
|
|||
EOS
|
|||
5a48a521 | Marc Dequènes (Duck) | @container_elements = {}
|
|
1f9bfec2 | Marc Dequènes (Duck) | end
|
|
bbe89518 | Marc Dequènes (Duck) | def set_config(shadow_def_raw)
|
|
9a7f1b63 | Marc Dequènes (Duck) | shadow_default_config = @config.load_default_config('shadow')
|
|
shadow_def = @config.parse(@name, 'shadow', shadow_def_raw)
|
|||
@shadow_config = shadow_default_config.recursive_merge(shadow_def)
|
|||
1f9bfec2 | Marc Dequènes (Duck) | end
|
|
bbe89518 | Marc Dequènes (Duck) | def get_config
|
|
@shadow_config
|
|||
1f9bfec2 | Marc Dequènes (Duck) | end
|
|
def add_aspect(aspect_name, aspect_def_raw)
|
|||
2e882c9f | Marc Dequènes (Duck) | add_container_element('aspect', aspect_name, aspect_def_raw)
|
|
1f9bfec2 | Marc Dequènes (Duck) | end
|
|
def get_aspect(aspect_name)
|
|||
5a48a521 | Marc Dequènes (Duck) | @container_elements['aspect'][aspect_name.to_s]
|
|
1f9bfec2 | Marc Dequènes (Duck) | end
|
|
4b046f91 | Marc Dequènes (Duck) | def add_object(object_name, object_def_raw)
|
|
c4ca8497 | Marc Dequènes (Duck) | add_container_element('object', object_name, object_def_raw)
|
|
1f9bfec2 | Marc Dequènes (Duck) | end
|
|
90809ae4 | Marc Dequènes (Duck) | def get_object(object_name)
|
|
5a48a521 | Marc Dequènes (Duck) | @container_elements['object'][object_name.to_s]
|
|
1f9bfec2 | Marc Dequènes (Duck) | end
|
|
90809ae4 | Marc Dequènes (Duck) | def cast
|
|
f25d0aed | Marc Dequènes (Duck) | if @cast
|
|
raise PreProcessingError, _("SHadow '%s' already initialized") % @name
|
|||
end
|
|||
90809ae4 | Marc Dequènes (Duck) | @container_elements.each_value do |e_models|
|
|
e_models.each_value do |e_klass|
|
|||
e_klass.cast
|
|||
1f9bfec2 | Marc Dequènes (Duck) | end
|
|
90809ae4 | Marc Dequènes (Duck) | end
|
|
@container_elements.each_value do |e_models|
|
|||
e_models.each_value do |e_klass|
|
|||
e_klass.cast_relations
|
|||
1f9bfec2 | Marc Dequènes (Duck) | end
|
|
end
|
|||
f25d0aed | Marc Dequènes (Duck) | ||
@cast = true
|
|||
1f9bfec2 | Marc Dequènes (Duck) | end
|
|
def objects
|
|||
5a48a521 | Marc Dequènes (Duck) | @container_elements['object'].keys.sort
|
|
end
|
|||
def aspects
|
|||
@container_elements['aspect'].keys.sort
|
|||
end
|
|||
fe532158 | Marc Dequènes (Duck) | def tree(search_base, raw = false, all_objects = false)
|
|
item_list = {}
|
|||
if all_objects
|
|||
ActiveLdap::Base.find(:all, :base => search_base, :scope => :sub, :attributes => ['']).each do |item|
|
|||
item_list[ActiveLdap::DistinguishedName.parse(item.dn)]= item
|
|||
end
|
|||
else
|
|||
accc97fc | Marc Dequènes (Duck) | self.get_config[:presentation][:tree_objects].each do |obj_hdl|
|
|
fe532158 | Marc Dequènes (Duck) | obj_klass = get_object(obj_hdl.downcase)
|
|
unless obj_klass
|
|||
raise PreProcessingError, _("Tree object '%s' not defined") % obj_hdl
|
|||
end
|
|||
obj_klass.find(:all, :base => search_base, :scope => :sub).each do |item|
|
|||
item_list[item.dn_obj] = item
|
|||
end
|
|||
end
|
|||
end
|
|||
raw_tree = {}
|
|||
tree = {}
|
|||
2bd92292 | Marc Dequènes (Duck) | base_dn = ActiveLdap::DistinguishedName.parse(ActiveLdap::Base.base)
|
|
fe532158 | Marc Dequènes (Duck) | dn_list = item_list.keys
|
|
dn_list.each do |dn|
|
|||
p_raw_tree = raw_tree
|
|||
p_tree = tree
|
|||
p_dn = nil
|
|||
p_item = nil
|
|||
current_dn_parts = [base_dn.to_s]
|
|||
(dn - base_dn).to_s.split(',').reverse.each do |p_dn|
|
|||
unless raw
|
|||
current_dn_parts.unshift p_dn
|
|||
current_dn = ActiveLdap::DistinguishedName.parse(current_dn_parts.join(","))
|
|||
current_item = item_list[current_dn]
|
|||
776a7c72 | Marc Dequènes (Duck) | if current_item.nil?
|
|
current_item = ActiveLdap::Base.find(:first, :base => current_dn.to_s, :scope => :base, :attributes => [''])
|
|||
item_list[current_dn] = current_item
|
|||
fe532158 | Marc Dequènes (Duck) | end
|
|
2bd92292 | Marc Dequènes (Duck) | p_item = LdapShadows::Manipulation.raw_item_info(self, item_list[current_dn])[:name]
|
|
fe532158 | Marc Dequènes (Duck) | end
|
|
unless p_raw_tree.has_key?(p_dn)
|
|||
p_raw_tree[p_dn] = {}
|
|||
p_tree[p_item] = {} unless raw
|
|||
end
|
|||
p_raw_tree = p_raw_tree[p_dn]
|
|||
p_tree = p_tree[p_item] unless raw
|
|||
end
|
|||
p_raw_tree = nil
|
|||
p_tree = nil
|
|||
end
|
|||
if raw
|
|||
res = {base_dn.to_s => raw_tree}
|
|||
else
|
|||
776a7c72 | Marc Dequènes (Duck) | base_obj = ActiveLdap::Base.find(:first, :base => base_dn.to_s, :scope => :base, :attributes => [''])
|
|
2bd92292 | Marc Dequènes (Duck) | res = {LdapShadows::Manipulation.raw_item_info(self, base_obj)[:name] => tree}
|
|
fe532158 | Marc Dequènes (Duck) | end
|
|
res
|
|||
end
|
|||
5a48a521 | Marc Dequènes (Duck) | protected
|
|
bbe89518 | Marc Dequènes (Duck) | def add_container_element(e_type, e_name, e_def_raw)
|
|
5a48a521 | Marc Dequènes (Duck) | superklass_location = LdapShadows::Elements
|
|
90809ae4 | Marc Dequènes (Duck) | superklass_name = 'Ldap' + e_type.camelize
|
|
5a48a521 | Marc Dequènes (Duck) | unless superklass_location.const_defined?(superklass_name)
|
|
raise PreProcessingError, _("Element model '%s' does not exist") % e_type
|
|||
end
|
|||
90809ae4 | Marc Dequènes (Duck) | klass_name = superklass_name + e_name.to_s.camelize
|
|
5a48a521 | Marc Dequènes (Duck) | if @container.const_defined?(klass_name)
|
|
raise PreProcessingError, _("Element '%s' for model '%s' as already been defined") % [e_name, e_type]
|
|||
end
|
|||
9a7f1b63 | Marc Dequènes (Duck) | e_default_parameters = @config.load_default_config(e_type)
|
|
428fdd46 | Marc Dequènes (Duck) | e_def = @config.parse(e_name, e_type, e_def_raw, true, {:default_parameters => e_default_parameters})
|
|
9a7f1b63 | Marc Dequènes (Duck) | e_config = e_default_parameters.recursive_merge(e_def)
|
|
90809ae4 | Marc Dequènes (Duck) | ||
bbe89518 | Marc Dequènes (Duck) | 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)
|
|||
70b17bfa | Marc Dequènes (Duck) | klass.instance_variable_set(:@config, @config)
|
|
428fdd46 | Marc Dequènes (Duck) | klass.instance_variable_set(:@default_parameters, e_default_parameters)
|
|
9a7f1b63 | Marc Dequènes (Duck) | klass.instance_variable_set(:@parameters, e_config)
|
|
bbe89518 | Marc Dequènes (Duck) | klass.meta_eval do
|
|
428fdd46 | Marc Dequènes (Duck) | attr_reader :handle, :shadow, :config, :default_parameters, :parameters
|
|
bbe89518 | Marc Dequènes (Duck) | end
|
|
begin
|
|||
5a48a521 | Marc Dequènes (Duck) | klass_content = @config.load_hook_content(@name, e_type, e_name)
|
|
klass.class_eval(klass_content) unless klass_content.nil?
|
|||
rescue
|
|||
raise PreProcessingError, _("Could not load '%s' plugin for '%s': %s") % [e_type, e_name, $!]
|
|||
end
|
|||
@container_elements[e_type] ||= {}
|
|||
@container_elements[e_type][e_name] = klass
|
|||
klass
|
|||
bbe89518 | Marc Dequènes (Duck) | rescue
|
|
428fdd46 | Marc Dequènes (Duck) | raise PreProcessingError, _("Could not create element '%s' for model '%s': %s") % [e_name, e_type, $!]
|
|
1f9bfec2 | Marc Dequènes (Duck) | end
|
|
end
|
|||
end
|