Class: PasswordValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/password_validator.rb

Overview

Class is used to verify that the user’s password is strong enough

Constant Summary collapse

MINIMUM_LENGTH =
10
MAX_LENGTH =
256
MIN_UNIQUE_CHARACTERS =
5
IGNORE_SIMILARITY_SHORTER_THAN =
4
VALIDATION_METHODS =
[
  :password_too_short?,
  :password_too_long?,
  :not_enough_unique_characters?,
  :name_included_in_password?,
  :nickname_included_in_password?,
  :email_included_in_password?,
  :domain_included_in_password?,
  :password_too_common?,
  :blacklisted?
].freeze

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object

Check if user’s password is strong enough

record - Instance of a form (e.g. Decidim::RegistrationForm) or model attribute - “password” value - Actual password Returns true if password is strong enough



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/validators/password_validator.rb', line 27

def validate_each(record, attribute, value)
  return false if value.blank?

  @record = record
  @attribute = attribute
  @value = value
  @weak_password_reasons = []

  return true if strong?

  @weak_password_reasons.each do |reason|
    record.errors[attribute] << get_message(reason)
  end

  false
end