2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-12-19 14:12:03 -06:00
|
|
|
class PasswordValidator < ActiveModel::EachValidator
|
|
|
|
def validate_each(record, attribute, value)
|
2017-11-30 22:19:24 -06:00
|
|
|
return unless record.password_validation_required?
|
|
|
|
|
2013-12-19 14:12:03 -06:00
|
|
|
if value.nil?
|
|
|
|
record.errors.add(attribute, :blank)
|
2016-03-03 12:01:31 -06:00
|
|
|
elsif value.length < SiteSetting.min_admin_password_length &&
|
|
|
|
(record.admin? || is_developer?(record.email))
|
2016-03-02 02:31:38 -06:00
|
|
|
record.errors.add(attribute, :too_short, count: SiteSetting.min_admin_password_length)
|
2013-12-19 15:15:36 -06:00
|
|
|
elsif value.length < SiteSetting.min_password_length
|
|
|
|
record.errors.add(attribute, :too_short, count: SiteSetting.min_password_length)
|
2015-02-25 10:59:57 -06:00
|
|
|
elsif record.username.present? && value == record.username
|
|
|
|
record.errors.add(attribute, :same_as_username)
|
2019-05-13 15:43:19 -05:00
|
|
|
elsif record.name.present? && value == record.name
|
|
|
|
record.errors.add(attribute, :same_as_name)
|
2016-01-05 14:43:11 -06:00
|
|
|
elsif record.email.present? && value == record.email
|
2015-02-27 12:47:43 -06:00
|
|
|
record.errors.add(attribute, :same_as_email)
|
2016-08-24 11:27:09 -05:00
|
|
|
elsif record.confirm_password?(value)
|
|
|
|
record.errors.add(attribute, :same_as_current)
|
2013-12-20 15:34:34 -06:00
|
|
|
elsif SiteSetting.block_common_passwords && CommonPasswords.common_password?(value)
|
|
|
|
record.errors.add(attribute, :common)
|
2017-02-14 08:40:15 -06:00
|
|
|
elsif value.chars.uniq.length < SiteSetting.password_unique_characters
|
2017-02-09 14:00:22 -06:00
|
|
|
record.errors.add(attribute, :unique_characters)
|
2013-12-19 14:12:03 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-03 12:01:31 -06:00
|
|
|
def is_developer?(value)
|
|
|
|
Rails.configuration.respond_to?(:developer_emails) &&
|
|
|
|
Rails.configuration.developer_emails.include?(value)
|
|
|
|
end
|
2013-12-19 14:12:03 -06:00
|
|
|
end
|