2013-12-21 05:34:34 +08:00
|
|
|
require_dependency "common_passwords/common_passwords"
|
|
|
|
|
2013-12-20 04:12:03 +08: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-04-01 05:33:25 +08:00
|
|
|
elsif value.length < SiteSetting.min_admin_password_length && (record.admin? || is_developer?(record.email))
|
|
|
|
record.errors.add(attribute, :too_short, count: SiteSetting.min_admin_password_length)
|
2013-12-20 05:15:36 +08:00
|
|
|
elsif value.length < SiteSetting.min_password_length
|
|
|
|
record.errors.add(attribute, :too_short, count: SiteSetting.min_password_length)
|
2015-02-26 00:59:57 +08:00
|
|
|
elsif record.username.present? && value == record.username
|
|
|
|
record.errors.add(attribute, :same_as_username)
|
2016-04-01 05:33:25 +08:00
|
|
|
elsif record.email.present? && value == record.email
|
2015-02-28 02:47:43 +08:00
|
|
|
record.errors.add(attribute, :same_as_email)
|
2013-12-21 05:34:34 +08:00
|
|
|
elsif SiteSetting.block_common_passwords && CommonPasswords.common_password?(value)
|
|
|
|
record.errors.add(attribute, :common)
|
2013-12-20 04:12:03 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
def is_developer?(value)
|
|
|
|
Rails.configuration.respond_to?(:developer_emails) && Rails.configuration.developer_emails.include?(value)
|
|
|
|
end
|
|
|
|
|
2013-12-20 04:12:03 +08:00
|
|
|
end
|