1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
require "active_ldap/attributes"
module ActiveLdap
class EntryAttribute
include Attributes::Normalizable
attr_reader :must, :may, :object_classes, :schemata
def initialize(schema, object_classes)
@schemata = {}
@names = {}
@normalized_names = {}
@aliases = {}
@must = []
@may = []
@object_classes = []
register(schema.attribute('objectClass')) if schema
object_classes.each do |objc|
# get all attributes for the class
object_class = schema.object_class(objc)
@object_classes << object_class
@must.concat(object_class.must)
@may.concat(object_class.may)
end
@must.uniq!
@may.uniq!
(@must + @may).each do |attr|
# Update attr_method with appropriate
register(attr)
end
end
def names(normalize=false)
names = @names.keys
if normalize
names.collect do |name|
normalize(name)
end.uniq
else
names
end
end
def normalize(name, allow_normalized_name=false)
return name if name.nil?
return nil if @names.empty? and @aliases.empty?
name = name.to_s
real_name = @names[name]
real_name ||= @aliases[name.underscore]
if real_name
real_name
elsif allow_normalized_name
return nil if @normalized_names.empty?
@normalized_names[normalize_attribute_name(name)]
else
nil
end
end
def exist?(name)
not normalize(name).nil?
end
def all_names
@names.keys + @aliases.keys
end
# register
#
# Make a method entry for _every_ alias of a valid attribute and map it
# onto the first attribute passed in.
def register(attribute)
real_name = attribute.name
return if @schemata.has_key?(real_name)
@schemata[real_name] = attribute
([real_name] + attribute.aliases).each do |name|
@names[name] = real_name
@aliases[name.underscore] = real_name
@normalized_names[normalize_attribute_name(name)] = real_name
end
end
end
end
|