]> git.openstreetmap.org Git - rails.git/blob - app/models/user.rb
1a12790420a899473de6c4c56fc890a0874e0fd5
[rails.git] / app / models / user.rb
1 require 'digest/md5'
2
3 class User < ActiveRecord::Base
4   has_many :traces
5
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
12
13   def set_defaults
14     self.creation_time = Time.now
15     self.timeout = Time.now
16     self.token = make_token()
17   end
18   
19   def pass_crypt=(str) 
20     write_attribute("pass_crypt", Digest::MD5.hexdigest(str)) 
21   end 
22
23   def pass_crypt_confirmation=(str) 
24     write_attribute("pass_crypt_confirm", Digest::MD5.hexdigest(str)) 
25   end 
26
27   def self.authenticate(email, passwd) 
28     find_first([ "email = ? AND pass_crypt =?", 
29                email, 
30                Digest::MD5.hexdigest(passwd) ]) 
31   end 
32   
33   private
34   def make_token
35     chars = 'abcdefghijklmnopqrtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
36     confirmstring = ''
37
38     30.times do
39       confirmstring += chars[(rand * chars.length).to_i].chr
40     end
41
42     return confirmstring
43   end
44
45 end