C0 code coverage information
Generated on Tue Dec 04 22:06:28 -0500 2007 with rcov 0.8.0
Code reported as executed by Ruby looks like this...
and this: this line is also marked as covered.
Lines considered as run by rcov, but not reported by Ruby, look like this,
and this: these lines were inferred by rcov (using simple heuristics).
Finally, here's a line marked as not executed.
1 #####
2 #
3 # Copyright 2007 Chris Strom, Robin Strom
4 #
5 # This file is part of EEE Code (though much was auto-generated by the
6 # acts_as_authenticated plugin, which is licensed under the MIT
7 # license).
8 #
9 # EEE Code is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # EEE Code is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with EEE Code. If not, see <http://www.gnu.org/licenses/>.
21 #
22 require 'digest/sha1'
23 class User < ActiveRecord::Base
24 has_many :user_roles, :dependent => :destroy
25 has_many :roles, :through => :user_roles
26
27 # Virtual attribute for the unencrypted password
28 attr_accessor :password
29
30 validates_presence_of :login, :email
31 validates_presence_of :password, :if => :password_required?
32 validates_presence_of :password_confirmation, :if => :password_required?
33 validates_length_of :password, :within => 4..40, :if => :password_required?
34 validates_confirmation_of :password, :if => :password_required?
35 validates_length_of :login, :within => 3..40
36 validates_length_of :email, :within => 3..100
37 validates_uniqueness_of :login, :email, :case_sensitive => false
38 before_save :encrypt_password
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 def author?
78 self.roles.include?(Role.author)
79 end
80
81 protected
82 # before filter
83 def encrypt_password
84 return if password.blank?
85 self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
86 self.crypted_password = encrypt(password)
87 end
88
89 def password_required?
90 crypted_password.blank? || !password.blank?
91 end
92 end
Generated using the rcov code coverage analysis tool for Ruby version 0.8.0.