Project

General

Profile

Feature #37279 ยป 37279.patch

Go MAEDA, 2022-07-09 09:14

View differences:

app/models/user.rb
validates_format_of :password, :with => v, :message => :"must_contain_#{k}", :allow_blank => true, :if => Proc.new {Setting.password_required_char_classes.include?(k)}
end
validate :validate_password_length
validate :validate_password_complexity
validate do
if password_confirmation && password != password_confirmation
errors.add(:password, :confirmation)
......
end
end
def validate_password_complexity
return if password.blank? && generate_password?
return if password.nil?
bad_passwords = (
[login, firstname, lastname, mail] +
email_addresses.map(&:address)
)
errors.add(:password, :too_simple) if bad_passwords.any? {|p| password.casecmp?(p)}
end
def instantiate_email_address
email_address || build_email_address
end
config/locales/en.yml
blank: "cannot be blank"
too_long: "is too long (maximum is %{count} characters)"
too_short: "is too short (minimum is %{count} characters)"
too_simple: "is too simple"
wrong_length: "is the wrong length (should be %{count} characters)"
taken: "has already been taken"
not_a_number: "is not a number"
test/unit/user_test.rb
end
end
def test_validate_password_complexity
user = users(:users_002)
bad_passwords = [
user.login,
user.lastname,
user.firstname,
user.mail,
user.login.upcase
]
bad_passwords.each do |p|
user.password, user.password_confirmation = p, p
assert_not user.save
assert user.errors.full_messages.include?('Password is too simple')
end
end
def test_name_format
assert_equal 'John S.', @jsmith.name(:firstname_lastinitial)
assert_equal 'Smith, John', @jsmith.name(:lastname_comma_firstname)
    (1-1/1)