1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00
heartcombo--devise/lib/devise/schema.rb

103 lines
3.9 KiB
Ruby
Raw Normal View History

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 when enabled (on by default), encrypted_password and password_salt.
2009-11-13 19:33:00 -05:00
#
# == Options
# * :null - When true, allow columns to be null.
# * :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.
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
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
end
2009-11-13 19:33:00 -05:00
# Creates password salt for encryption support.
def encryptable
apply_devise_schema :password_salt, String
end
# Creates authentication_token.
def token_authenticatable
apply_devise_schema :authentication_token, String
end
2009-11-13 19:33:00 -05:00
# Creates confirmation_token, confirmed_at and confirmation_sent_at.
def confirmable
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 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?)
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.
#
# == 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
apply_devise_schema :remember_created_at, DateTime
2009-11-13 19:33:00 -05:00
end
# 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.
def trackable
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
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
apply_devise_schema :failed_attempts, Integer, :default => 0
end
if [:both, :email].include?(unlock_strategy)
apply_devise_schema :unlock_token, String
end
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.
def apply_devise_schema(name, type, options={})
2009-11-13 19:33:00 -05:00
raise NotImplementedError
end
end
end