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
|
|
|
|
|
|
|
|
# Creates email, encrypted_password and password_salt.
|
|
|
|
#
|
|
|
|
# == 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-03-31 15:43:19 -04:00
|
|
|
default = options[:default] || ""
|
2010-03-29 18:07:11 -04:00
|
|
|
|
2010-06-13 05:48:35 -04:00
|
|
|
apply_devise_schema :email, String, :null => null, :default => default
|
2010-06-18 16:00:31 -04:00
|
|
|
apply_devise_schema :encrypted_password, String, :null => null, :default => default, :limit => 128
|
2010-06-13 05:48:35 -04:00
|
|
|
apply_devise_schema :password_salt, String, :null => null, :default => default
|
2010-03-29 10:13:19 -04:00
|
|
|
end
|
2009-11-13 19:33:00 -05:00
|
|
|
|
2010-01-23 21:38:52 -05:00
|
|
|
# Creates authentication_token.
|
2010-03-29 18:07:11 -04:00
|
|
|
def token_authenticatable(options={})
|
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
|
|
|
|
|
|
|
|
# Creates reset_password_token.
|
|
|
|
def recoverable
|
2010-06-13 05:48:35 -04:00
|
|
|
apply_devise_schema :reset_password_token, String
|
2009-11-13 19:33:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Creates remember_token and remember_created_at.
|
|
|
|
def rememberable
|
2010-06-13 05:48:35 -04:00
|
|
|
apply_devise_schema :remember_token, String
|
|
|
|
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
|