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)
|
2016-03-02 03:31:38 -05:00
|
|
|
elsif value.length < SiteSetting.min_admin_password_length && record.admin?
|
|
|
|
record.errors.add(attribute, :too_short, count: SiteSetting.min_admin_password_length)
|
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)
|
2016-01-05 15:43:11 -05:00
|
|
|
elsif record.email.present? && value == record.email
|
2015-02-27 13:47:43 -05:00
|
|
|
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
|