2009-09-18 10:20:45 -03:00
|
|
|
module Devise
|
2009-10-09 08:30:25 -03:00
|
|
|
module Models
|
2009-10-09 09:27:44 -03:00
|
|
|
|
|
|
|
# Recoverable takes care of reseting the user password and send reset instructions
|
|
|
|
# Examples:
|
|
|
|
#
|
|
|
|
# # resets the user password and save the record, true if valid passwords are given, otherwise false
|
|
|
|
# User.find(1).reset_password!('password123', 'password123')
|
2009-11-24 13:56:54 -02:00
|
|
|
#
|
2009-10-09 09:27:44 -03:00
|
|
|
# # only resets the user password, without saving the record
|
|
|
|
# user = User.find(1)
|
|
|
|
# user.reset_password('password123', 'password123')
|
2009-11-24 13:56:54 -02:00
|
|
|
#
|
2009-10-09 09:27:44 -03:00
|
|
|
# # creates a new token and send it with instructions about how to reset the password
|
|
|
|
# User.find(1).send_reset_password_instructions
|
2009-10-09 08:30:25 -03:00
|
|
|
module Recoverable
|
2010-02-17 12:35:38 +01:00
|
|
|
extend ActiveSupport::Concern
|
2009-09-18 10:20:45 -03:00
|
|
|
|
2009-10-15 17:36:44 -03:00
|
|
|
# Update password saving the record and clearing token. Returns true if
|
|
|
|
# the passwords are valid and the record was saved, false otherwise.
|
2009-10-09 08:30:25 -03:00
|
|
|
def reset_password!(new_password, new_password_confirmation)
|
2009-12-15 00:30:28 +01:00
|
|
|
self.password = new_password
|
|
|
|
self.password_confirmation = new_password_confirmation
|
2009-10-18 09:54:53 -02:00
|
|
|
clear_reset_password_token if valid?
|
2009-10-15 17:36:44 -03:00
|
|
|
save
|
2009-09-18 10:20:45 -03:00
|
|
|
end
|
2009-09-18 10:47:12 -03:00
|
|
|
|
2009-10-18 10:16:30 -02:00
|
|
|
# Resets reset password token and send reset password instructions by email
|
2009-10-09 08:30:25 -03:00
|
|
|
def send_reset_password_instructions
|
2009-10-18 09:14:52 -02:00
|
|
|
generate_reset_password_token!
|
2010-02-17 12:25:20 +01:00
|
|
|
::Devise::Mailer.reset_password_instructions(self).deliver
|
2009-10-09 08:30:25 -03:00
|
|
|
end
|
|
|
|
|
2009-10-18 09:14:52 -02:00
|
|
|
protected
|
|
|
|
|
|
|
|
# Generates a new random token for reset password
|
|
|
|
def generate_reset_password_token
|
2009-11-18 09:26:47 -02:00
|
|
|
self.reset_password_token = Devise.friendly_token
|
2009-10-18 09:14:52 -02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Resets the reset password token with and save the record without
|
|
|
|
# validating
|
|
|
|
def generate_reset_password_token!
|
2010-02-16 14:31:49 +01:00
|
|
|
generate_reset_password_token && save(:validate => false)
|
2009-10-18 09:14:52 -02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Removes reset_password token
|
|
|
|
def clear_reset_password_token
|
|
|
|
self.reset_password_token = nil
|
|
|
|
end
|
|
|
|
|
2009-10-09 08:30:25 -03:00
|
|
|
module ClassMethods
|
|
|
|
# Attempt to find a user by it's email. If a record is found, send new
|
|
|
|
# password instructions to it. If not user is found, returns a new user
|
|
|
|
# with an email not found error.
|
2009-10-10 16:20:23 -03:00
|
|
|
# Attributes must contain the user email
|
|
|
|
def send_reset_password_instructions(attributes={})
|
2009-11-24 21:35:07 -02:00
|
|
|
recoverable = find_or_initialize_with_error_by(:email, attributes[:email], :not_found)
|
2010-03-28 13:26:07 -07:00
|
|
|
recoverable.send_reset_password_instructions if recoverable.persisted?
|
2009-10-09 08:30:25 -03:00
|
|
|
recoverable
|
|
|
|
end
|
|
|
|
|
2009-10-18 09:14:52 -02:00
|
|
|
# Attempt to find a user by it's reset_password_token to reset it's
|
|
|
|
# password. If a user is found, reset it's password and automatically
|
|
|
|
# try saving the record. If not user is found, returns a new user
|
2009-10-18 10:16:30 -02:00
|
|
|
# containing an error in reset_password_token attribute.
|
2009-10-18 09:14:52 -02:00
|
|
|
# Attributes must contain reset_password_token, password and confirmation
|
2010-03-10 16:13:54 +01:00
|
|
|
def reset_password_by_token(attributes={})
|
2009-11-24 21:35:07 -02:00
|
|
|
recoverable = find_or_initialize_with_error_by(:reset_password_token, attributes[:reset_password_token])
|
2010-03-28 13:26:07 -07:00
|
|
|
recoverable.reset_password!(attributes[:password], attributes[:password_confirmation]) if recoverable.persisted?
|
2009-10-09 08:30:25 -03:00
|
|
|
recoverable
|
|
|
|
end
|
2009-09-18 10:47:12 -03:00
|
|
|
end
|
2009-09-18 10:20:45 -03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|