Created
September 24, 2010 06:40
-
-
Save Ranjithkumar/594946 to your computer and use it in GitHub Desktop.
User model common logics
This file contains hidden or 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
module UserCommonLogic | |
def self.included(base) | |
base.extend ClassMethods | |
end | |
#Class methods | |
module ClassMethods | |
def authenticate(username, password) | |
u = find :first, :conditions => ['username = ?', username] | |
u && u.authenticated?(password) ? u : nil | |
end | |
def find_by_email_or_username(str) | |
find :first, :conditions =>["email=? OR username=?", str, str] | |
end | |
def encrypt(password, salt) | |
Digest::SHA1.hexdigest("--#{salt}--#{password}--") | |
end | |
end | |
#InstanceMethods | |
def authenticated?(password) | |
hash_password == encrypt(password) | |
end | |
def forget_me | |
self.remember_token_expires_at = nil | |
self.remember_token = nil | |
save(false) | |
end | |
# These create and unset the fields required for remembering users between browser closes | |
def remember_me | |
self.remember_token_expires_at = 2.weeks.from_now.utc | |
self.remember_token = encrypt("#{email}--#{remember_token_expires_at}") | |
save(false) | |
end | |
# Resets reset password token and send reset password instructions by email | |
def send_reset_password_instructions | |
generate_reset_password_token! | |
UserNotifier.deliver_reset_password_instructions(self) | |
end | |
# Removes reset_password token | |
def clear_reset_password_token | |
self.reset_password_token = nil | |
end | |
def check_if_its_new_email_then_send_confirmation(prev_email, params) | |
self.email = prev_email | |
self.new_email = params[:user][:email] | |
self.new_email_token = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join ) | |
save(false) | |
UserNotifier.deliver_confirmation_for_new_email(self) | |
end | |
def confirm_email | |
update_attributes(:email => self.new_email, :new_email => nil, :new_email_token => nil) | |
end | |
def cancel_email_change | |
self.new_email = nil | |
self.new_email_token = nil | |
save(false) | |
end | |
def display_name | |
[first_name.to_s.capitalize, last_name.to_s.capitalize].join(" ").strip | |
end | |
################################## | |
protected | |
################################## | |
def encrypt_password | |
return if password.blank? | |
self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{email}--") if new_record? | |
self.hash_password = encrypt(password) | |
end | |
# Encrypts the password with the user salt | |
def encrypt(password) | |
self.class.encrypt(password, salt) | |
end | |
# Generates a new random token for reset password | |
def generate_reset_password_token | |
self.reset_password_token = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join ) | |
end | |
# Resets the reset password token with and save the record without | |
# validating | |
def generate_reset_password_token! | |
generate_reset_password_token && save(false) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I didn't use devise for authentication becz my application is very simple and this is for who is using simple authentication plugin/library.
Rails => DRY
I found that all the applications, we will write the common features like authenticate, forgot password, remember me, send reset password instructions, check if its new email then send confirmation, confirm email, cancel email change and etc. so i created common library for User model. You just "include UserCommonLogic" in your User model and change based on your user attributes. You have to add notification templates for confirmation_for_new_email, reset_password_instructions etc. thats it.
my users table structure :-
create_table "users", :force => true do |t|
t.string "first_name", :limit => 50
t.string "last_name", :limit => 100
t.string "username", :limit => 50
t.string "email"
t.string "hash_password"
t.date "birthday"
t.string "gender", :limit => 10
t.integer "location_id"
t.string "nationality", :limit => 25
t.string "locale", :limit => 2
t.string "reset_password_token", :limit => 40
t.string "salt"
t.string "remember_token", :limit => 40
t.datetime "remember_token_expires_at"
t.boolean "first_login", :default => true
t.string "new_email"
t.string "new_email_token", :limit => 40
t.text "bio"
t.datetime "created_at"
t.datetime "updated_at"
end