Created
August 21, 2011 17:20
-
-
Save willywg/1160873 to your computer and use it in GitHub Desktop.
Mass password reset and email notification for Devise 1.4.2 with Rails 3.x
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
# Assign an new random password for each user and send them a email notification | |
# path: lib/tasks/devise.rake | |
namespace :devise do | |
desc 'Mass password reset and send email instructions' | |
task :mass_password_reset => :environment do | |
model = ENV['MODEL'] || 'User' | |
begin | |
model_mailer = "#{model}Mailer".constantize | |
model = model.constantize | |
model.find_each(:conditions => 'id > 70720') do |record| | |
# Assign a random password | |
random_password = User.send(:generate_token, 'encrypted_password').slice(0, 8) | |
record.password = random_password | |
record.save | |
# Send change notification (Ensure you have created #{model}Mailer e.g. UserMailer) | |
model_mailer.password_reset(record, random_password).deliver | |
end | |
rescue Exception => e | |
puts "Error: #{e.message}" | |
end | |
end | |
end |
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
# Send password reset notification | |
# path: app/mailers/user_mailer.rb | |
class UserMailer < ActionMailer::Base | |
default :from => "[email protected]" | |
def password_reset(user, password) | |
@user = user | |
@password = password | |
mail(:to => user.email, | |
:subject => 'Password Reset Notification') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment