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.

Methods

Classes and Modules

Class User::IncorrectPassword
Class User::UnsupportedEncryptionType

Attributes

password  [RW]  Virtual attribute for the unencrypted password

Public Class methods

adds a new encryption method.

[Source]

    # File app/models/user.rb, line 72
72:   def self.add_encryption_method(sym, &block)
73:     encryption_methods[sym] = block
74:   end

Authenticates a user by their login name and unencrypted password. Returns the user or nil.

[Source]

    # File app/models/user.rb, line 50
50:   def self.authenticate(login, password)
51:     u = find_by_login(login) # need to get the salt
52:     u && u.authenticated?(password) ? u : nil
53:   end

a Hash containing the available encryption methods. Keys are symbols, values are Proc objects that contain the actual encryption code.

[Source]

    # File app/models/user.rb, line 67
67:   def self.encryption_methods
68:     @encryption_methods ||= {}
69:   end

FIXME ugly workaround

[Source]

    # 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

[Source]

    # File app/models/user.rb, line 57
57:   def self.system_encryption_method
58:     @system_encryption_method || :salted_sha1
59:   end

[Source]

    # File app/models/user.rb, line 61
61:   def self.system_encryption_method=(method)
62:     @system_encryption_method = method
63:   end

Public Instance methods

[Source]

     # 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.

  • Raises IncorrectPassword if current is different from the user‘s current password.
  • Saves the record unless it is a new one.

[Source]

     # 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

[Source]

    # 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

[Source]

    # 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.

[Source]

     # 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

[Source]

     # 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

[Source]

     # File app/models/user.rb, line 154
154:   def name
155:     person.name
156:   end

These create and unset the fields required for remembering users between browser closes

[Source]

     # 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

[Source]

     # 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

Protected Instance methods

before filter

[Source]

     # 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

[Source]

     # File app/models/user.rb, line 167
167:     def password_required?
168:       crypted_password.blank? || !password.blank?
169:     end

[Validate]