Skip to content

Instantly share code, notes, and snippets.

@mphalliday
Forked from Bertg/devise_migration.rb
Created June 4, 2011 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mphalliday/1007890 to your computer and use it in GitHub Desktop.
Save mphalliday/1007890 to your computer and use it in GitHub Desktop.
Migrating to a new password encryption in devise, coming from authlogic
class MigrateUserToDevise < ActiveRecord::Migration
def self.up
change_table :users do |t|
t.string :encrypted_password, :null => false, :limit => 128
# ...
end
end
def self.down
end
end
module Devise
module PasswordEncryptionMigration
def valid_password?(incoming_password)
if using_authlogic_validation?
Devise.secure_compare(authlogic_password_digest(incoming_password), self.crypted_password).tap do |validated|
if validated
self.password = incoming_password
self.crypted_password = nil
self.save(:validate => false)
end
end
else
Devise.secure_compare(password_digest(incoming_password), self.encrypted_password)
end
end
def using_authlogic_validation?
self.encrypted_password.blank?
end
def authlogic_password_digest(password)
if self.password_salt.present?
Devise::Encryptors::AuthlogicSha512.digest(password, 20, self.password_salt, nil)
end
end
end
end
class User < ActiveRecord::Base
# devise ...
include Devise::PasswordEncryptionMigration
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment