merged back from production
[selectricity-live] / app / models / user.rb
1 # Selectricity: Voting Machinery for the Masses
2 # Copyright (C) 2007, 2008 Benjamin Mako Hill <mako@atdot.cc>
3 # Copyright (C) 2007 Massachusetts Institute of Technology
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Affero General Public License for more details.
14 #
15 # You should have received a copy of the GNU Affero General Public
16 # License along with this program.  If not, see
17 # <http://www.gnu.org/licenses/>.
18
19 require 'digest/sha1'
20 class User < ActiveRecord::Base
21   has_many :elections
22
23   # Virtual attribute for the unencrypted password
24   attr_accessor :password
25   attr_accessor :current_user
26   
27   validates_presence_of     :email
28   validates_presence_of     :password,                   :if => :password_required?
29   validates_presence_of     :password_confirmation,      :if => :password_required?
30   validates_length_of       :password, :within => 4..40, :if => :password_required?
31   validates_confirmation_of :password,                   :if => :password_required?
32   validates_length_of       :login,    :within => 3..40
33   validates_uniqueness_of   :login, :email, :case_sensitive => false
34   before_save :encrypt_password
35
36   def name
37      [ firstname, lastname].join(" ")
38   end
39
40   # Authenticates a user by their login name and unencrypted password.  Returns the user or nil.
41   def self.authenticate(login, password)
42     u = find_by_login(login) # need to get the salt
43     u && u.authenticated?(password) ? u : nil
44   end
45
46   # Encrypts some data with the salt.
47   def self.encrypt(password, salt)
48     Digest::SHA1.hexdigest("--#{salt}--#{password}--")
49   end
50
51   # Encrypts the password with the user salt
52   def encrypt(password)
53     self.class.encrypt(password, salt)
54   end
55
56   def authenticated?(password)
57     crypted_password == encrypt(password)
58   end
59
60   def remember_token?
61     remember_token_expires_at && Time.now.utc < remember_token_expires_at 
62   end
63
64   # These create and unset the fields required for remembering users between browser closes
65   def remember_me
66     self.remember_token_expires_at = 2.weeks.from_now.utc
67     self.remember_token            = encrypt("#{email}--#{remember_token_expires_at}")
68     save(false)
69   end
70
71   def forget_me
72     self.remember_token_expires_at = nil
73     self.remember_token            = nil
74     save(false)
75   end
76
77   protected
78     # before filter 
79     def encrypt_password
80       return if password.blank?
81       self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
82       self.crypted_password = encrypt(password)
83     end
84     
85     def password_required?
86       crypted_password.blank? || !password.blank?
87     end
88     def validate
89       # E-mail regex, moderate complexity
90       # Stolen from http://www.regular-expressions.info/email.html
91       errors.add(:email, "is not valid") unless email  =~
92                   /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i
93       errors.add(:login, "should not begin or end with spaces") if login and login.strip!
94       errors.add(:login, "should contain only letters, numbers, and spaces") unless login =~ /^[A-Za-z0-9 ]*$/
95     end
96 end

Benjamin Mako Hill || Want to submit a patch?