|
#--
|
|
# 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/>.
|
|
#++
|
|
|
|
|
|
$KCODE = 'UTF8'
|
|
require 'jcode'
|
|
require 'active_ldap'
|
|
require 'ldap_shadows/activeldap_fixes'
|
|
require 'ldap_shadows/object'
|
|
|
|
|
|
module LdapShadows
|
|
class Controller
|
|
def initialize(mod_container = LdapShadows::Objects)
|
|
@mod_container = mod_container
|
|
@object_definitions = {}
|
|
@aspects = {}
|
|
# should replace @aspects properly one day
|
|
@aspects2 = {}
|
|
|
|
LdapShadows::LdapObject.mapper = self
|
|
end
|
|
|
|
def set_global_config(global_config)
|
|
@global_config = global_config
|
|
end
|
|
|
|
def get_global_config
|
|
@global_config
|
|
end
|
|
|
|
def set_aspect(aspect_name, aspect_def)
|
|
@aspects[aspect_name] = aspect_def
|
|
|
|
filename = File.join(Config::CFG_DIR, "hooks", "aspects", aspect_name.to_s.downcase + ".rb")
|
|
if File.exists?(filename)
|
|
klass_name = "LdapAspect" + aspect_name.to_s.capitalize
|
|
klass_content = IO.read(filename)
|
|
begin
|
|
Aspects.module_eval(<<-EOS)
|
|
class #{klass_name} < Aspect
|
|
#{klass_content}
|
|
end
|
|
EOS
|
|
rescue
|
|
STDERR.puts "Could not load Aspect plugin '#{aspect_name}'"
|
|
exit 1
|
|
end
|
|
|
|
@aspects2[aspect_name] = Aspects.const_get(klass_name)
|
|
end
|
|
end
|
|
|
|
def get_aspect(aspect_name)
|
|
@aspects[aspect_name.to_sym]
|
|
end
|
|
|
|
def get_aspect_klass(aspect_name)
|
|
@aspects2[aspect_name.to_sym]
|
|
end
|
|
|
|
def self.object_name_to_klass_name(obj_name)
|
|
"LdapObject" + obj_name.to_s.capitalize
|
|
end
|
|
|
|
def load_object(obj_name, obj_def)
|
|
obj_mapping = obj_def[:mapping]
|
|
klass_name = self.class.object_name_to_klass_name(obj_name)
|
|
|
|
# create class
|
|
@mod_container.module_eval(<<-EOS)
|
|
class #{klass_name} < LdapShadows::LdapObject; end
|
|
EOS
|
|
|
|
# configure class
|
|
klass = find_klass(obj_name)
|
|
klass.handle = obj_name
|
|
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) }
|
|
|
|
# store definition for later relations processing
|
|
@object_definitions[obj_name] = obj_def
|
|
end
|
|
|
|
def find_klass(obj_name)
|
|
klass_name = self.class.object_name_to_klass_name(obj_name)
|
|
return nil unless @mod_container.const_defined?(klass_name)
|
|
@mod_container.const_get(klass_name)
|
|
end
|
|
|
|
# run it _once_ when all objects are loaded
|
|
def load_relations
|
|
@object_definitions.each_pair do |obj_name, obj_def|
|
|
obj_rel = {}
|
|
obj_rel.merge!(obj_def[:relations]) if obj_def.include?(:relations)
|
|
if obj_def[:presentation].has_key?(:allowed_aspects)
|
|
obj_def[:presentation][:allowed_aspects].each do |rel|
|
|
rel_data = get_aspect(rel)
|
|
obj_rel.merge!(rel_data[:relations]) if rel_data.has_key?(:relations) and rel_data[:relations]
|
|
end
|
|
end
|
|
next if obj_rel.empty?
|
|
|
|
klass = find_klass(obj_name)
|
|
|
|
obj_relations_info = {}
|
|
obj_rel.each_pair do |field_name, rel|
|
|
foreign_klass = find_klass(rel[:object])
|
|
rel[:class_name] = foreign_klass.to_s
|
|
|
|
case rel[:type]
|
|
when :belongs_to
|
|
klass.belongs_to field_name, rel.reject {|key, val| not ActiveLdap::Associations::ClassMethods::VALID_BELONGS_TO_OPTIONS.include?(key) }
|
|
when :has_many
|
|
klass.has_many field_name, rel.reject {|key, val| not ActiveLdap::Associations::ClassMethods::VALID_HAS_MANY_OPTIONS.include?(key) }
|
|
else
|
|
raise "bug in '#{obj_name}' object relations (wrong type)"
|
|
end
|
|
|
|
obj_relations_info[field_name] = {
|
|
:foreign_klass => foreign_klass,
|
|
:single_value => ActiveLdap::Base.schema.attribute(rel[:foreign_key]).single_value?,
|
|
:read_only => rel[:read_only] || false
|
|
}
|
|
end
|
|
klass.relations_info = obj_relations_info
|
|
end
|
|
end
|
|
|
|
def objects
|
|
@object_definitions.keys.collect{|key| key.to_s }.sort
|
|
end
|
|
end
|
|
|
|
# default location for mapped objects
|
|
module Objects
|
|
end
|
|
|
|
class Aspect
|
|
def hook_create
|
|
end
|
|
|
|
def hook_mod
|
|
end
|
|
end
|
|
|
|
module Aspects
|
|
end
|
|
end
|
|
|