Created
August 29, 2012 19:10
-
-
Save efatsi/3517365 to your computer and use it in GitHub Desktop.
password reset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PasswordReset < ActiveRecord::Base | |
include ActiveModel::Validations | |
include ActiveModel::Conversion | |
attr_accessor :existing_user, :user, :identifier, :password, :password_confirmation | |
validates_presence_of :identifier, :unless => :persisted? | |
validates_presence_of :password, :if => :persisted? | |
validates_confirmation_of :password, :if => :password? | |
validate :user_exists, :unless => :persisted? | |
validate :user_has_email_address, :unless => :persisted? | |
def self.find(id) | |
new(:existing_user => User.find_by_password_reset_token(id)) | |
end | |
def self.new(attributes = {}) | |
self.attributes = attributes | |
end | |
def save | |
if valid? | |
user.send_password_reset | |
true | |
else | |
false | |
end | |
end | |
def update_attributes(attributes = {}) | |
self.attributes = attributes | |
if valid? | |
existing_user.password = attributes[:password] | |
existing_user.password_confirmation = attributes[:password_confirmation] | |
existing_user.password_reset_token = nil | |
existing_user.save | |
else | |
false | |
end | |
end | |
def user | |
@user ||= if identifier.present? | |
User.where(['username = :identifier OR email = :identifier', :identifier => identifier]).first | |
end | |
end | |
def id | |
existing_user.try(:password_reset_token) | |
end | |
def expired? | |
existing_user.password_reset_sent_at < 2.hours.ago | |
end | |
def persisted? | |
id.present? | |
end | |
private | |
def user_exists | |
errors.add(:user, "was not found") unless user | |
end | |
def user_has_email_address | |
errors.add(:user, "must have an email address") if user && user.email.blank? | |
end | |
def password? | |
password.present? | |
end | |
def attributes=(attributes) | |
(attributes || {}).each {|k,v| send("#{k}=", v) } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment