2009-11-13 19:33:00 -05:00
|
|
|
module Devise
|
|
|
|
# Holds devise schema information. To use it, just include its methods
|
|
|
|
# and overwrite the apply_schema method.
|
|
|
|
module Schema
|
|
|
|
|
2011-02-09 22:51:19 -05:00
|
|
|
# Creates email when enabled (on by default), encrypted_password and password_salt.
|
2009-11-13 19:33:00 -05:00
|
|
|
#
|
|
|
|
# == Options
|
2009-11-15 17:35:19 -05:00
|
|
|
# * :null - When true, allow columns to be null.
|
2010-03-31 15:43:19 -04:00
|
|
|
# * :default - Should be set to "" when :null is false.
|
2010-06-18 16:00:31 -04:00
|
|
|
#
|
|
|
|
# == Notes
|
|
|
|
# For Datamapper compatibility, we explicitly hardcode the limit for the
|
|
|
|
# encrypter password field in 128 characters.
|
2010-03-29 10:13:19 -04:00
|
|
|
def database_authenticatable(options={})
|
2010-03-29 18:07:11 -04:00
|
|
|
null = options[:null] || false
|
2010-08-26 07:30:12 -04:00
|
|
|
default = options.key?(:default) ? options[:default] : ("" if null == false)
|
2011-03-30 09:35:38 -04:00
|
|
|
include_email = !respond_to?(:authentication_keys) || self.authentication_keys.include?(:email)
|
2010-03-29 18:07:11 -04:00
|
|
|
|
2011-02-09 22:51:19 -05:00
|
|
|
apply_devise_schema :email, String, :null => null, :default => default if include_email
|
2010-06-18 16:00:31 -04:00
|
|
|
apply_devise_schema :encrypted_password, String, :null => null, :default => default, :limit => 128
|
2010-09-26 13:08:25 -04:00
|
|
|
end
|
2009-11-13 19:33:00 -05:00
|
|
|
|
2010-09-25 10:08:46 -04:00
|
|
|
# Creates password salt for encryption support.
|
|
|
|
def encryptable
|
|
|
|
apply_devise_schema :password_salt, String
|
|
|
|
end
|
|
|
|
|
2010-01-23 21:38:52 -05:00
|
|
|
# Creates authentication_token.
|
2010-09-26 13:08:25 -04:00
|
|
|
def token_authenticatable
|
2010-06-13 05:48:35 -04:00
|
|
|
apply_devise_schema :authentication_token, String
|
2010-01-23 21:38:52 -05:00
|
|
|
end
|
|
|
|
|
2009-11-13 19:33:00 -05:00
|
|
|
# Creates confirmation_token, confirmed_at and confirmation_sent_at.
|
|
|
|
def confirmable
|
2010-06-13 05:48:35 -04:00
|
|
|
apply_devise_schema :confirmation_token, String
|
|
|
|
apply_devise_schema :confirmed_at, DateTime
|
|
|
|
apply_devise_schema :confirmation_sent_at, DateTime
|
2009-11-13 19:33:00 -05:00
|
|
|
end
|
|
|
|
|
2011-01-24 10:48:44 -05:00
|
|
|
# Creates reset_password_token and reset_password_sent_at.
|
2011-03-30 09:35:38 -04:00
|
|
|
#
|
|
|
|
# == 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?)
|
2010-06-13 05:48:35 -04:00
|
|
|
apply_devise_schema :reset_password_token, String
|
2011-03-30 09:35:38 -04:00
|
|
|
apply_devise_schema :reset_password_sent_at, DateTime if use_within
|
2009-11-13 19:33:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Creates remember_token and remember_created_at.
|
2010-09-25 06:37:06 -04:00
|
|
|
#
|
|
|
|
# == Options
|
|
|
|
# * :use_salt - When true, does not create a remember_token and use password_salt instead.
|
|
|
|
def rememberable(options={})
|
|
|
|
use_salt = options.fetch(:use_salt, Devise.use_salt_as_remember_token)
|
|
|
|
apply_devise_schema :remember_token, String unless use_salt
|
2010-06-13 05:48:35 -04:00
|
|
|
apply_devise_schema :remember_created_at, DateTime
|
2009-11-13 19:33:00 -05:00
|
|
|
end
|
|
|
|
|
2009-11-24 12:18:42 -05:00
|
|
|
# Creates sign_in_count, current_sign_in_at, last_sign_in_at,
|
2009-11-24 12:46:54 -05:00
|
|
|
# current_sign_in_ip, last_sign_in_ip.
|
2009-11-24 12:18:42 -05:00
|
|
|
def trackable
|
2010-06-13 05:48:35 -04:00
|
|
|
apply_devise_schema :sign_in_count, Integer, :default => 0
|
|
|
|
apply_devise_schema :current_sign_in_at, DateTime
|
|
|
|
apply_devise_schema :last_sign_in_at, DateTime
|
|
|
|
apply_devise_schema :current_sign_in_ip, String
|
|
|
|
apply_devise_schema :last_sign_in_ip, String
|
2009-11-24 12:18:42 -05:00
|
|
|
end
|
|
|
|
|
2010-03-31 05:54:11 -04:00
|
|
|
# Creates failed_attempts, unlock_token and locked_at depending on the options given.
|
2010-03-29 18:07:11 -04:00
|
|
|
#
|
|
|
|
# == Options
|
2010-03-31 05:54:11 -04:00
|
|
|
# * :unlock_strategy - The strategy used for unlock. Can be :time, :email, :both (default), :none.
|
2010-03-29 18:07:11 -04:00
|
|
|
# If :email or :both, creates a unlock_token field.
|
2010-03-31 05:54:11 -04:00
|
|
|
# * :lock_strategy - The strategy used for locking. Can be :failed_attempts (default) or :none.
|
2010-03-29 18:07:11 -04:00
|
|
|
def lockable(options={})
|
2010-03-31 05:54:11 -04:00
|
|
|
unlock_strategy = options[:unlock_strategy]
|
|
|
|
unlock_strategy ||= self.unlock_strategy if respond_to?(:unlock_strategy)
|
|
|
|
unlock_strategy ||= :both
|
2010-03-29 18:07:11 -04:00
|
|
|
|
2010-03-31 05:54:11 -04:00
|
|
|
lock_strategy = options[:lock_strategy]
|
|
|
|
lock_strategy ||= self.lock_strategy if respond_to?(:lock_strategy)
|
|
|
|
lock_strategy ||= :failed_attempts
|
|
|
|
|
|
|
|
if lock_strategy == :failed_attempts
|
2010-06-13 05:48:35 -04:00
|
|
|
apply_devise_schema :failed_attempts, Integer, :default => 0
|
2010-03-29 19:58:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if [:both, :email].include?(unlock_strategy)
|
2010-06-13 05:48:35 -04:00
|
|
|
apply_devise_schema :unlock_token, String
|
2010-03-29 19:58:06 -04:00
|
|
|
end
|
|
|
|
|
2010-06-13 05:48:35 -04:00
|
|
|
apply_devise_schema :locked_at, DateTime
|
2009-12-30 12:19:33 -05:00
|
|
|
end
|
|
|
|
|
2009-11-13 19:33:00 -05:00
|
|
|
# Overwrite with specific modification to create your own schema.
|
2010-06-13 05:48:35 -04:00
|
|
|
def apply_devise_schema(name, type, options={})
|
2009-11-13 19:33:00 -05:00
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|