Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 206
- Log:
Initial import of Gullery, an open source photo gallery:
http://nubyonrails.com/pages/gullery
- Author:
- rool
- Date:
- Sun May 20 19:05:59 +0100 2007
- Size:
- 1697 Bytes
1 | require 'digest/sha1' |
2 | class User < ActiveRecord::Base |
3 | # Virtual attribute for the unencrypted password |
4 | attr_accessor :password |
5 | |
6 | validates_presence_of :login |
7 | validates_presence_of :password, :if => :password_required? |
8 | validates_presence_of :password_confirmation, :if => :password_required? |
9 | validates_length_of :password, :within => 5..40, :if => :password_required? |
10 | validates_confirmation_of :password, :if => :password_required? |
11 | validates_length_of :login, :within => 3..40 |
12 | validates_uniqueness_of :login, :salt |
13 | before_save :encrypt_password |
14 | |
15 | has_many :projects |
16 | |
17 | # Authenticates a user by their login name and unencrypted password. Returns the user or nil. |
18 | def self.authenticate(login, password) |
19 | # use this instead if you want user activation |
20 | # u = find :first, :select => 'id, salt', :conditions => ['login = ? and activated_at IS NOT NULL', login] |
21 | u = find_by_login(login) # need to get the salt |
22 | return nil unless u |
23 | find :first, :conditions => ["id = ? AND crypted_password = ?", u.id, u.encrypt(password)] |
24 | end |
25 | |
26 | # Encrypts some data with the salt. |
27 | def self.encrypt(password, salt) |
28 | Digest::SHA1.hexdigest("--#{salt}--#{password}--") |
29 | end |
30 | |
31 | # Encrypts the password with the user salt |
32 | def encrypt(password) |
33 | self.class.encrypt(password, salt) |
34 | end |
35 | |
36 | protected |
37 | |
38 | # before filter |
39 | def encrypt_password |
40 | return if password.blank? |
41 | self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record? |
42 | self.crypted_password = encrypt(password) |
43 | end |
44 | |
45 | def password_required? |
46 | crypted_password.blank? or not password.blank? |
47 | end |
48 | |
49 | end |