| 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: # Workaround for GetText's ugly implementation
26: begin
27: instance_method(:save_without_validation)
28: rescue NameError
29: alias_method_chain :save, :validation
30: alias_method_chain :save!, :validation
31: alias_method_chain :update_attribute, :validation_skipping
32: end
33:
34: validate_on_create :validate_duplicated_dn_creation
35: validate :validate_dn
36: validate :validate_excluded_classes
37: validate :validate_required_ldap_values
38: validate :validate_ldap_values
39:
40: class << self
41: if method_defined?(:evaluate_condition)
42: def evaluate_condition_with_active_ldap_support(condition, entry)
43: evaluate_condition_without_active_ldap_support(condition, entry)
44: rescue ActiveRecord::ActiveRecordError
45: raise Error, $!.message
46: end
47: alias_method_chain :evaluate_condition, :active_ldap_support
48: end
49: end
50:
51: def save_with_active_ldap_support!
52: save_without_active_ldap_support!
53: rescue ActiveRecord::RecordInvalid
54: raise EntryInvalid, $!.message
55: end
56: alias_method_chain :save!, :active_ldap_support
57:
58: private
59: def run_validations_with_active_ldap_support(validation_method)
60: run_validations_without_active_ldap_support(validation_method)
61: rescue ActiveRecord::ActiveRecordError
62: raise Error, $!.message
63: end
64: if private_method_defined?(:run_validations)
65: alias_method_chain :run_validations, :active_ldap_support
66: else
67: alias_method(:run_callbacks_with_active_ldap_support,
68: :run_validations_with_active_ldap_support)
69: alias_method_chain :run_callbacks, :active_ldap_support
70: alias_method(:run_validations_without_active_ldap_support,
71: :run_callbacks_without_active_ldap_support)
72: end
73: end
74: end
# File lib/active_ldap/validations.rb, line 42
42: def evaluate_condition_with_active_ldap_support(condition, entry)
43: evaluate_condition_without_active_ldap_support(condition, entry)
44: rescue ActiveRecord::ActiveRecordError
45: raise Error, $!.message
46: 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 59
59: def run_validations_with_active_ldap_support(validation_method)
60: run_validations_without_active_ldap_support(validation_method)
61: rescue ActiveRecord::ActiveRecordError
62: raise Error, $!.message
63: end
# File lib/active_ldap/validations.rb, line 51
51: def save_with_active_ldap_support!
52: save_without_active_ldap_support!
53: rescue ActiveRecord::RecordInvalid
54: raise EntryInvalid, $!.message
55: end
# File lib/active_ldap/validations.rb, line 93
93: def validate_dn
94: dn
95: rescue DistinguishedNameInvalid
96: format = _("%{fn} is invalid: %s")
97: format = format.sub(/^%\{fn\} /, '') unless ActiveLdap.get_text_supported?
98: errors.add("dn", format % $!.message)
99: rescue DistinguishedNameNotSetError
100: format = _("%{fn} isn't set: %s")
101: format = format.sub(/^%\{fn\} /, '') unless ActiveLdap.get_text_supported?
102: errors.add("dn", format % $!.message)
103: end
# File lib/active_ldap/validations.rb, line 77
77: def validate_duplicated_dn_creation
78: _dn = nil
79: begin
80: _dn = dn
81: rescue DistinguishedNameInvalid, DistinguishedNameNotSetError
82: return
83: end
84: if _dn and exist?
85: format = _("%{fn} is duplicated: %s")
86: unless ActiveLdap.get_text_supported?
87: format = format.sub(/^%\{fn\} /, '')
88: end
89: errors.add("dn", format % _dn)
90: end
91: end
# File lib/active_ldap/validations.rb, line 105
105: def validate_excluded_classes
106: return if self.class.excluded_classes.empty?
107:
108: _schema = schema
109: unexpected_classes = self.class.excluded_classes.collect do |name|
110: _schema.object_class(name)
111: end
112: unexpected_classes -= classes.collect do |name|
113: _schema.object_class(name)
114: end
115: return if unexpected_classes.empty?
116:
117: names = unexpected_classes.collect do |object_class|
118: self.class.human_object_class_name(object_class)
119: end
120: format = n_("%{fn} has excluded value: %s",
121: "%{fn} has excluded values: %s",
122: names.size)
123: format = format.sub(/^%\{fn\} /, '') unless ActiveLdap.get_text_supported?
124: errors.add("objectClass", format % names.join(", "))
125: end
# File lib/active_ldap/validations.rb, line 174
174: def validate_ldap_value(attribute, name, value)
175: failed_reason, option = attribute.validate(value)
176: return if failed_reason.nil?
177: params = [self.class.human_readable_format(value),
178: self.class.human_syntax_description(attribute.syntax),
179: failed_reason]
180: if option
181: format = _("%{fn}(%s) has invalid format: %s: required syntax: %s: %s")
182: else
183: format = _("%{fn} has invalid format: %s: required syntax: %s: %s")
184: end
185: params.unshift(option) if option
186: unless ActiveLdap.get_text_supported?
187: format = format.sub(/^%\{fn\} ?/, '')
188: end
189: errors.add(name, format % params)
190: end
# File lib/active_ldap/validations.rb, line 166
166: def validate_ldap_values
167: entry_attribute.schemata.each do |name, attribute|
168: value = self[name]
169: next if self.class.blank_value?(value)
170: validate_ldap_value(attribute, name, value)
171: end
172: end
Basic validation:
# File lib/active_ldap/validations.rb, line 131
131: def validate_required_ldap_values
132: _schema = nil
133: # Make sure all MUST attributes have a value
134: entry_attribute.object_classes.each do |object_class|
135: object_class.must.each do |required_attribute|
136: # Normalize to ensure we catch schema problems
137: # needed?
138: real_name = to_real_attribute_name(required_attribute.name, true)
139: raise UnknownAttribute.new(required_attribute) if real_name.nil?
140:
141: next if required_attribute.read_only?
142:
143: value = @data[real_name] || []
144: next unless self.class.blank_value?(value)
145:
146: _schema ||= schema
147: aliases = required_attribute.aliases.collect do |name|
148: self.class.human_attribute_name(name)
149: end
150: args = [self.class.human_object_class_name(object_class)]
151: if aliases.empty?
152: format = _("%{fn} is required attribute by objectClass '%s'")
153: else
154: format = _("%{fn} is required attribute by objectClass " \
155: "'%s': aliases: %s")
156: args << aliases.join(', ')
157: end
158: unless ActiveLdap.get_text_supported?
159: format = format.sub(/^%\{fn\} /, '')
160: end
161: errors.add(real_name, format % args)
162: end
163: end
164: end