| Class | User |
| In: |
app/models/user.rb
|
| Parent: | ActiveRecord::Base |
User models the system users, and is generated by the acts_as_authenticated Rails generator.
| password | [RW] | Virtual attribute for the unencrypted password |
adds a new encryption method.
# File app/models/user.rb, line 72
72: def self.add_encryption_method(sym, &block)
73: encryption_methods[sym] = block
74: end
a Hash containing the available encryption methods. Keys are symbols, values are Proc objects that contain the actual encryption code.
# File app/models/user.rb, line 67
67: def self.encryption_methods
68: @encryption_methods ||= {}
69: end
FIXME ugly workaround
# File app/models/user.rb, line 11
11: def self.human_attribute_name(attrib)
12: case attrib.to_sym
13: when :login: return _('Username')
14: when :email: return _('e-Mail')
15: else self.superclass.human_attribute_name(attrib)
16: end
17: end
# File app/models/user.rb, line 57
57: def self.system_encryption_method
58: @system_encryption_method || :salted_sha1
59: end
# File app/models/user.rb, line 61
61: def self.system_encryption_method=(method)
62: @system_encryption_method = method
63: end
# File app/models/user.rb, line 103
103: def authenticated?(password)
104: result = (crypted_password == encrypt(password))
105: if (encryption_method != User.system_encryption_method) && result
106: self.password_type = User.system_encryption_method.to_s
107: self.password = password
108: self.password_confirmation = password
109: self.save!
110: end
111: result
112: end
Changes the password of a user.
# File app/models/user.rb, line 140
140: def change_password!(current, new, confirmation)
141: raise IncorrectPassword unless self.authenticated?(current)
142: self.force_change_password!(new, confirmation)
143: end
Encrypts the password using the chosen method
# File app/models/user.rb, line 82
82: def encrypt(password)
83: method = self.class.encryption_methods[encryption_method]
84: if method
85: method.call(password, salt)
86: else
87: raise UnsupportedEncryptionType, "Unsupported encryption type: #{encryption_method}"
88: end
89: end
the encryption method used for this instance
# File app/models/user.rb, line 77
77: def encryption_method
78: (password_type || User.system_encryption_method).to_sym
79: end
Changes the password of a user without asking for the old password. This method is intended to be used by the "I forgot my password", and must be used with care.
# File app/models/user.rb, line 148
148: def force_change_password!(new, confirmation)
149: self.password = new
150: self.password_confirmation = confirmation
151: save! unless new_record?
152: end
# File app/models/user.rb, line 125
125: def forget_me
126: self.remember_token_expires_at = nil
127: self.remember_token = nil
128: save(false)
129: end
These create and unset the fields required for remembering users between browser closes
# File app/models/user.rb, line 119
119: def remember_me
120: self.remember_token_expires_at = 2.weeks.from_now.utc
121: self.remember_token = encrypt("#{email}--#{remember_token_expires_at}")
122: save(false)
123: end
# File app/models/user.rb, line 114
114: def remember_token?
115: remember_token_expires_at && Time.now.utc < remember_token_expires_at
116: end
before filter
# File app/models/user.rb, line 160
160: def encrypt_password
161: return if password.blank?
162: self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
163: self.password_type ||= User.system_encryption_method.to_s
164: self.crypted_password = encrypt(password)
165: end