| Module | ActiveLdap::Attributes::ClassMethods |
| In: |
lib/active_ldap/attributes.rb
|
# File lib/active_ldap/attributes.rb, line 8
8: def attr_protected(*attributes)
9: targets = attributes.collect {|attr| attr.to_s} - protected_attributes
10: instance_variable_set("@attr_protected", targets)
11: end
Enforce typing: Hashes are for subtypes Arrays are for multiple entries
# File lib/active_ldap/attributes.rb, line 26
26: def normalize_attribute(name, value)
27: logger.debug {"stub: called normalize_attribute" +
28: "(#{name.inspect}, #{value.inspect})"}
29: if name.nil?
30: raise RuntimeError, 'The first argument, name, must not be nil. ' +
31: 'Please report this as a bug!'
32: end
33:
34: name = normalize_attribute_name(name)
35: rubyish_class_name = Inflector.underscore(value.class.name)
36: handler = "normalize_attribute_value_of_#{rubyish_class_name}"
37: if respond_to?(handler, true)
38: [name, send(handler, name, value)]
39: else
40: [name, [value.to_s]]
41: end
42: end
# File lib/active_ldap/attributes.rb, line 19
19: def normalize_attribute_name(name)
20: name.to_s.downcase
21: end
# File lib/active_ldap/attributes.rb, line 13
13: def protected_attributes
14: ancestors[0..(ancestors.index(Base))].inject([]) do |result, ancestor|
15: result + ancestor.instance_eval {@attr_protected ||= []}
16: end
17: end
# File lib/active_ldap/attributes.rb, line 52
52: def unnormalize_attribute(name, values, result={})
53: if values.empty?
54: result[name] = []
55: else
56: values.each do |value|
57: if value.is_a?(Hash)
58: suffix, real_value = extract_subtypes(value)
59: new_name = name + suffix
60: result[new_name] ||= []
61: result[new_name].concat(real_value)
62: else
63: result[name] ||= []
64: result[name] << value.dup
65: end
66: end
67: end
68: result
69: end
# File lib/active_ldap/attributes.rb, line 44
44: def unnormalize_attributes(attributes)
45: result = {}
46: attributes.each do |name, values|
47: unnormalize_attribute(name, values, result)
48: end
49: result
50: end
Extracts all of the subtypes from a given set of nested hashes and returns the attribute suffix and the final true value
# File lib/active_ldap/attributes.rb, line 162
162: def extract_subtypes(value)
163: logger.debug {"stub: called extract_subtypes(#{value.inspect})"}
164: subtype = ''
165: ret_val = value
166: if value.class == Hash
167: subtype = ';' + value.keys[0]
168: ret_val = value[value.keys[0]]
169: subsubtype = ''
170: if ret_val.class == Hash
171: subsubtype, ret_val = extract_subtypes(ret_val)
172: end
173: subtype += subsubtype
174: end
175: ret_val = [ret_val] unless ret_val.class == Array
176: return subtype, ret_val
177: end
Makes the Hashized value from the full attributename e.g. userCertificate;binary => "some_bin"
becomes userCertificate => {"binary" => "some_bin"}
# File lib/active_ldap/attributes.rb, line 138
138: def make_subtypes(attr, value)
139: logger.debug {"stub: called make_subtypes(#{attr.inspect}, " +
140: "#{value.inspect})"}
141: return [attr, value] unless attr.match(/;/)
142:
143: ret_attr, *subtypes = attr.split(/;/)
144: return [ret_attr, [make_subtypes_helper(subtypes, value)]]
145: end
This is a recursive function for building nested hashed from multi-subtyped values
# File lib/active_ldap/attributes.rb, line 151
151: def make_subtypes_helper(subtypes, value)
152: logger.debug {"stub: called make_subtypes_helper" +
153: "(#{subtypes.inspect}, #{value.inspect})"}
154: return value if subtypes.size == 0
155: return {subtypes[0] => make_subtypes_helper(subtypes[1..-1], value)}
156: end
# File lib/active_ldap/attributes.rb, line 72
72: def normalize_attribute_value_of_array(name, value)
73: if value.size > 1 and schema.single_value?(name)
74: raise TypeError, "Attribute #{name} can only have a single value"
75: end
76: if value.empty?
77: schema.binary_required?(name) ? [{'binary' => value}] : value
78: else
79: value.collect do |entry|
80: normalize_attribute(name, entry)[1][0]
81: end
82: end
83: end
# File lib/active_ldap/attributes.rb, line 111
111: def normalize_attribute_value_of_date(name, value)
112: new_value = sprintf('%.04d%.02d%.02d%.02d%.02d%.02d%s',
113: value.year, value.month, value.mday, 0, 0, 0,
114: '+0000')
115: normalize_attribute_value_of_string(name, new_value)
116: end
# File lib/active_ldap/attributes.rb, line 125
125: def normalize_attribute_value_of_date_time(name, value)
126: new_value = sprintf('%.04d%.02d%.02d%.02d%.02d%.02d%s',
127: value.year, value.month, value.mday, value.hour,
128: value.min, value.sec, value.zone)
129: normalize_attribute_value_of_string(name, new_value)
130: end
# File lib/active_ldap/attributes.rb, line 85
85: def normalize_attribute_value_of_hash(name, value)
86: if value.keys.size > 1
87: raise TypeError, "Hashes must have one key-value pair only."
88: end
89: unless value.keys[0].match(/^(lang-[a-z][a-z]*)|(binary)$/)
90: logger.warn {"unknown subtype did not match lang-* or binary:" +
91: "#{value.keys[0]}"}
92: end
93: # Contents MUST be a String or an Array
94: if !value.has_key?('binary') and schema.binary_required?(name)
95: suffix, real_value = extract_subtypes(value)
96: name, values = make_subtypes(name + suffix + ';binary', real_value)
97: values
98: else
99: [value]
100: end
101: end
# File lib/active_ldap/attributes.rb, line 103
103: def normalize_attribute_value_of_nil_class(name, value)
104: schema.binary_required?(name) ? [{'binary' => []}] : []
105: end
# File lib/active_ldap/attributes.rb, line 107
107: def normalize_attribute_value_of_string(name, value)
108: [schema.binary_required?(name) ? {'binary' => [value]} : value]
109: end