Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

113 lines (88 sloc) 2.785 kB
require 'digest/sha1'
module Clearance
module User
extend ActiveSupport::Concern
included do
attr_accessor :password_changing
attr_reader :password
include Validations
include Callbacks
include (Clearance.configuration.password_strategy ||
Clearance::PasswordStrategies::BCrypt)
end
module ClassMethods
def authenticate(email, password)
if user = find_by_email(email.to_s.downcase)
if user.authenticated? password
return user
end
end
end
end
module Validations
extend ActiveSupport::Concern
included do
validates_presence_of :email, :unless => :email_optional?
validates_uniqueness_of :email, :allow_blank => true
validates_format_of :email, :with => %r{^[a-z0-9!#\$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#\$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$}i, :allow_blank => true
validates_presence_of :password, :unless => :password_optional?
end
end
module Callbacks
extend ActiveSupport::Concern
included do
before_validation :downcase_email
before_create :generate_remember_token
end
end
def forgot_password!
generate_confirmation_token
save :validate => false
end
def remember_me!
warn '[DEPRECATION] remember_me! will be removed in Clearance 1.0.0: use reset_remember_token! instead'
reset_remember_token!
end
def reset_remember_token!
generate_remember_token
save :validate => false
end
def update_password(new_password)
self.password_changing = true
self.password = new_password
if valid?
self.confirmation_token = nil
generate_remember_token
end
save
end
private
def downcase_email
self.email = email.to_s.downcase
end
def email_optional?
false
end
def generate_confirmation_token
self.confirmation_token = SecureRandom.hex(20).encode('UTF-8')
end
def generate_random_code(length = 20)
warn "[DEPRECATION] generate_random_code will be removed in Clearance 1.0.0: instead, use SecureRandom.hex(20).encode 'UTF-8'"
if RUBY_VERSION >= '1.9'
SecureRandom.hex(length).encode 'UTF-8'
else
SecureRandom.hex length
end
end
def generate_remember_token
self.remember_token = SecureRandom.hex(20).encode('UTF-8')
end
def password_optional?
encrypted_password.present? && password.blank? && password_changing.blank?
end
def password_required?
warn '[DEPRECATION] password_required?: use !password_optional? instead'
!password_optional?
end
end
end
Jump to Line
Something went wrong with that request. Please try again.