From ed51fc7636e0deec4f260b125bc7d08e06edd5fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 30 Mar 2011 15:35:38 +0200 Subject: [PATCH] Tidy up previous commits. --- CHANGELOG.rdoc | 1 + lib/devise.rb | 2 +- lib/devise/models/recoverable.rb | 16 ++++++++++------ lib/devise/schema.rb | 10 +++++++--- lib/generators/templates/devise.rb | 10 +++++----- test/rails_app/config/initializers/devise.rb | 5 +++++ 6 files changed, 29 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 88452cd7..a5190345 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,5 +1,6 @@ * enhancements * All controllers can now handle different mime types than html using Responders (by github.com/sikachu) + * Added reset_password_within as configuration option to send the token for recovery (by github.com/jdguyot) * bug fix * Fix a bug where configuration options were being included too late diff --git a/lib/devise.rb b/lib/devise.rb index d01437f9..36593c82 100644 --- a/lib/devise.rb +++ b/lib/devise.rb @@ -173,7 +173,7 @@ module Devise # Time interval you can reset your password with a reset password key mattr_accessor :reset_password_within - @@reset_password_within = 1.hour + @@reset_password_within = nil # The default scope which is used by warden. mattr_accessor :default_scope diff --git a/lib/devise/models/recoverable.rb b/lib/devise/models/recoverable.rb index d6d1214e..7879a1dd 100644 --- a/lib/devise/models/recoverable.rb +++ b/lib/devise/models/recoverable.rb @@ -35,7 +35,7 @@ module Devise # Resets reset password token and send reset password instructions by email def send_reset_password_instructions - generate_reset_password_token! if self.reset_password_token.nil? or !reset_password_period_valid? + generate_reset_password_token! if should_generate_token? ::Devise.mailer.reset_password_instructions(self).deliver end @@ -59,15 +59,20 @@ module Devise # reset_password_period_valid? # will always return false # def reset_password_period_valid? - reset_password_sent_at && reset_password_sent_at.utc >= self.class.reset_password_within.ago + respond_to?(:reset_password_sent_at) && reset_password_sent_at && + reset_password_sent_at.utc >= self.class.reset_password_within.ago end protected + def should_generate_token? + reset_password_token.nil? || !reset_password_period_valid? + end + # Generates a new random token for reset password def generate_reset_password_token self.reset_password_token = self.class.reset_password_token - self.reset_password_sent_at = Time.now.utc + self.reset_password_sent_at = Time.now.utc if respond_to?(:reset_password_sent_at=) end # Resets the reset password token with and save the record without @@ -79,7 +84,7 @@ module Devise # Removes reset_password token def clear_reset_password_token self.reset_password_token = nil - self.reset_password_sent_at = nil + self.reset_password_sent_at = nil if respond_to?(:reset_password_sent_at=) end module ClassMethods @@ -115,8 +120,7 @@ module Devise recoverable end - Devise::Models.config(self, :reset_password_keys) - Devise::Models.config(self, :reset_password_within) + Devise::Models.config(self, :reset_password_keys, :reset_password_within) end end end diff --git a/lib/devise/schema.rb b/lib/devise/schema.rb index 80536385..972a60c9 100644 --- a/lib/devise/schema.rb +++ b/lib/devise/schema.rb @@ -15,7 +15,7 @@ module Devise def database_authenticatable(options={}) null = options[:null] || false default = options.key?(:default) ? options[:default] : ("" if null == false) - include_email = !self.respond_to?(:authentication_keys) || self.authentication_keys.include?(:email) + include_email = !respond_to?(:authentication_keys) || self.authentication_keys.include?(:email) apply_devise_schema :email, String, :null => null, :default => default if include_email apply_devise_schema :encrypted_password, String, :null => null, :default => default, :limit => 128 @@ -39,9 +39,13 @@ module Devise end # Creates reset_password_token and reset_password_sent_at. - def recoverable + # + # == Options + # * :reset_within - When true, adds a column that reset passwords within some date + def recoverable(options={}) + use_within = options.fetch(:reset_within, Devise.reset_password_within.present?) apply_devise_schema :reset_password_token, String - apply_devise_schema :reset_password_sent_at, DateTime + apply_devise_schema :reset_password_sent_at, DateTime if use_within end # Creates remember_token and remember_created_at. diff --git a/lib/generators/templates/devise.rb b/lib/generators/templates/devise.rb index f44cafa5..eb843785 100644 --- a/lib/generators/templates/devise.rb +++ b/lib/generators/templates/devise.rb @@ -125,11 +125,11 @@ Devise.setup do |config| # # Defines which key will be used when recovering the password for an account # config.reset_password_keys = [ :email ] - # - # Time interval you can reset your password with a reset password key - # Don't put a too small interval or your users won't have the time to change their passwords - # Default to 1 hour - config.reset_password_within = 1.hour + + # Time interval you can reset your password with a reset password key. + # Don't put a too small interval or your users won't have the time to + # change their passwords. + config.reset_password_within = 2.hours # ==> Configuration for :encryptable # Allow you to use another encryption algorithm besides bcrypt (default). You can use diff --git a/test/rails_app/config/initializers/devise.rb b/test/rails_app/config/initializers/devise.rb index 004d17bb..447c24c8 100644 --- a/test/rails_app/config/initializers/devise.rb +++ b/test/rails_app/config/initializers/devise.rb @@ -119,6 +119,11 @@ Devise.setup do |config| # Defines which key will be used when recovering the password for an account # config.reset_password_keys = [ :email ] + # Time interval you can reset your password with a reset password key. + # Don't put a too small interval or your users won't have the time to + # change their passwords. + config.reset_password_within = 2.hours + # ==> Configuration for :encryptable # Allow you to use another encryption algorithm besides bcrypt (default). You can use # :sha1, :sha512 or encryptors from others authentication tools as :clearance_sha1,