Project

General

Profile

Download (7.56 KB) Statistics
| Branch: | Tag: | Revision:
#--
# 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/>.
#++


module LdapShadows
class LdapObject < ActiveLdap::Base
class_inheritable_accessor :presentation, :mapper

ldap_mapping :prefix => '', :classes => ['top'], :scope => :sub

OPERATIONAL_ATTRIBUTES = ['creatorsName', 'createTimestamp', 'modifiersName', 'modifyTimestamp', 'structuralObjectClass', 'entryUUID']

# temporary method until active_ldap is fixed: return a DN object (see #23932)
def dn_obj
ActiveLdap::DistinguishedName.parse(self.dn)
end

def name
name = self[dn_attribute].is_a?(Array) ? self[dn_attribute][0] : self[dn_attribute]
name.strip
end

def human_name
[self.class.presentation[:name_attribute], 'displayName', 'cn'].each do |attr|
if attr == 'dn' or (self.has_attribute?(attr) and self.attribute_present?(attr))
val = self.send(attr)
name = val.is_a?(Array) ? val[0] : val
return name.strip
end
end
return ""
end

def description
[self.class.presentation[:desc_attribute], 'description'].each do |attr|
if self.has_attribute?(attr) and self.attribute_present?(attr)
return self[attr].is_a?(Array) ? self[attr][0] : self[attr]
end
end
return ""
end

def possible_relations
self.associations.collect {|assoc| assoc.to_s } - ['children']
end

def relations
rel_list = []

rel_list += self.class.presentation[:associated_relations] if self.class.presentation[:associated_relations]

aspects.each do |aspect|
aspect_data = self.class.mapper.get_aspect(aspect)
if defined?(aspect_data[:presentation][:associated_relations]) and aspect_data[:presentation][:associated_relations]
rel_list += aspect_data[:presentation][:associated_relations]
end
end

rel_list & possible_relations
end

def aspects
present_aspects = []
(self.class.presentation[:allowed_aspects] || []).each do |aspect|
aspect_data = self.class.mapper.get_aspect(aspect)
aspect_mapping = aspect_data[:mapping]
present_aspects << aspect if self.classes & aspect_mapping[:classes] == aspect_mapping[:classes]
end

present_aspects
end

def objectclasses_attr_list(objectclass_list)
objectclass_list = [objectclass_list] unless objectclass_list.is_a? Array
list = []
objectclass_list.each do |objectclass|
objectclass_obj = ActiveLdap::Base.schema.object_class(objectclass)
attr_list = objectclass_obj.must + objectclass_obj.may
list += attr_list.collect{|attr| attr.human_attribute_name }
end
list
end

def organized_data(options = {})
options.symbolize_keys!
options[:expert] ||= false;
options[:skip_binary] ||= false;

ignored_attrs = self.mapper.get_global_config[:hidden_attributes] || []
ignored_attrs += self.class.presentation[:hidden_attributes] || []
ignored_attrs += (self.class.presentation[:expert_attributes] || []) unless options[:expert]
attr_list = self.attributes.keys - ignored_attrs

aspects = self.aspects
rel_list = self.possible_relations

# first pass to take aspects forced relations into account
obj_aspects = {}
aspects.each do |aspect|
aspect_data = self.class.mapper.get_aspect(aspect)

if defined?(aspect_data[:presentation][:associated_attributes]) and aspect_data[:presentation][:associated_attributes]
taken_attr_list = aspect_data[:presentation][:associated_attributes] & (attr_list + ignored_attrs)
unless taken_attr_list.empty?
obj_aspects[aspect] ||= {}
obj_aspects[aspect].merge!(fetch_attributes_data(taken_attr_list, options))
attr_list -= taken_attr_list
end
end

if defined?(aspect_data[:presentation][:associated_relations]) and aspect_data[:presentation][:associated_relations]
taken_rel_list = aspect_data[:presentation][:associated_relations] & rel_list
unless taken_rel_list.empty?
obj_aspects[aspect] ||= {}
obj_aspects[aspect].merge!(fetch_relations_data(taken_rel_list, options))
rel_list -= taken_rel_list
end
end
end

# manage general attributes
obj_info = {}
if self.class.presentation[:associate_unclaimed_attributes]
taken_attr_list = attr_list
else
taken_attr_list = []
if self.class.presentation.has_key?(:associated_attributes)
taken_attr_list += self.class.presentation[:associated_attributes]
end
taken_attr_list += objectclasses_attr_list(self.required_classes + (self.class.presentation[:optional_classes] || []))
end
taken_attr_list += OPERATIONAL_ATTRIBUTES if options[:expert]
taken_attr_list = taken_attr_list.uniq & attr_list
obj_info = fetch_attributes_data(taken_attr_list, options)
attr_list -= taken_attr_list

# manage general relations
if self.class.presentation[:associated_relations]
taken_rel_list = self.class.presentation[:associated_relations] & rel_list
unless taken_rel_list.empty?
obj_info.merge!(fetch_relations_data(taken_rel_list, options))
rel_list -= taken_rel_list
end
end

# second pass to dispath the remaining attributes
unless attr_list.empty?
aspects.each do |aspect|
aspect_data = self.class.mapper.get_aspect(aspect)

taken_attr_list = (objectclasses_attr_list(aspect_data[:mapping][:classes]) & attr_list)
obj_aspects[aspect] ||= {}
obj_aspects[aspect].merge!(fetch_attributes_data(taken_attr_list, options))
attr_list -= taken_attr_list

break if attr_list.empty?
end
end

[obj_info, obj_aspects]
end

def family_parent_dn
pdn = self.dn_obj.dup
pdn.shift
pdn
end

def family_children
LdapObject.find(:all, :base => self.dn, :scope => :one)
end

def family_children_dn
self.family_children.collect {|obj| obj.dn }
end

def family_siblings
# cannot substract, as the ruby object signature may be different
LdapObject.find(:all, :base => self.family_parent_dn.to_s, :scope => :one).select{|obj| obj.dn != self.dn }
end

def family_siblings_dn
self.family_siblings.collect {|obj| obj.dn }
end

protected

def fetch_attributes_data(attr_list, options = {})
attr_data = self.attributes.select {|key, val| attr_list.include?(key) and not (options[:skip_binary] and ActiveLdap::Base.schema.attribute(key).binary?) }
Hash[attr_data]
end

def fetch_relations_data(rel_list, options = {})
rel_data = rel_list.collect do |rel|
data = self.send(rel).collect{|g| g.name }
data.empty? ? nil : ["rel:" + rel, data]
end
Hash[rel_data.compact]
end
end
end

(4-4/4)