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
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.
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.
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/>.
20 class User < ActiveRecord::Base
23 # Virtual attribute for the unencrypted password
24 attr_accessor :password
25 attr_accessor :current_user
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
37 [ firstname, lastname].join(" ")
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
46 # Encrypts some data with the salt.
47 def self.encrypt(password, salt)
48 Digest::SHA1.hexdigest("--#{salt}--#{password}--")
51 # Encrypts the password with the user salt
53 self.class.encrypt(password, salt)
56 def authenticated?(password)
57 crypted_password == encrypt(password)
61 remember_token_expires_at && Time.now.utc < remember_token_expires_at
64 # These create and unset the fields required for remembering users between browser closes
66 self.remember_token_expires_at = 2.weeks.from_now.utc
67 self.remember_token = encrypt("#{email}--#{remember_token_expires_at}")
72 self.remember_token_expires_at = nil
73 self.remember_token = nil
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)
85 def password_required?
86 crypted_password.blank? || !password.blank?
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 ]*$/