forked from heartcombo/devise
-
Notifications
You must be signed in to change notification settings - Fork 4
How To: Migration legacy database
shanlalit edited this page May 18, 2012
·
5 revisions
migrate a legacy user database to devise, which is using password MD5 hexdigest without salt, put this code in initializers:
module Devise
module Models
DatabaseAuthenticatable.class_eval do
def valid_password_with_legacy?(password)
if self.legacy_password_hash.present?
if ::Digest::MD5.hexdigest(password).upcase == self.legacy_password_hash
self.password = password
self.legacy_password_hash = nil
self.save!
return true
else
return false
end
else
return valid_password_without_legacy?(password)
end
end
alias_method_chain :valid_password?, :legacy
end
Recoverable.class_eval do
def reset_password_with_legacy!(new_password, new_password_confirmation)
self.legacy_password_hash = nil
reset_password_without_legacy!(new_password, new_password_confirmation)
end
alias_method_chain :reset_password!, :legacy
end
end
end
Josevalim suggested a much better solution in (issue #1858), just override valid_password? and reset_password? and use 'super' in your devise model
class User
def valid_password?(password)
if self.legacy_password_hash.present?
if ::Digest::MD5.hexdigest(password).upcase == self.legacy_password_hash
self.password = password
self.legacy_password_hash = nil
self.save!
true
else
false
end
else
super
end
end
def reset_password!(*args)
self.legacy_password_hash = nil
super
end
end