2013-12-20 16:34:34 -05:00
|
|
|
require_dependency "common_passwords/common_passwords"
|
|
|
|
|
2013-12-19 15:12:03 -05:00
|
|
|
class PasswordValidator < ActiveModel::EachValidator
|
|
|
|
|
|
|
|
def validate_each(record, attribute, value)
|
|
|
|
return unless record.password_required?
|
|
|
|
if value.nil?
|
|
|
|
record.errors.add(attribute, :blank)
|
2013-12-19 16:15:36 -05:00
|
|
|
elsif value.length < SiteSetting.min_password_length
|
|
|
|
record.errors.add(attribute, :too_short, count: SiteSetting.min_password_length)
|
2015-02-25 11:59:57 -05:00
|
|
|
elsif record.username.present? && value == record.username
|
|
|
|
record.errors.add(attribute, :same_as_username)
|
2015-02-27 13:47:43 -05:00
|
|
|
elsif record.username.present? && value == record.email
|
|
|
|
record.errors.add(attribute, :same_as_email)
|
2013-12-20 16:34:34 -05:00
|
|
|
elsif SiteSetting.block_common_passwords && CommonPasswords.common_password?(value)
|
|
|
|
record.errors.add(attribute, :common)
|
2013-12-19 15:12:03 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|