Skip to content

Instantly share code, notes, and snippets.

@phss
Created March 17, 2010 22:19
Show Gist options
  • Save phss/335800 to your computer and use it in GitHub Desktop.
Save phss/335800 to your computer and use it in GitHub Desktop.
require "dm-core"
require "dm-validations"
require "dm-timestamps"
require "digest"
class User
include DataMapper::Resource
property :id, Serial
property :username, String, :required => true, :unique => true
property :email, String
property :encrypted_password, String
property :salt, String
attr_accessor :password, :password_confirmation
validates_present :password, :password_confirmation
validates_is_confirmed :password
before :save do
self.salt = self.object_id.to_s + rand.to_s
self.encrypted_password = encrypt(password)
end
def match_password?(password)
encrypted_password == encrypt(password)
end
private
def encrypt(string)
Digest::SHA1.hexdigest("#{string}-blah-#{salt}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment