| Module | ActiveLdap::Validations |
| In: |
lib/active_ldap/validations.rb
|
| human_attribute_name | -> | human_attribute_name_active_ldap |
| human_attribute_name | -> | human_attribute_name_active_record |
| human_attribute_name_active_ldap | -> | human_attribute_name |
# File lib/active_ldap/validations.rb, line 3
3: def self.append_features(base)
4: super
5:
6: base.class_eval do
7: alias_method :new_record?, :new_entry?
8: class << self
9: alias_method :human_attribute_name_active_ldap,
10: :human_attribute_name
11: end
12: include ActiveRecord::Validations
13: class << self
14: alias_method :human_attribute_name_active_record,
15: :human_attribute_name
16: alias_method :human_attribute_name,
17: :human_attribute_name_active_ldap
18: unless method_defined?(:human_attribute_name_with_gettext)
19: def human_attribute_name_with_gettext(attribute_key_name)
20: s_("#{self}|#{attribute_key_name.humanize}")
21: end
22: end
23: end
24:
25: class_local_attr_accessor true, :validation_skip_attributes
26: remove_method :validation_skip_attributes
27: self.validation_skip_attributes = []
28:
29: # Workaround for GetText's ugly implementation
30: begin
31: instance_method(:save_without_validation)
32: rescue NameError
33: alias_method_chain :save, :validation
34: alias_method_chain :save!, :validation
35: alias_method_chain :update_attribute, :validation_skipping
36: end
37:
38: validate_on_create :validate_duplicated_dn_creation
39: validate_on_update :validate_duplicated_dn_rename
40: validate :validate_dn
41: validate :validate_excluded_classes
42: validate :validate_required_ldap_values
43: validate :validate_ldap_values
44:
45: class << self
46: if method_defined?(:evaluate_condition)
47: def evaluate_condition_with_active_ldap_support(condition, entry)
48: evaluate_condition_without_active_ldap_support(condition, entry)
49: rescue ActiveRecord::ActiveRecordError
50: raise Error, $!.message
51: end
52: alias_method_chain :evaluate_condition, :active_ldap_support
53: end
54: end
55:
56: def save_with_active_ldap_support!
57: save_without_active_ldap_support!
58: rescue ActiveRecord::RecordInvalid
59: raise EntryInvalid, $!.message
60: end
61: alias_method_chain :save!, :active_ldap_support
62:
63: private
64: def run_validations_with_active_ldap_support(validation_method)
65: run_validations_without_active_ldap_support(validation_method)
66: rescue ActiveRecord::ActiveRecordError
67: raise Error, $!.message
68: end
69: if private_method_defined?(:run_validations)
70: alias_method_chain :run_validations, :active_ldap_support
71: else
72: alias_method(:run_callbacks_with_active_ldap_support,
73: :run_validations_with_active_ldap_support)
74: alias_method_chain :run_callbacks, :active_ldap_support
75: alias_method(:run_validations_without_active_ldap_support,
76: :run_callbacks_without_active_ldap_support)
77: end
78: end
79: end
# File lib/active_ldap/validations.rb, line 47
47: def evaluate_condition_with_active_ldap_support(condition, entry)
48: evaluate_condition_without_active_ldap_support(condition, entry)
49: rescue ActiveRecord::ActiveRecordError
50: raise Error, $!.message
51: end
# File lib/active_ldap/validations.rb, line 19
19: def human_attribute_name_with_gettext(attribute_key_name)
20: s_("#{self}|#{attribute_key_name.humanize}")
21: end
# File lib/active_ldap/validations.rb, line 64
64: def run_validations_with_active_ldap_support(validation_method)
65: run_validations_without_active_ldap_support(validation_method)
66: rescue ActiveRecord::ActiveRecordError
67: raise Error, $!.message
68: end
# File lib/active_ldap/validations.rb, line 56
56: def save_with_active_ldap_support!
57: save_without_active_ldap_support!
58: rescue ActiveRecord::RecordInvalid
59: raise EntryInvalid, $!.message
60: end
# File lib/active_ldap/validations.rb, line 81
81: def validation_skip_attributes
82: @validation_skip_attributes ||= []
83: end
# File lib/active_ldap/validations.rb, line 85
85: def validation_skip_attributes=(attributes)
86: @validation_skip_attributes = attributes
87: end
# File lib/active_ldap/validations.rb, line 90
90: def format_validation_message(format, parameters)
91: format % parameters
92: end
# File lib/active_ldap/validations.rb, line 128
128: def validate_dn
129: dn
130: rescue DistinguishedNameInvalid
131: format = _("is invalid: %s")
132: message = format_validation_message(format, $!.message)
133: errors.add("distinguishedName", message)
134: rescue DistinguishedNameNotSetError
135: format = _("isn't set: %s")
136: message = format_validation_message(format, $!.message)
137: errors.add("distinguishedName", message)
138: end
# File lib/active_ldap/validations.rb, line 94
94: def validate_duplicated_dn_creation
95: _dn = nil
96: begin
97: _dn = dn
98: rescue DistinguishedNameInvalid, DistinguishedNameNotSetError
99: return
100: end
101: if _dn and exist?
102: format = _("is duplicated: %s")
103: message = format_validation_message(format, _dn)
104: errors.add("distinguishedName", message)
105: end
106: end
# File lib/active_ldap/validations.rb, line 108
108: def validate_duplicated_dn_rename
109: _dn_attribute = dn_attribute_with_fallback
110: original_dn_value = @ldap_data[_dn_attribute]
111: current_dn_value = @data[_dn_attribute]
112: return if original_dn_value == current_dn_value
113: return if original_dn_value == [current_dn_value]
114:
115: _dn = nil
116: begin
117: _dn = dn
118: rescue DistinguishedNameInvalid, DistinguishedNameNotSetError
119: return
120: end
121: if _dn and exist?
122: format = _("is duplicated: %s")
123: message = format_validation_message(format, _dn)
124: errors.add("distinguishedName", message)
125: end
126: end
# File lib/active_ldap/validations.rb, line 140
140: def validate_excluded_classes
141: excluded_classes = self.class.excluded_classes
142: return if excluded_classes.empty?
143:
144: _schema = schema
145: _classes = classes.collect do |name|
146: _schema.object_class(name)
147: end
148: unexpected_classes = excluded_classes.inject([]) do |classes, name|
149: excluded_class = _schema.object_class(name)
150: if _classes.include?(excluded_class)
151: classes << excluded_class
152: end
153: classes
154: end
155: return if unexpected_classes.empty?
156:
157: names = unexpected_classes.collect do |object_class|
158: self.class.human_object_class_name(object_class)
159: end
160: format = n_("has excluded value: %s",
161: "has excluded values: %s",
162: names.size)
163: message = format_validation_message(format, names.join(", "))
164: errors.add("objectClass", message)
165: end
# File lib/active_ldap/validations.rb, line 217
217: def validate_ldap_value(attribute, name, value)
218: failed_reason, option = attribute.validate(value)
219: return if failed_reason.nil?
220: if attribute.binary?
221: inspected_value = _("<binary-value>")
222: else
223: inspected_value = self.class.human_readable_format(value)
224: end
225: params = [inspected_value,
226: self.class.human_syntax_description(attribute.syntax),
227: failed_reason]
228: if option
229: format = _("(%s) has invalid format: %s: required syntax: %s: %s")
230: else
231: format = _("has invalid format: %s: required syntax: %s: %s")
232: end
233: params.unshift(option) if option
234: message = format_validation_message(format, params)
235: errors.add(name, message)
236: end
# File lib/active_ldap/validations.rb, line 209
209: def validate_ldap_values
210: entry_attribute.schemata.each do |name, attribute|
211: value = self[name]
212: next if self.class.blank_value?(value)
213: validate_ldap_value(attribute, name, value)
214: end
215: end
Basic validation:
# File lib/active_ldap/validations.rb, line 171
171: def validate_required_ldap_values
172: _schema = nil
173: @validation_skip_attributes ||= []
174: _validation_skip_attributes =
175: @validation_skip_attributes +
176: (self.class.validation_skip_attributes || [])
177: # Make sure all MUST attributes have a value
178: entry_attribute.object_classes.each do |object_class|
179: object_class.must.each do |required_attribute|
180: # Normalize to ensure we catch schema problems
181: # needed?
182: real_name = to_real_attribute_name(required_attribute.name, true)
183: raise UnknownAttribute.new(required_attribute) if real_name.nil?
184:
185: next if required_attribute.read_only?
186: next if _validation_skip_attributes.include?(real_name)
187:
188: value = @data[real_name] || []
189: next unless self.class.blank_value?(value)
190:
191: _schema ||= schema
192: aliases = required_attribute.aliases.collect do |name|
193: self.class.human_attribute_name(name)
194: end
195: args = [self.class.human_object_class_name(object_class)]
196: if aliases.empty?
197: format = _("is required attribute by objectClass '%s'")
198: else
199: format = _("is required attribute by objectClass " \
200: "'%s': aliases: %s")
201: args << aliases.join(', ')
202: end
203: message = format_validation_message(format, args)
204: errors.add(real_name, message)
205: end
206: end
207: end