Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Dr Nic Williams http://drnicwilliams.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Reset user password based on another users

When you first create an app you might not have time *cough* to add special user password reset features. Especially if you don't have many users.

Here's a quick script to set the password of a user to another user (say a manually created user 'tester' with a known password).

   1  
   2  namespace :user do
   3    desc 'Reset user (USER=username) password to that of username "tester" (or FROM=username env)'
   4    task :reset => :environment do
   5      reset_username = ENV['USER']
   6      unless ENV['USER']
   7        puts 'Require "USER=username" for user to be reset'
   8        next
   9      end
  10      reset_user     = User.find_by_username reset_username
  11      unless reset_user
  12        puts "Cannot find user: #{reset_username}"
  13        next
  14      end
  15      from_username  = ENV['FROM'] || 'tester'
  16      from_user      = User.find_by_username from_username
  17      unless from_user
  18        puts "Cannot find user: #{from_username}"
  19        next
  20      end
  21      
  22      reset_user.crypted_password = from_user.crypted_password
  23      reset_user.salt             = from_user.salt
  24      reset_user  .save
  25      puts 'User password reset'
  26    end
  27  end
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS