Project

General

Profile

Feature #37279 ยป 37279.patch

Go MAEDA, 2022-07-09 09:14

View differences:

app/models/user.rb
116 116
    validates_format_of :password, :with => v, :message => :"must_contain_#{k}", :allow_blank => true, :if => Proc.new {Setting.password_required_char_classes.include?(k)}
117 117
  end
118 118
  validate :validate_password_length
119
  validate :validate_password_complexity
119 120
  validate do
120 121
    if password_confirmation && password != password_confirmation
121 122
      errors.add(:password, :confirmation)
......
910 911
    end
911 912
  end
912 913

  
914
  def validate_password_complexity
915
    return if password.blank? && generate_password?
916
    return if password.nil?
917

  
918
    bad_passwords = (
919
      [login, firstname, lastname, mail] +
920
      email_addresses.map(&:address)
921
    )
922
    errors.add(:password, :too_simple) if bad_passwords.any? {|p| password.casecmp?(p)}
923
  end
924

  
913 925
  def instantiate_email_address
914 926
    email_address || build_email_address
915 927
  end
config/locales/en.yml
114 114
        blank: "cannot be blank"
115 115
        too_long: "is too long (maximum is %{count} characters)"
116 116
        too_short: "is too short (minimum is %{count} characters)"
117
        too_simple: "is too simple"
117 118
        wrong_length: "is the wrong length (should be %{count} characters)"
118 119
        taken: "has already been taken"
119 120
        not_a_number: "is not a number"
test/unit/user_test.rb
558 558
    end
559 559
  end
560 560

  
561
  def test_validate_password_complexity
562
    user = users(:users_002)
563
    bad_passwords = [
564
      user.login,
565
      user.lastname,
566
      user.firstname,
567
      user.mail,
568
      user.login.upcase
569
    ]
570

  
571
    bad_passwords.each do |p|
572
      user.password, user.password_confirmation = p, p
573
      assert_not user.save
574
      assert user.errors.full_messages.include?('Password is too simple')
575
    end
576
  end
577

  
561 578
  def test_name_format
562 579
    assert_equal 'John S.', @jsmith.name(:firstname_lastinitial)
563 580
    assert_equal 'Smith, John', @jsmith.name(:lastname_comma_firstname)
    (1-1/1)