From 81926c2cd2b1b41a79c0bbf444562ea3d4db70bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 30 Mar 2010 00:07:11 +0200 Subject: [PATCH] Allow :unlock_strategy to be :none. --- CHANGELOG.rdoc | 1 + lib/devise/models/lockable.rb | 5 ++--- lib/devise/schema.rb | 36 ++++++++++++++++++++++------------- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 2bd4d5cf..9d69ded2 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -14,6 +14,7 @@ * E-mails asks headers_for in the model to set the proper headers. * Allow to specify haml in devise_views. * Compatibility with Datamapper and Mongoid. + * Allow :unlock_strategy to be :none. * bug fix * Do not allow unlockable strategies based on time to access a controller. diff --git a/lib/devise/models/lockable.rb b/lib/devise/models/lockable.rb index 5da4fbfc..bf47cf5d 100644 --- a/lib/devise/models/lockable.rb +++ b/lib/devise/models/lockable.rb @@ -13,7 +13,7 @@ module Devise # Configuration: # # maximum_attempts: how many attempts should be accepted before blocking the user. - # unlock_strategy: unlock the user account by :time, :email or :both. + # unlock_strategy: unlock the user account by :time, :email, :both or :none. # unlock_in: the time you want to lock the user after to lock happens. Only # available when unlock_strategy is :time or :both. # @@ -34,12 +34,11 @@ module Devise end # Unlock an user by cleaning locket_at and failed_attempts. - # TODO Check if unlock_token is available. def unlock_access! if_access_locked do self.locked_at = nil self.failed_attempts = 0 - self.unlock_token = nil + self.unlock_token = nil if self.respond_to?(:unlock_token=) save(:validate => false) end end diff --git a/lib/devise/schema.rb b/lib/devise/schema.rb index 0bec53b2..8b8f138e 100644 --- a/lib/devise/schema.rb +++ b/lib/devise/schema.rb @@ -12,37 +12,40 @@ module Devise # # == Options # * :null - When true, allow columns to be null. - # * :encryptor - The encryptor going to be used, necessary for setting the proper encrypter password length. + # * :default - Should be set to "" when :null is true. def database_authenticatable(options={}) - null = options[:null] || false - default = options[:default] - encryptor = options[:encryptor] || (respond_to?(:encryptor) ? self.encryptor : :sha1) + null = options[:null] || false + default = options[:default] + + if options.delete(:encryptor) + ActiveSupport::Deprecation.warn ":encryptor as option is deprecated, simply remove it." + end apply_schema :email, String, :null => null, :default => default - apply_schema :encrypted_password, String, :null => null, :default => default, :limit => Devise::ENCRYPTORS_LENGTH[encryptor] + apply_schema :encrypted_password, String, :null => null, :default => default apply_schema :password_salt, String, :null => null, :default => default end # Creates authentication_token. - def token_authenticatable - apply_schema :authentication_token, String, :limit => 20 + def token_authenticatable(options={}) + apply_schema :authentication_token, String end # Creates confirmation_token, confirmed_at and confirmation_sent_at. def confirmable - apply_schema :confirmation_token, String, :limit => 20 + apply_schema :confirmation_token, String apply_schema :confirmed_at, DateTime apply_schema :confirmation_sent_at, DateTime end # Creates reset_password_token. def recoverable - apply_schema :reset_password_token, String, :limit => 20 + apply_schema :reset_password_token, String end # Creates remember_token and remember_created_at. def rememberable - apply_schema :remember_token, String, :limit => 20 + apply_schema :remember_token, String apply_schema :remember_created_at, DateTime end @@ -56,10 +59,17 @@ module Devise apply_schema :last_sign_in_ip, String end - # Creates failed_attempts, unlock_token and locked_at - def lockable + # Creates failed_attempts, unlock_token and locked_at. + # + # == Options + # * :unlock_strategy - The strategy used for unlock. Can be :time, :email, :both, :none. + # If :email or :both, creates a unlock_token field. + def lockable(options={}) + unlock_strategy = options[:unlock_strategy] || + (respond_to?(:unlock_strategy) ? self.unlock_strategy : :both) + apply_schema :failed_attempts, Integer, :default => 0 - apply_schema :unlock_token, String, :limit => 20 + apply_schema :unlock_token, String if [:both, :email].include?(unlock_strategy) apply_schema :locked_at, DateTime end