Class | User |
In: |
app/models/user.rb
|
Parent: | ActiveRecord::Base |
this model expects a certain database layout and its based on the name/login pattern. This was autogenerated with the login generator
Authenticate a user.
Example:
@user = User.authenticate('bob', 'bobpass')
# File app/models/user.rb, line 18 18: def self.authenticate(login, pass) 19: find_first(["login = ? AND password = ?", login, sha1(pass)]) 20: end
Apply SHA1 encryption to the supplied password. We will additionally surround the password with a salt for additional security.
# File app/models/user.rb, line 27 27: def self.sha1(pass) 28: Digest::SHA1.hexdigest("#{salt}--#{pass}--") 29: end
Before saving the record to database we will crypt the password using SHA1. We never store the actual password in the DB.
# File app/models/user.rb, line 36 36: def crypt_password 37: write_attribute "password", self.class.sha1(password) 38: end
If the record is updated we will check if the password is empty. If its empty we assume that the user didn’t want to change his password and just reset it to the old value.
# File app/models/user.rb, line 45 45: def crypt_unless_empty 46: if password.empty? 47: user = self.class.find(self.id) 48: self.password = user.password 49: else 50: write_attribute "password", self.class.sha1(password) 51: end 52: end