root/lib/ldap_shadows/elements/object.rb @ effa90c8
89d8bebc | Marc Dequènes (Duck) | #--
|
|
# 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
|
|||
5a48a521 | Marc Dequènes (Duck) | module Elements
|
|
89d8bebc | Marc Dequènes (Duck) | class LdapObject < ActiveLdap::Base
|
|
90809ae4 | Marc Dequènes (Duck) | include LdapElement
|
|
88b0bbce | Marc Dequènes (Duck) | ||
90809ae4 | Marc Dequènes (Duck) | @relations_info = {}
|
|
89d8bebc | Marc Dequènes (Duck) | ||
90809ae4 | Marc Dequènes (Duck) | class << self
|
|
0032f05d | Marc Dequènes (Duck) | attr_reader :relations_info
|
|
90809ae4 | Marc Dequènes (Duck) | end
|
|
# default
|
|||
89d8bebc | Marc Dequènes (Duck) | ldap_mapping :prefix => '', :classes => ['top'], :scope => :sub
|
|
90809ae4 | Marc Dequènes (Duck) | def self.cast
|
|
f25d0aed | Marc Dequènes (Duck) | super
|
|
90809ae4 | Marc Dequènes (Duck) | ldap_mapping self.parameters[:mapping]
|
|
end
|
|||
def self.cast_relations
|
|||
f25d0aed | Marc Dequènes (Duck) | super
|
|
90809ae4 | Marc Dequènes (Duck) | object_rel = {}
|
|
object_rel.merge!(self.parameters[:relations]) if self.parameters.include?(:relations)
|
|||
if self.parameters[:presentation].has_key?(:allowed_aspects)
|
|||
76228fb7 | Marc Dequènes (Duck) | self.parameters[:presentation][:allowed_aspects].each do |aspect_name|
|
|
aspect = self.shadow.get_aspect(aspect_name)
|
|||
if aspect.nil?
|
|||
raise PreProcessingError, _("Aspect '%s' is missing for object '%s'") % [aspect_name, self.handle]
|
|||
90809ae4 | Marc Dequènes (Duck) | end
|
|
76228fb7 | Marc Dequènes (Duck) | aspect_data = aspect.parameters
|
|
90809ae4 | Marc Dequènes (Duck) | 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
|
|||
0032f05d | Marc Dequènes (Duck) | instance_variable_set(:@relations_info, object_relations_info)
|
|
90809ae4 | Marc Dequènes (Duck) | end
|
|
e04ae9bc | Marc Dequènes (Duck) | # temporary method until active_ldap is fixed: return a DN object (see activeldap#23932)
|
|
89d8bebc | Marc Dequènes (Duck) | def dn_obj
|
|
ActiveLdap::DistinguishedName.parse(self.dn)
|
|||
end
|
|||
e04ae9bc | Marc Dequènes (Duck) | # temporary method until active_ldap is fixed: return a DN object (see activeldap#23932)
|
|
def self.base_obj
|
|||
ActiveLdap::DistinguishedName.parse(self.base)
|
|||
end
|
|||
89d8bebc | Marc Dequènes (Duck) | def name
|
|
2d1982bd | Marc Dequènes (Duck) | name = self[dn_attribute] || self.attributes[dn_attribute] || self.dn
|
|
name = name[0] if name.is_a? Array
|
|||
89d8bebc | Marc Dequènes (Duck) | name.strip
|
|
end
|
|||
5d28ad6e | Marc Dequènes (Duck) | def has_field?(field)
|
|
return false if field.downcase == "objectclass"
|
|||
has_attribute?(field)
|
|||
end
|
|||
89d8bebc | Marc Dequènes (Duck) | def human_name
|
|
718238ae | Marc Dequènes (Duck) | attr_list = ['displayName', 'cn']
|
|
0032f05d | Marc Dequènes (Duck) | attr_list.unshift(self.class.parameters[:presentation][:name_attribute]) if self.class.parameters[:presentation].has_key?(:name_attribute)
|
|
718238ae | Marc Dequènes (Duck) | attr_list.each do |attr|
|
|
if attr == 'dn'
|
|||
return self.dn
|
|||
elsif self.attribute_present?(attr)
|
|||
val = self.send(attr, true)
|
|||
return val[0].strip
|
|||
89d8bebc | Marc Dequènes (Duck) | end
|
|
end
|
|||
return ""
|
|||
end
|
|||
378df927 | Marc Dequènes (Duck) | def human_description
|
|
718238ae | Marc Dequènes (Duck) | attr_list = ['description']
|
|
0032f05d | Marc Dequènes (Duck) | attr_list.unshift(self.class.parameters[:presentation][:desc_attribute]) if self.class.parameters[:presentation].has_key?(:desc_attribute)
|
|
718238ae | Marc Dequènes (Duck) | attr_list.each do |attr|
|
|
if self.attribute_present?(attr)
|
|||
89d8bebc | Marc Dequènes (Duck) | 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 = []
|
|||
0032f05d | Marc Dequènes (Duck) | rel_list += self.class.parameters[:presentation][:associated_relations] if self.class.parameters[:presentation][:associated_relations]
|
|
89d8bebc | Marc Dequènes (Duck) | ||
aspects.each do |aspect|
|
|||
2e882c9f | Marc Dequènes (Duck) | aspect_data = self.class.shadow.get_aspect(aspect).parameters
|
|
89d8bebc | Marc Dequènes (Duck) | 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
|
|||
6089b33a | Marc Dequènes (Duck) | def self.possible_aspects
|
|
0032f05d | Marc Dequènes (Duck) | return [] unless self.parameters[:presentation][:allowed_aspects]
|
|
self.parameters[:presentation][:allowed_aspects].collect{|key| key.to_s }.sort
|
|||
6089b33a | Marc Dequènes (Duck) | end
|
|
89d8bebc | Marc Dequènes (Duck) | def aspects
|
|
present_aspects = []
|
|||
0032f05d | Marc Dequènes (Duck) | (self.class.parameters[:presentation][:allowed_aspects] || []).each do |aspect|
|
|
2e882c9f | Marc Dequènes (Duck) | aspect_data = self.class.shadow.get_aspect(aspect).parameters
|
|
89d8bebc | Marc Dequènes (Duck) | aspect_mapping = aspect_data[:mapping]
|
|
present_aspects << aspect if self.classes & aspect_mapping[:classes] == aspect_mapping[:classes]
|
|||
end
|
|||
present_aspects
|
|||
end
|
|||
a24bc5b1 | Marc Dequènes (Duck) | def self.objectclasses_attr_list(objectclass_list)
|
|
89d8bebc | Marc Dequènes (Duck) | 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
|
|||
a24bc5b1 | Marc Dequènes (Duck) | def self.possible_attributes
|
|
0c7af251 | Marc Dequènes (Duck) | self.objectclasses_attr_list(self.required_classes)
|
|
a24bc5b1 | Marc Dequènes (Duck) | end
|
|
def self.possible_attributes_for_aspect(aspect)
|
|||
2e882c9f | Marc Dequènes (Duck) | aspect_data = self.shadow.get_aspect(aspect).parameters
|
|
a24bc5b1 | Marc Dequènes (Duck) | self.objectclasses_attr_list(aspect_data[:mapping][:classes])
|
|
end
|
|||
43bd8057 | Marc Dequènes (Duck) | def info_for_relation(rel)
|
|
return nil unless self.relations.include?(rel)
|
|||
0032f05d | Marc Dequènes (Duck) | self.class.relations_info[rel.to_sym]
|
|
43bd8057 | Marc Dequènes (Duck) | end
|
|
f7217dcd | Marc Dequènes (Duck) | def organized_data
|
|
bbe89518 | Marc Dequènes (Duck) | ignored_attrs = self.class.shadow.get_config[:presentation][:hidden_attributes] || []
|
|
0032f05d | Marc Dequènes (Duck) | ignored_attrs += self.class.parameters[:presentation][:hidden_attributes] || []
|
|
d9091cc0 | Marc Dequènes (Duck) | attr_list = self.nonempty_attributes - ignored_attrs
|
|
89d8bebc | Marc Dequènes (Duck) | ||
0032f05d | Marc Dequènes (Duck) | expert_attributes = (self.class.parameters[:presentation][:expert_attributes] || [])
|
|
b52f0f7d | Marc Dequènes (Duck) | admin_attributes = attr_list.select do |attr|
|
|
ActiveLdap::Base.schema.attribute(attr).operational?
|
|||
end
|
|||
89d8bebc | Marc Dequènes (Duck) | aspects = self.aspects
|
|
rel_list = self.possible_relations
|
|||
# first pass to take aspects forced relations into account
|
|||
obj_aspects = {}
|
|||
aspects.each do |aspect|
|
|||
2e882c9f | Marc Dequènes (Duck) | aspect_data = self.class.shadow.get_aspect(aspect).parameters
|
|
89d8bebc | Marc Dequènes (Duck) | ||
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] ||= {}
|
|||
b52f0f7d | Marc Dequènes (Duck) | obj_aspects[aspect].merge!(fetch_attributes_data(taken_attr_list, expert_attributes, admin_attributes))
|
|
89d8bebc | Marc Dequènes (Duck) | 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] ||= {}
|
|||
f7217dcd | Marc Dequènes (Duck) | obj_aspects[aspect].merge!(fetch_relations_data(taken_rel_list, expert_attributes))
|
|
89d8bebc | Marc Dequènes (Duck) | rel_list -= taken_rel_list
|
|
end
|
|||
end
|
|||
end
|
|||
# manage general attributes
|
|||
obj_info = {}
|
|||
0032f05d | Marc Dequènes (Duck) | if self.class.parameters[:presentation][:associate_unclaimed_attributes]
|
|
89d8bebc | Marc Dequènes (Duck) | taken_attr_list = attr_list
|
|
else
|
|||
b52f0f7d | Marc Dequènes (Duck) | taken_attr_list = admin_attributes
|
|
0032f05d | Marc Dequènes (Duck) | if self.class.parameters[:presentation].has_key?(:associated_attributes)
|
|
taken_attr_list += self.class.parameters[:presentation][:associated_attributes]
|
|||
89d8bebc | Marc Dequènes (Duck) | end
|
|
a24bc5b1 | Marc Dequènes (Duck) | taken_attr_list += self.class.possible_attributes
|
|
89d8bebc | Marc Dequènes (Duck) | end
|
|
taken_attr_list = taken_attr_list.uniq & attr_list
|
|||
b52f0f7d | Marc Dequènes (Duck) | obj_info = fetch_attributes_data(taken_attr_list, expert_attributes, admin_attributes)
|
|
89d8bebc | Marc Dequènes (Duck) | attr_list -= taken_attr_list
|
|
# manage general relations
|
|||
0032f05d | Marc Dequènes (Duck) | if self.class.parameters[:presentation][:associated_relations]
|
|
taken_rel_list = self.class.parameters[:presentation][:associated_relations] & rel_list
|
|||
89d8bebc | Marc Dequènes (Duck) | unless taken_rel_list.empty?
|
|
f7217dcd | Marc Dequènes (Duck) | obj_info.merge!(fetch_relations_data(taken_rel_list, expert_attributes))
|
|
89d8bebc | Marc Dequènes (Duck) | rel_list -= taken_rel_list
|
|
end
|
|||
end
|
|||
# second pass to dispath the remaining attributes
|
|||
unless attr_list.empty?
|
|||
aspects.each do |aspect|
|
|||
a24bc5b1 | Marc Dequènes (Duck) | taken_attr_list = (self.class.possible_attributes_for_aspect(aspect) & attr_list)
|
|
89d8bebc | Marc Dequènes (Duck) | obj_aspects[aspect] ||= {}
|
|
b52f0f7d | Marc Dequènes (Duck) | obj_aspects[aspect].merge!(fetch_attributes_data(taken_attr_list, expert_attributes, admin_attributes))
|
|
89d8bebc | Marc Dequènes (Duck) | attr_list -= taken_attr_list
|
|
break if attr_list.empty?
|
|||
end
|
|||
end
|
|||
[obj_info, obj_aspects]
|
|||
end
|
|||
3d5fa735 | Marc Dequènes (Duck) | def item_modify_from_string(str)
|
|
unless str =~ /^([a-zA-Z]*(?::[a-zA-Z]+)?)(=|\+=|-=)(.*)$/
|
|||
raise SyntaxError, _("modification parameter '%s' is invalid") % str
|
|||
end
|
|||
key = $1
|
|||
op = $2
|
|||
val = $3
|
|||
item_modify(key, op, val)
|
|||
end
|
|||
def item_modify(key, op, val)
|
|||
if key.index(":")
|
|||
type, field = key.split(":")
|
|||
else
|
|||
type = nil
|
|||
field = key
|
|||
end
|
|||
case type
|
|||
when nil
|
|||
item_modify_field(key, op, val)
|
|||
when 'rel'
|
|||
item_modify_relation(field, op, val)
|
|||
when ''
|
|||
case field
|
|||
when 'aspects'
|
|||
item_modify_aspects(op, val)
|
|||
else
|
|||
raise PreProcessingError, _("Unknown core field '%s'") % field
|
|||
end
|
|||
else
|
|||
raise PreProcessingError, _("Unknown type '%s' for field '%s'") % [type, field]
|
|||
end
|
|||
end
|
|||
def item_modify_field(field, op, val)
|
|||
unless self.has_field?(field)
|
|||
raise PreProcessingError, _("No such field '%s' in object '%s'") % [field, self.class.handle]
|
|||
end
|
|||
attr_info = ActiveLdap::Base.schema.attribute(field)
|
|||
if attr_info.read_only?
|
|||
raise PreProcessingError, _("The field '%s' cannot be modified (read only)") % field
|
|||
end
|
|||
if attr_info.binary?
|
|||
unless File.exists?(val)
|
|||
raise PreProcessingError, _("The field '%s' contains binary data, you must provide a filename instead of a direct value") % field
|
|||
end
|
|||
begin
|
|||
val = File.read(val)
|
|||
rescue
|
|||
raise PreProcessingError, _("The file for the binary field '%s' cannot be read: ") % [field, $!]
|
|||
end
|
|||
end
|
|||
old_val = self.send(field, true)
|
|||
# if val is nil or the latest value is removed, then the attribute is removed from the object,
|
|||
case op
|
|||
when '='
|
|||
73a3d129 | Marc Dequènes (Duck) | val = [val] if old_val.is_a? Enumerable
|
|
3d5fa735 | Marc Dequènes (Duck) | return false if val == old_val
|
|
self.send(field + "=", val)
|
|||
when '+='
|
|||
if attr_info.single_value?
|
|||
raise PreProcessingError, _("The field '%s' cannot hold more than one value") % field
|
|||
end
|
|||
return false if old_val.include?(val)
|
|||
new_val = old_val << val
|
|||
self.send(field + "=", new_val)
|
|||
when '-='
|
|||
return false unless old_val.include?(val)
|
|||
new_val = old_val - [val]
|
|||
self.send(field + "=", new_val)
|
|||
else
|
|||
raise SyntaxError, _("Unknown operator '%s'") % op
|
|||
end
|
|||
true
|
|||
end
|
|||
def item_modify_relation(rel, op, val)
|
|||
unless self.relations.include?(rel)
|
|||
raise PreProcessingError, _("No such relation '%s' for object '%s'") % [rel, self.class.handle]
|
|||
end
|
|||
rel_info = self.info_for_relation(rel)
|
|||
if rel_info[:read_only]
|
|||
raise PreProcessingError, _("The relation '%s' cannot be modified (read only)") % rel
|
|||
end
|
|||
# fetch remote object in relation, which will be the real 'val'
|
|||
foreign_item_list = rel_info[:foreign_klass].find(:all, val)
|
|||
if foreign_item_list.empty?
|
|||
raise PreProcessingError, _("Foreign item '%s' for relation '%s' not found") % [val, rel]
|
|||
end
|
|||
if foreign_item_list.size > 1
|
|||
raise WeirdError, _("Ambiguous item '%s' for relation '%s' (%s possible items)") %
|
|||
[val, rel, foreign_item_list.size]
|
|||
end
|
|||
old_val = self.send(rel)
|
|||
val = foreign_item_list.first
|
|||
# if val is nil or the latest value is removed, then the association's attribute is removed from one side
|
|||
case op
|
|||
when '='
|
|||
val = [val] if old_val.is_a? Enumerable
|
|||
self.send(rel + "=", val)
|
|||
when '+='
|
|||
if rel_info[:single_value]
|
|||
raise PreProcessingError, _("The relation '%s' cannot hold more than one foreign item") % rel
|
|||
end
|
|||
return false if old_val.include?(val)
|
|||
self.send(rel) << val
|
|||
when '-='
|
|||
return false unless old_val.include?(val)
|
|||
self.send(rel).delete(val)
|
|||
else
|
|||
raise SyntaxError, _("Unknown operator '%s'") % op
|
|||
end
|
|||
true
|
|||
end
|
|||
def item_modify_aspects(op, aspect)
|
|||
unless self.class.possible_aspects.include?(aspect)
|
|||
raise PreProcessingError, _("No such aspect '%s' for object '%s'") % [aspect, self.class.handle]
|
|||
end
|
|||
case op
|
|||
when '='
|
|||
raise PreProcessingError, _("The equality operator is not possible for aspects")
|
|||
when '+='
|
|||
return false if self.aspects.include?(aspect)
|
|||
self.add_aspect(aspect)
|
|||
when '-='
|
|||
return false unless self.aspects.include?(aspect)
|
|||
self.remove_aspect(aspect)
|
|||
else
|
|||
raise SyntaxError, _("Unknown operator '%s'") % op
|
|||
end
|
|||
true
|
|||
end
|
|||
bbe89518 | Marc Dequènes (Duck) | def self.items_find_from_strings(shadow, str_list)
|
|
bd714351 | Marc Dequènes (Duck) | ldap_search_objects = "(objectClass=*)"
|
|
ldap_search_aspects = ""
|
|||
ldap_search_fields = []
|
|||
str_list.each do |str|
|
|||
unless str =~ /^([a-zA-Z]*(?::[a-zA-Z]+)?)(=|~=)(.*)$/
|
|||
raise SyntaxError, _("search parameter '%s' is invalid") % str
|
|||
end
|
|||
key = $1
|
|||
op = $2
|
|||
val = $3
|
|||
if key.index(":")
|
|||
type, field = key.split(":")
|
|||
else
|
|||
type = nil
|
|||
field = key
|
|||
end
|
|||
case type
|
|||
when nil
|
|||
ldap_search_fields << ldap_search_string_field(field, op, val)
|
|||
when 'rel'
|
|||
raise PreProcessingError, _("Searching relations is not implemented yet")
|
|||
when ''
|
|||
case field
|
|||
when 'objects'
|
|||
bbe89518 | Marc Dequènes (Duck) | ldap_search_objects = ldap_search_string_objects(shadow, field, op, val)
|
|
bd714351 | Marc Dequènes (Duck) | when 'aspects'
|
|
bbe89518 | Marc Dequènes (Duck) | ldap_search_aspects = ldap_search_string_aspects(shadow, field, op, val)
|
|
bd714351 | Marc Dequènes (Duck) | else
|
|
raise PreProcessingError, _("Unknown core field '%s'") % field
|
|||
end
|
|||
else
|
|||
raise PreProcessingError, _("Unknown type '%s' for field '%s'") % [type, field]
|
|||
end
|
|||
end
|
|||
ldap_search_string = "(&" + ldap_search_objects + ldap_search_aspects + ldap_search_fields.join + ")"
|
|||
c30d4613 | Marc Dequènes (Duck) | LdapObject.find(:all, :scope => :sub, :filter => ldap_search_string, :attributes => ["objectClass"])
|
|
bd714351 | Marc Dequènes (Duck) | end
|
|
def self.ldap_search_string_field(field, op, val)
|
|||
esc_val = self.connection.escape_filter_value(val)
|
|||
case op
|
|||
when "="
|
|||
"(#{field}=#{esc_val})"
|
|||
when "~="
|
|||
raise PreProcessingError, _("Searching with regex is not implemented yet")
|
|||
else
|
|||
raise SyntaxError, _("Unknown operator '%s'") % op
|
|||
end
|
|||
end
|
|||
bbe89518 | Marc Dequènes (Duck) | def self.ldap_search_string_objects(shadow, field, op, val_list)
|
|
bd714351 | Marc Dequènes (Duck) | ldap_search_parts = val_list.split(",").collect do |val|
|
|
obj_hdl = val.downcase.singularize
|
|||
90809ae4 | Marc Dequènes (Duck) | obj_klass = shadow.get_object(obj_hdl)
|
|
bd714351 | Marc Dequènes (Duck) | raise PreProcessingError, _("No such object '%s'") % val if obj_klass.nil?
|
|
ldap_classes = obj_klass.required_classes
|
|||
case op
|
|||
when "="
|
|||
"(&" + ldap_classes.collect{|cl| "(objectClass=#{cl})" }.join + ")"
|
|||
when "~="
|
|||
raise PreProcessingError, _("Searching with regex is not possible with objects")
|
|||
else
|
|||
raise SyntaxError, _("Unknown operator '%s'") % op
|
|||
end
|
|||
end
|
|||
"(|" + ldap_search_parts.join + ")"
|
|||
end
|
|||
bbe89518 | Marc Dequènes (Duck) | def self.ldap_search_string_aspects(shadow, field, op, val_list)
|
|
bd714351 | Marc Dequènes (Duck) | ldap_search_parts = val_list.split(",").collect do |val|
|
|
2e882c9f | Marc Dequènes (Duck) | aspect = shadow.get_aspect(val)
|
|
raise PreProcessingError, _("No such aspect '%s'") % val if aspect.nil?
|
|||
aspect_data = aspect.parameters
|
|||
bd714351 | Marc Dequènes (Duck) | ||
ldap_classes = aspect_data[:mapping][:classes]
|
|||
case op
|
|||
when "="
|
|||
"(&" + ldap_classes.collect{|cl| "(objectClass=#{cl})" }.join + ")"
|
|||
when "~="
|
|||
raise PreProcessingError, _("Searching with regex is not possible with aspects")
|
|||
else
|
|||
raise SyntaxError, _("Unknown operator '%s'") % op
|
|||
end
|
|||
end
|
|||
"(&" + ldap_search_parts.join + ")"
|
|||
end
|
|||
bbe89518 | Marc Dequènes (Duck) | def self.find_raw_item_object(shadow, raw_item)
|
|
540aa3c6 | Marc Dequènes (Duck) | # easy case
|
|
base_obj_klass = LdapShadows::Elements::LdapObject
|
|||
if raw_item.class != base_obj_klass and raw_item.class.ancestors.include?(base_obj_klass)
|
|||
return raw_item.class.handle
|
|||
end
|
|||
bbe89518 | Marc Dequènes (Duck) | shadow.objects.each do |obj_hdl|
|
|
90809ae4 | Marc Dequènes (Duck) | obj_klass = shadow.get_object(obj_hdl)
|
|
a4aeb5ee | Marc Dequènes (Duck) | ldap_classes = obj_klass.required_classes
|
|
c30d4613 | Marc Dequènes (Duck) | return obj_hdl if raw_item.classes & ldap_classes == ldap_classes
|
|
a4aeb5ee | Marc Dequènes (Duck) | end
|
|
nil
|
|||
end
|
|||
bbe89518 | Marc Dequènes (Duck) | def self.raw_item_info(shadow, raw_item)
|
|
obj_hdl = self.find_raw_item_object(shadow, raw_item)
|
|||
c30d4613 | Marc Dequènes (Duck) | if obj_hdl
|
|
90809ae4 | Marc Dequènes (Duck) | obj_klass = shadow.get_object(obj_hdl)
|
|
c30d4613 | Marc Dequènes (Duck) | item = obj_klass.new(raw_item.dn)
|
|
{:name => "#{obj_hdl}/#{item.name}", :item => item, :object => obj_klass}
|
|||
else
|
|||
33cde370 | Marc Dequènes (Duck) | {:name => "unknown/#{raw_item.dn}"}
|
|
c30d4613 | Marc Dequènes (Duck) | end
|
|
end
|
|||
89d8bebc | Marc Dequènes (Duck) | def family_parent_dn
|
|
pdn = self.dn_obj.dup
|
|||
pdn.shift
|
|||
pdn
|
|||
end
|
|||
c30d4613 | Marc Dequènes (Duck) | def family_parent
|
|
# nothing found when using LdapObject, that's weird, so using ActiveLdap::Base
|
|||
# until further investigation
|
|||
#LdapObject.find(:first, :base => self.family_parent_dn.to_s, :scope => :base)
|
|||
ActiveLdap::Base.find(:first, :base => self.family_parent_dn.to_s, :scope => :base)
|
|||
end
|
|||
89d8bebc | Marc Dequènes (Duck) | 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
|
|||
70b17bfa | Marc Dequènes (Duck) | def modified_attributes(op_types = nil, att_only = false)
|
|
0814bfc3 | Marc Dequènes (Duck) | list = []
|
|
prepare_data_for_saving do |data, ldap_data|
|
|||
list = collect_modified_attributes(ldap_data, data)
|
|||
false
|
|||
end
|
|||
70b17bfa | Marc Dequènes (Duck) | ||
unless op_types.nil?
|
|||
list.reject! do |mod_info|
|
|||
op_type, att, new_val = mod_info
|
|||
not op_types.include? op_type
|
|||
end
|
|||
end
|
|||
if att_only
|
|||
list.collect! do |mod_info|
|
|||
op_type, att, new_val = mod_info
|
|||
att
|
|||
end
|
|||
end
|
|||
0814bfc3 | Marc Dequènes (Duck) | list
|
|
end
|
|||
d9091cc0 | Marc Dequènes (Duck) | def add_aspect(aspect)
|
|
return unless self.class.possible_aspects.include?(aspect)
|
|||
2e882c9f | Marc Dequènes (Duck) | aspect_data = self.class.shadow.get_aspect(aspect).parameters
|
|
d9091cc0 | Marc Dequènes (Duck) | add_class(*aspect_data[:mapping][:classes])
|
|
13594999 | Marc Dequènes (Duck) | ||
# recursive dependency enforcement
|
|||
depends = aspect_data[:mapping][:depend_aspects] || []
|
|||
depends.each do |dep_aspect|
|
|||
add_aspect(dep_aspect)
|
|||
end
|
|||
d9091cc0 | Marc Dequènes (Duck) | end
|
|
def remove_aspect(aspect)
|
|||
return unless self.class.possible_aspects.include?(aspect)
|
|||
2e882c9f | Marc Dequènes (Duck) | aspect_data = self.class.shadow.get_aspect(aspect).parameters
|
|
d9091cc0 | Marc Dequènes (Duck) | remove_class(*aspect_data[:mapping][:classes])
|
|
end
|
|||
def nonempty_attributes
|
|||
self.attributes.collect{|key, val| (val.nil? or val == []) ? nil : key }.compact
|
|||
end
|
|||
def missing_attributes
|
|||
self.must.collect{|attr| attr.name } - self.nonempty_attributes - ['objectClass']
|
|||
end
|
|||
3de546c5 | Marc Dequènes (Duck) | def delete(options = {})
|
|
7b832340 | Marc Dequènes (Duck) | before_delete_jobs
|
|
3de546c5 | Marc Dequènes (Duck) | super(options)
|
|
d8cfa085 | Marc Dequènes (Duck) | after_save_jobs
|
|
end
|
|||
def delete_recursive
|
|||
# TODO: recursive instanciation and reverse recursive hook calls
|
|||
7b832340 | Marc Dequènes (Duck) | before_delete_jobs
|
|
d8cfa085 | Marc Dequènes (Duck) | self.class.delete_all(nil, :scope => :sub, :base => self.dn)
|
|
after_delete_jobs
|
|||
dba6a81b | Marc Dequènes (Duck) | end
|
|
953a2b4d | Marc Dequènes (Duck) | # cannot override create_or_update() because of alias chaining
|
|
def save
|
|||
before_save_jobs
|
|||
r = super
|
|||
after_save_jobs
|
|||
r
|
|||
end
|
|||
89d8bebc | Marc Dequènes (Duck) | ||
953a2b4d | Marc Dequènes (Duck) | def save!
|
|
3de546c5 | Marc Dequènes (Duck) | before_save_jobs
|
|
r = super
|
|||
after_save_jobs
|
|||
r
|
|||
end
|
|||
953a2b4d | Marc Dequènes (Duck) | protected
|
|
5aa80ef6 | Marc Dequènes (Duck) | def before_save_jobs
|
|
d8cfa085 | Marc Dequènes (Duck) | check_hooks_before(:save)
|
|
5aa80ef6 | Marc Dequènes (Duck) | check_missing_attributes
|
|
70b17bfa | Marc Dequènes (Duck) | check_password
|
|
5aa80ef6 | Marc Dequènes (Duck) | end
|
|
d8cfa085 | Marc Dequènes (Duck) | def before_delete_jobs
|
|
check_hooks_before(:delete)
|
|||
end
|
|||
def check_hooks_before(action)
|
|||
7b832340 | Marc Dequènes (Duck) | case action
|
|
when :save
|
|||
if self.new_entry?
|
|||
953a2b4d | Marc Dequènes (Duck) | self.class.hook_before_create(self)
|
|
7b832340 | Marc Dequènes (Duck) | else
|
|
953a2b4d | Marc Dequènes (Duck) | self.class.hook_before_modify(self)
|
|
7b832340 | Marc Dequènes (Duck) | end
|
|
when :delete
|
|||
953a2b4d | Marc Dequènes (Duck) | self.class.hook_before_delete(self)
|
|
7b832340 | Marc Dequènes (Duck) | end
|
|
953a2b4d | Marc Dequènes (Duck) | # TODO: move this in the LdapAspect class
|
|
dba6a81b | Marc Dequènes (Duck) | self.aspects.each do |aspect|
|
|
2e882c9f | Marc Dequènes (Duck) | aklass = self.class.shadow.get_aspect(aspect)
|
|
dba6a81b | Marc Dequènes (Duck) | next if aklass.nil?
|
|
d8cfa085 | Marc Dequènes (Duck) | case action
|
|
when :save
|
|||
if self.new_entry?
|
|||
953a2b4d | Marc Dequènes (Duck) | aklass.hook_before_create(self)
|
|
d8cfa085 | Marc Dequènes (Duck) | else
|
|
953a2b4d | Marc Dequènes (Duck) | aklass.hook_before_modify(self)
|
|
d8cfa085 | Marc Dequènes (Duck) | end
|
|
when :delete
|
|||
953a2b4d | Marc Dequènes (Duck) | aklass.hook_before_delete(self)
|
|
dba6a81b | Marc Dequènes (Duck) | end
|
|
end
|
|||
end
|
|||
5aa80ef6 | Marc Dequènes (Duck) | def check_missing_attributes
|
|
missing_fields = self.missing_attributes
|
|||
unless missing_fields.empty?
|
|||
miss_str = []
|
|||
missing_fields.each do |field|
|
|||
str = Translator.translate_field_name(field)
|
|||
str += " [#{field}]" if $program_options[:handles]
|
|||
miss_str << str
|
|||
end
|
|||
raise PreProcessingError, _("Cannot save the item; the following fields are missing: %s") %
|
|||
miss_str.join(", ")
|
|||
end
|
|||
end
|
|||
70b17bfa | Marc Dequènes (Duck) | def check_password
|
|
return unless self.modified_attributes([:replace], true).include? 'userPassword'
|
|||
hash_func = self.class.config.global_config[:password_hash]
|
|||
return if hash_func.nil?
|
|||
self.userPassword = ActiveLdap::UserPassword.send(hash_func, self.userPassword)
|
|||
end
|
|||
d8cfa085 | Marc Dequènes (Duck) | def after_save_jobs
|
|
check_hooks_after(:save)
|
|||
end
|
|||
def after_delete_jobs
|
|||
check_hooks_after(:delete)
|
|||
end
|
|||
def check_hooks_after(action)
|
|||
self.aspects.each do |aspect|
|
|||
2e882c9f | Marc Dequènes (Duck) | aklass = self.class.shadow.get_aspect(aspect)
|
|
d8cfa085 | Marc Dequènes (Duck) | next if aklass.nil?
|
|
case action
|
|||
when :save
|
|||
if self.new_entry?
|
|||
953a2b4d | Marc Dequènes (Duck) | aklass.hook_after_create(self)
|
|
d8cfa085 | Marc Dequènes (Duck) | else
|
|
953a2b4d | Marc Dequènes (Duck) | aklass.hook_after_modify(self)
|
|
d8cfa085 | Marc Dequènes (Duck) | end
|
|
when :delete
|
|||
953a2b4d | Marc Dequènes (Duck) | aklass.hook_after_delete(self)
|
|
d8cfa085 | Marc Dequènes (Duck) | end
|
|
end
|
|||
7b832340 | Marc Dequènes (Duck) | ||
953a2b4d | Marc Dequènes (Duck) | # TODO: move this in the LdapAspect class
|
|
7b832340 | Marc Dequènes (Duck) | case action
|
|
when :save
|
|||
if self.new_entry?
|
|||
953a2b4d | Marc Dequènes (Duck) | self.class.hook_after_create(self)
|
|
7b832340 | Marc Dequènes (Duck) | else
|
|
953a2b4d | Marc Dequènes (Duck) | self.class.hook_after_modify(self)
|
|
7b832340 | Marc Dequènes (Duck) | end
|
|
when :delete
|
|||
953a2b4d | Marc Dequènes (Duck) | self.class.hook_after_delete(self)
|
|
7b832340 | Marc Dequènes (Duck) | end
|
|
d8cfa085 | Marc Dequènes (Duck) | end
|
|
b52f0f7d | Marc Dequènes (Duck) | def fetch_attributes_data(attr_list, expert_attributes, admin_attributes)
|
|
f7217dcd | Marc Dequènes (Duck) | attr_data = self.attributes.collect do |key, val|
|
|
if attr_list.include?(key)
|
|||
[key, {
|
|||
:value => val,
|
|||
:multiple => (val.is_a?(Array) ? val.size : 1),
|
|||
:expert => expert_attributes.include?(key),
|
|||
b52f0f7d | Marc Dequènes (Duck) | :admin => admin_attributes.include?(key),
|
|
f7217dcd | Marc Dequènes (Duck) | :binary => ActiveLdap::Base.schema.attribute(key).binary?
|
|
}]
|
|||
else
|
|||
nil
|
|||
end
|
|||
end
|
|||
Hash[attr_data.compact]
|
|||
89d8bebc | Marc Dequènes (Duck) | end
|
|
f7217dcd | Marc Dequènes (Duck) | def fetch_relations_data(rel_list, expert_attributes)
|
|
89d8bebc | Marc Dequènes (Duck) | rel_data = rel_list.collect do |rel|
|
|
3d58e226 | Marc Dequènes (Duck) | data = self.send(rel)
|
|
if data.is_a? Enumerable
|
|||
if data.empty?
|
|||
value = nil
|
|||
else
|
|||
value = data.collect{|g| g.name }
|
|||
multiple = true
|
|||
end
|
|||
else
|
|||
a987d14b | Marc Dequènes (Duck) | # the exists? method also ensure the object is loaded
|
|
if data.exists?
|
|||
3d58e226 | Marc Dequènes (Duck) | value = data.name
|
|
9f623500 | Marc Dequènes (Duck) | else
|
|
value = nil
|
|||
3d58e226 | Marc Dequènes (Duck) | end
|
|
multiple = false
|
|||
end
|
|||
if value.nil?
|
|||
f7217dcd | Marc Dequènes (Duck) | nil
|
|
else
|
|||
3d58e226 | Marc Dequènes (Duck) | rel_key = "rel:" + rel
|
|
[rel_key, {
|
|||
:value => value,
|
|||
:multiple => multiple,
|
|||
:expert => expert_attributes.include?(rel_key),
|
|||
b52f0f7d | Marc Dequènes (Duck) | :admin => false,
|
|
f7217dcd | Marc Dequènes (Duck) | :binary => false
|
|
}]
|
|||
end
|
|||
89d8bebc | Marc Dequènes (Duck) | end
|
|
Hash[rel_data.compact]
|
|||
end
|
|||
end
|
|||
end
|
|||
5a48a521 | Marc Dequènes (Duck) | end
|