Skip to content
This repository has been archived by the owner on Jan 30, 2024. It is now read-only.

Support custom error message for password failure #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/devise_security_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ module Devise
mattr_accessor :password_regex
@@password_regex = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z])/

# The error message to display if password validation fails on the regex
mattr_accessor :password_error_message
@@password_error_message = ' is invalid.'

# How often save old passwords in archive
mattr_accessor :password_archiving_count
@@password_archiving_count = 5
Expand Down
5 changes: 3 additions & 2 deletions lib/devise_security_extension/models/secure_validatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def self.included(base)
validates :email, :email => email_validation if email_validation # use rails_email_validator or similar

# validates password
validates :password, :presence => true, :length => password_length, :format => password_regex, :confirmation => true, :if => :password_required?
validates :password, :presence => true, :length => password_length, :confirmation => true, :if => :password_required?
validates :password, :format => {:with => password_regex, :message => password_error_message}, :if => :password_required?

# don't allow use same password
validate :current_equal_password_validation
Expand Down Expand Up @@ -61,7 +62,7 @@ def email_required?
end

module ClassMethods
Devise::Models.config(self, :password_regex, :password_length, :email_validation)
Devise::Models.config(self, :password_regex, :password_error_message, :password_length, :email_validation)
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/generators/devise_security_extension/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def add_configs
" # config.expire_password_after = false\n\n" +
" # Need 1 char of A-Z, a-z and 0-9\n" +
" # config.password_regex = /(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])/\n\n" +
" # Message to display if the password fails validation against the regex\n" +
" # config.password_error_message = ' must contain at least 1 each of the following: uppercase, lowercase, numeric and symbol.'\n\n" +
" # How many passwords to keep in archive\n" +
" # config.password_archiving_count = 5\n\n" +
" # Deny old password (true, false, count)\n" +
Expand Down