C0 code coverage information
Generated on Tue May 30 23:34:44 CEST 2006 with rcov 0.5.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 require 'digest/sha1'
2
3 # this model expects a certain database layout and its based on the name/login pattern.
4 class User < ActiveRecord::Base
5
6 # echo "typo" | sha1sum -
7 @@salt = '20ac4d290c2293702c64b3b287ae5ea79b26a5c1'
8 cattr_accessor :salt
9
10 # Authenticate a user.
11 #
12 # Example:
13 # @user = User.authenticate('bob', 'bobpass')
14 #
15 def self.authenticate(login, pass)
16 find_first(["login = ? AND password = ?", login, sha1(pass)])
17 end
18
19 def self.authenticate?(login, pass)
20 user = self.authenticate(login, pass)
21 return false if user.nil?
22 return true if user.login == login
23
24 false
25 end
26
27 protected
28
29 # Apply SHA1 encryption to the supplied password.
30 # We will additionally surround the password with a salt
31 # for additional security.
32 def self.sha1(pass)
33 Digest::SHA1.hexdigest("#{salt}--#{pass}--")
34 end
35
36 before_create :crypt_password
37
38 # Before saving the record to database we will crypt the password
39 # using SHA1.
40 # We never store the actual password in the DB.
41 def crypt_password
42 write_attribute "password", self.class.sha1(password)
43 end
44
45 before_update :crypt_unless_empty
46
47 # If the record is updated we will check if the password is empty.
48 # If its empty we assume that the user didn't want to change his
49 # password and just reset it to the old value.
50 def crypt_unless_empty
51 if password.empty?
52 user = self.class.find(self.id)
53 self.password = user.password
54 else
55 write_attribute "password", self.class.sha1(password)
56 end
57 end
58
59 validates_uniqueness_of :login, :on => :create
60 validates_length_of :password, :within => 5..40, :on => :create
61 validates_presence_of :login
62
63 validates_confirmation_of :password, :if=> Proc.new { |u| u.password.size > 0}
64 validates_length_of :login, :within => 3..40
65 end
Generated using the rcov code coverage analysis tool for Ruby version 0.5.0.