Revision 90809ae4
Added by Marc Dequènes over 14 years ago
- ID 90809ae4611daed224702e4f6083c42f9277a4a4
lib/ldap_shadows/elements/object.rb | ||
---|---|---|
#++
|
||
|
||
|
||
require 'ldap_shadows/hooks'
|
||
|
||
|
||
module LdapShadows
|
||
module Elements
|
||
class LdapObject < ActiveLdap::Base
|
||
include Hooks
|
||
include LdapElement
|
||
|
||
class_inheritable_accessor :presentation, :relations_info
|
||
class_inheritable_accessor :presentation
|
||
@relations_info = {}
|
||
|
||
class << self
|
||
attr_accessor :relations_info
|
||
protected :relations_info
|
||
end
|
||
|
||
# default
|
||
ldap_mapping :prefix => '', :classes => ['top'], :scope => :sub
|
||
|
||
def self.cast
|
||
ldap_mapping self.parameters[:mapping]
|
||
end
|
||
|
||
def self.cast_relations
|
||
object_rel = {}
|
||
object_rel.merge!(self.parameters[:relations]) if self.parameters.include?(:relations)
|
||
if self.parameters[:presentation].has_key?(:allowed_aspects)
|
||
self.parameters[:presentation][:allowed_aspects].each do |aspect|
|
||
aspect_data = self.shadow.get_aspect(aspect)
|
||
if aspect_data.nil?
|
||
raise PreProcessingError, _("Aspect '%s' is missing for object '%s'") % [aspect, self.handle]
|
||
end
|
||
object_rel.merge!(aspect_data[:relations]) if aspect_data.has_key?(:relations) and aspect_data[:relations]
|
||
end
|
||
end
|
||
return if object_rel.empty?
|
||
|
||
object_relations_info = {}
|
||
object_rel.each_pair do |field_name, rel|
|
||
foreign_klass = self.shadow.get_object(rel[:object])
|
||
if foreign_klass.nil?
|
||
raise PreProcessingError, _("Relation '%s' for object '%s' is impossible: foreign object '%s' is missing") % [field_name, self.handle, rel[:object]]
|
||
end
|
||
rel[:class_name] = foreign_klass.to_s
|
||
|
||
case rel[:type]
|
||
when 'belongs_to'
|
||
belongs_to field_name, rel.reject {|key, val| not ActiveLdap::Associations::ClassMethods::VALID_BELONGS_TO_OPTIONS.include?(key) }
|
||
when 'has_many'
|
||
has_many field_name, rel.reject {|key, val| not ActiveLdap::Associations::ClassMethods::VALID_HAS_MANY_OPTIONS.include?(key) }
|
||
else
|
||
raise "bug in '#{self.handle}' object relations (wrong type)"
|
||
end
|
||
|
||
object_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
|
||
self.relations_info = object_relations_info
|
||
end
|
||
|
||
# temporary method until active_ldap is fixed: return a DN object (see activeldap#23932)
|
||
def dn_obj
|
||
ActiveLdap::DistinguishedName.parse(self.dn)
|
||
... | ... | |
def self.ldap_search_string_objects(shadow, field, op, val_list)
|
||
ldap_search_parts = val_list.split(",").collect do |val|
|
||
obj_hdl = val.downcase.singularize
|
||
obj_klass = shadow.find_klass(obj_hdl)
|
||
obj_klass = shadow.get_object(obj_hdl)
|
||
raise PreProcessingError, _("No such object '%s'") % val if obj_klass.nil?
|
||
|
||
ldap_classes = obj_klass.required_classes
|
||
... | ... | |
|
||
def self.find_raw_item_object(shadow, raw_item)
|
||
shadow.objects.each do |obj_hdl|
|
||
obj_klass = shadow.find_klass(obj_hdl)
|
||
obj_klass = shadow.get_object(obj_hdl)
|
||
ldap_classes = obj_klass.required_classes
|
||
return obj_hdl if raw_item.classes & ldap_classes == ldap_classes
|
||
end
|
||
... | ... | |
def self.raw_item_info(shadow, raw_item)
|
||
obj_hdl = self.find_raw_item_object(shadow, raw_item)
|
||
if obj_hdl
|
||
obj_klass = shadow.find_klass(obj_hdl)
|
||
obj_klass = shadow.get_object(obj_hdl)
|
||
item = obj_klass.new(raw_item.dn)
|
||
{:name => "#{obj_hdl}/#{item.name}", :item => item, :object => obj_klass}
|
||
else
|
Also available in: Unified diff
[cleanup] internal rework #2