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: if ActiveLdap.get_text_supported? 92: if /\A(%\{fn\})/ =~ format 93: place_holder = $1 94: format = $POSTMATCH 95: else 96: place_holder = "" 97: end 98: "#{place_holder}#{format % parameters}" 99: else 100: format.sub(/\A%\{fn\} ?/, '') % parameters 101: end 102: end
# File lib/active_ldap/validations.rb, line 138 138: def validate_dn 139: dn 140: rescue DistinguishedNameInvalid 141: format = _("%{fn} is invalid: %s") 142: message = format_validation_message(format, $!.message) 143: errors.add("distinguishedName", message) 144: rescue DistinguishedNameNotSetError 145: format = _("%{fn} isn't set: %s") 146: message = format_validation_message(format, $!.message) 147: errors.add("distinguishedName", message) 148: end
# File lib/active_ldap/validations.rb, line 104 104: def validate_duplicated_dn_creation 105: _dn = nil 106: begin 107: _dn = dn 108: rescue DistinguishedNameInvalid, DistinguishedNameNotSetError 109: return 110: end 111: if _dn and exist? 112: format = _("%{fn} is duplicated: %s") 113: message = format_validation_message(format, _dn) 114: errors.add("distinguishedName", message) 115: end 116: end
# File lib/active_ldap/validations.rb, line 118 118: def validate_duplicated_dn_rename 119: _dn_attribute = dn_attribute_with_fallback 120: original_dn_value = @ldap_data[_dn_attribute] 121: current_dn_value = @data[_dn_attribute] 122: return if original_dn_value == current_dn_value 123: return if original_dn_value == [current_dn_value] 124: 125: _dn = nil 126: begin 127: _dn = dn 128: rescue DistinguishedNameInvalid, DistinguishedNameNotSetError 129: return 130: end 131: if _dn and exist? 132: format = _("%{fn} is duplicated: %s") 133: message = format_validation_message(format, _dn) 134: errors.add("distinguishedName", message) 135: end 136: end
# File lib/active_ldap/validations.rb, line 150 150: def validate_excluded_classes 151: excluded_classes = self.class.excluded_classes 152: return if excluded_classes.empty? 153: 154: _schema = schema 155: _classes = classes.collect do |name| 156: _schema.object_class(name) 157: end 158: unexpected_classes = excluded_classes.inject([]) do |classes, name| 159: excluded_class = _schema.object_class(name) 160: if _classes.include?(excluded_class) 161: classes << excluded_class 162: end 163: classes 164: end 165: return if unexpected_classes.empty? 166: 167: names = unexpected_classes.collect do |object_class| 168: self.class.human_object_class_name(object_class) 169: end 170: format = n_("%{fn} has excluded value: %s", 171: "%{fn} has excluded values: %s", 172: names.size) 173: message = format_validation_message(format, names.join(", ")) 174: errors.add("objectClass", message) 175: end
# File lib/active_ldap/validations.rb, line 227 227: def validate_ldap_value(attribute, name, value) 228: failed_reason, option = attribute.validate(value) 229: return if failed_reason.nil? 230: if attribute.binary? 231: inspected_value = _("<binary-value>") 232: else 233: inspected_value = self.class.human_readable_format(value) 234: end 235: params = [inspected_value, 236: self.class.human_syntax_description(attribute.syntax), 237: failed_reason] 238: if option 239: format = _("%{fn}(%s) has invalid format: %s: required syntax: %s: %s") 240: else 241: format = _("%{fn} has invalid format: %s: required syntax: %s: %s") 242: end 243: params.unshift(option) if option 244: message = format_validation_message(format, params) 245: errors.add(name, message) 246: end
# File lib/active_ldap/validations.rb, line 219 219: def validate_ldap_values 220: entry_attribute.schemata.each do |name, attribute| 221: value = self[name] 222: next if self.class.blank_value?(value) 223: validate_ldap_value(attribute, name, value) 224: end 225: end
Basic validation:
# File lib/active_ldap/validations.rb, line 181 181: def validate_required_ldap_values 182: _schema = nil 183: @validation_skip_attributes ||= [] 184: _validation_skip_attributes = 185: @validation_skip_attributes + 186: (self.class.validation_skip_attributes || []) 187: # Make sure all MUST attributes have a value 188: entry_attribute.object_classes.each do |object_class| 189: object_class.must.each do |required_attribute| 190: # Normalize to ensure we catch schema problems 191: # needed? 192: real_name = to_real_attribute_name(required_attribute.name, true) 193: raise UnknownAttribute.new(required_attribute) if real_name.nil? 194: 195: next if required_attribute.read_only? 196: next if _validation_skip_attributes.include?(real_name) 197: 198: value = @data[real_name] || [] 199: next unless self.class.blank_value?(value) 200: 201: _schema ||= schema 202: aliases = required_attribute.aliases.collect do |name| 203: self.class.human_attribute_name(name) 204: end 205: args = [self.class.human_object_class_name(object_class)] 206: if aliases.empty? 207: format = _("%{fn} is required attribute by objectClass '%s'") 208: else 209: format = _("%{fn} is required attribute by objectClass " \ 210: "'%s': aliases: %s") 211: args << aliases.join(', ') 212: end 213: message = format_validation_message(format, args) 214: errors.add(real_name, message) 215: end 216: end 217: end