From aefee603fcfac04f494235e9e57b809eb3b85c0d Mon Sep 17 00:00:00 2001 From: Brian Thompson Date: Thu, 13 Jun 2013 08:22:40 -0400 Subject: [PATCH] added support for more detailed error message when the password does not meet the requirements of the regex specified. --- lib/devise_security_extension.rb | 4 ++++ lib/devise_security_extension/models/secure_validatable.rb | 5 +++-- .../devise_security_extension/install_generator.rb | 2 ++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/devise_security_extension.rb b/lib/devise_security_extension.rb index a902ed35..c3746c9b 100644 --- a/lib/devise_security_extension.rb +++ b/lib/devise_security_extension.rb @@ -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 diff --git a/lib/devise_security_extension/models/secure_validatable.rb b/lib/devise_security_extension/models/secure_validatable.rb index 0c1dec2f..86bd9165 100644 --- a/lib/devise_security_extension/models/secure_validatable.rb +++ b/lib/devise_security_extension/models/secure_validatable.rb @@ -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 @@ -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 diff --git a/lib/generators/devise_security_extension/install_generator.rb b/lib/generators/devise_security_extension/install_generator.rb index 9fd43964..04ef6c5b 100644 --- a/lib/generators/devise_security_extension/install_generator.rb +++ b/lib/generators/devise_security_extension/install_generator.rb @@ -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" +