|
#--
|
|
# CyborgHood, a distributed system management software.
|
|
# Copyright (c) 2009-2011 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/>.
|
|
#++
|
|
|
|
Dir.glob(File.join(File.dirname(__FILE__), "records", "*.rb")).each do |f|
|
|
logger.debug "Loading record definition '#{f}'"
|
|
require f
|
|
end
|
|
|
|
|
|
module CyborgHood
|
|
module LibrarianLand
|
|
class Records
|
|
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?
|
|
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)
|
|
list = index_result(list, primary_attr)
|
|
format_result(list, options[:format])
|
|
elsif obj_name == :all
|
|
list = klass.find(:all)
|
|
list = index_result(list, primary_attr)
|
|
format_result(list, options[:format])
|
|
else
|
|
list = klass.find(:all, :attribute => primary_attr, :value => obj_name)
|
|
return if list.nil? or list.empty?
|
|
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 format_value(v)
|
|
if v.is_a? Array
|
|
v.collect!{|v2| format_value(v2) }
|
|
elsif v.is_a? ActiveLdap::DistinguishedName
|
|
v.to_s
|
|
else
|
|
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
|
|
|
|
# temporary until LdapShadows is used
|
|
obj_attrs[:dn] = element.dn.to_s
|
|
|
|
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
|