3 class User < ActiveRecord::Base
6 validates_confirmation_of :pass_crypt, :message => 'Password must match the confirmation password'
7 validates_uniqueness_of :display_name
8 validates_uniqueness_of :email
9 validates_length_of :pass_crypt, :minimum => 8
10 validates_length_of :display_name, :minimum => 3
11 validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
14 self.creation_time = Time.now
15 self.timeout = Time.now
16 self.token = User.make_token()
20 write_attribute("pass_crypt", Digest::MD5.hexdigest(str))
23 def pass_crypt_confirmation=(str)
24 write_attribute("pass_crypt_confirm", Digest::MD5.hexdigest(str))
27 def self.authenticate(email, passwd)
28 find_first([ "email = ? AND pass_crypt =?", email, Digest::MD5.hexdigest(passwd) ])
31 def self.authenticate_token(token)
32 find_first([ "token = ? ", token])
35 def self.make_token(length=30)
36 chars = 'abcdefghijklmnopqrtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
40 confirmstring += chars[(rand * chars.length).to_i].chr