2 class User < ActiveRecord::Base
5 # Virtual attribute for the unencrypted password
6 attr_accessor :password
8 validates_presence_of :login, :email, :if => :not_openid?
9 validates_presence_of :password, :if => :password_required?
10 validates_presence_of :password_confirmation, :if => :password_required?
11 validates_length_of :password, :within => 4..40, :if => :password_required?
12 validates_confirmation_of :password, :if => :password_required?
13 validates_length_of :login, :within => 3..40, :if => :not_openid?
14 validates_length_of :email, :within => 3..100, :if => :not_openid?
15 validates_uniqueness_of :login, :email, :case_sensitive => false, :allow_nil => true
17 before_save :encrypt_password
18 before_create :make_activation_code
19 # prevents a user from submitting a crafted form that bypasses activation
20 # anything else you want your user to change should be added here.
21 attr_accessible :display_name, :login, :email, :password, :password_confirmation, :identity_url
23 has_many :issues, :class_name => "Issue", :foreign_key => "created_by"
24 has_many :comments, :class_name => "Comment", :foreign_key => "created_by"
26 has_many :recent_issues, :class_name => "Issue", :foreign_key => "created_by", :order => "created_at", :limit => 5
27 has_many :recent_comments, :class_name => "Comment", :foreign_key => "created_by", :group => "issue_id", :limit => 5
29 # Activates the user in the database.
32 self.activated_at = Time.now.utc
33 self.activation_code = nil
38 # the existence of an activation code means they have not activated yet
42 # Returns true if the user has just been activated.
43 def recently_activated?
47 # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
48 def self.authenticate(login, password)
49 u = find :first, :conditions => ['login = ? and activated_at IS NOT NULL', login] # need to get the salt
50 u && u.authenticated?(password) ? u : nil
53 # Encrypts some data with the salt.
54 def self.encrypt(password, salt)
55 Digest::SHA1.hexdigest("--#{salt}--#{password}--")
58 # Encrypts the password with the user salt
60 self.class.encrypt(password, salt)
63 def authenticated?(password)
64 crypted_password == encrypt(password)
68 remember_token_expires_at && Time.now.utc < remember_token_expires_at
71 # These create and unset the fields required for remembering users between browser closes
73 remember_me_for 2.weeks
76 def remember_me_for(time)
77 remember_me_until time.from_now.utc
80 def remember_me_until(time)
81 self.remember_token_expires_at = time
82 self.remember_token = encrypt("#{email}--#{remember_token_expires_at}")
87 self.remember_token_expires_at = nil
88 self.remember_token = nil
95 return if password.blank?
96 self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
97 self.crypted_password = encrypt(password)
100 def password_required?
101 not_openid? && (crypted_password.blank? || !password.blank?)
104 def make_activation_code
105 self.activation_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )