Revision 7bfcd98b
Added by Marc Dequènes about 13 years ago
- ID 7bfcd98b929705cc5c65b729a60f022827a66bfe
lib/cyborghood-librarian/records.rb | ||
---|---|---|
module CyborgHood
|
||
module LibrarianLand
|
||
class Records
|
||
def persons(obj_name = nil)
|
||
HIDDEN_ATTRIBUTES = ['objectClass', 'userPassword', 'hasSubordinates', 'entryUUID', 'entryDN', 'structuralObjectClass', 'subschemaSubentry']
|
||
|
||
def persons(*args)
|
||
find_records(LdapPerson, 'uid', *args)
|
||
end
|
||
|
||
def dns_domains(*args)
|
||
find_records(LdapDnsDomain, 'cn', *args)
|
||
end
|
||
|
||
protected
|
||
|
||
# obj_name:
|
||
# - nil: return list of objects'names
|
||
# - <value>: return object whose primary attribute is equal to <value>
|
||
# - :all: return all objects
|
||
def find_records(klass, primary_attr, obj_name = nil, options = {})
|
||
if obj_name.nil?
|
||
LdapPerson.find(:all, :attributes => ['uid']).collect{|i| i.uid }
|
||
klass.find(:all, :attributes => [primary_attr]).collect{|i| i.send(primary_attr) }
|
||
elsif obj_name == :search
|
||
filter_parts = []
|
||
options[:criterias].each do |k, v|
|
||
filter_parts << "(#{k}=#{v})"
|
||
end
|
||
filter = (filter_parts.size == 1) ? filter_parts.first : '(&' + filter_parts.join + ')'
|
||
|
||
list = klass.find(:all, :filter => filter)
|
||
index_result(list, primary_attr) if options[:indexed]
|
||
format_result(list, options[:format])
|
||
elsif obj_name == :all
|
||
list = klass.find(:all)
|
||
index_result(list, primary_attr) if options[:indexed]
|
||
format_result(list, options[:format])
|
||
else
|
||
list = LdapPerson.find(:all, :attribute => 'uid', :value => obj_name)
|
||
list = klass.find(:all, :attribute => primary_attr, :value => obj_name)
|
||
return if list.nil? or list.empty?
|
||
return list.first if list.size == 1
|
||
raise CyberError.new(:unrecoverable, "db/ldap", "broken DB: multiple Person with the same uid")
|
||
return format_result(list.first, options[:format]) if list.size == 1
|
||
raise CyberError.new(:unrecoverable, "db/ldap", "broken DB: multiple #{klass} with the same #{primary_attr}")
|
||
end
|
||
end
|
||
|
||
def dns_domains(obj_name = nil)
|
||
if obj_name.nil?
|
||
LdapDnsDomain.find(:all, :attributes => ['cn']).collect{|i| i.cn }
|
||
def format_value(v)
|
||
if v.is_a? Array
|
||
v.collect!{|v2| format_value(v2) }
|
||
elsif v.is_a? ActiveLdap::DistinguishedName
|
||
v.to_s
|
||
else
|
||
list = LdapDnsDomain.find(:all, :attribute => 'cn', :value => obj_name)
|
||
return if list.nil? or list.empty?
|
||
return list.first if list.size == 1
|
||
raise CyberError.new(:unrecoverable, "db/ldap", "broken DB: multiple DnsDomain with the same cn")
|
||
v
|
||
end
|
||
end
|
||
|
||
def dh_format_element(element)
|
||
obj_attrs = {}
|
||
element.attributes.each do |k, v|
|
||
next if HIDDEN_ATTRIBUTES.include? k
|
||
obj_attrs[k.to_sym] = format_value(v)
|
||
end
|
||
obj_attrs
|
||
end
|
||
|
||
def format_result(list, format = nil)
|
||
case format
|
||
when :data_hash
|
||
if list.is_a? Array
|
||
list.collect{|element| dh_format_element(element) }
|
||
elsif list.is_a? Hash
|
||
result = {}
|
||
list.each{|element_name, element| result[element_name] = dh_format_element(element) }
|
||
result
|
||
else
|
||
dh_format_element(list)
|
||
end
|
||
else
|
||
list
|
||
end
|
||
end
|
||
|
||
def index_result(list, primary_attr)
|
||
idx_list = {}
|
||
list.each{|i| idx_list[i.send(primary_attr)] = i }
|
||
idx_list
|
||
end
|
||
end
|
||
end # LibrarianLand
|
||
end
|
Also available in: Unified diff
[evol] Librarian API work §3