You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
789 B
33 lines
789 B
13 years ago
|
require 'sha1'
|
||
|
|
||
|
|
||
13 years ago
|
class User < ActiveRecord::Base
|
||
13 years ago
|
attr_accessor :password
|
||
|
attr_protected :hashed_password
|
||
13 years ago
|
validates_uniqueness_of :username
|
||
13 years ago
|
validates_confirmation_of :password, :if => lambda { |user|
|
||
|
user.new_record? or not user.password.blank? }
|
||
|
validates_length_of :password, :within => 5..40, :if => lambda { |user|
|
||
|
user.new_record? or not user.password.blank? }
|
||
|
|
||
|
def self.hashed(str)
|
||
|
SHA1.new(str).to_s
|
||
|
end
|
||
13 years ago
|
|
||
|
def self.authenticate(user_info)
|
||
13 years ago
|
user = find_by_username(user_info[:username])
|
||
|
if user && user.hashed_password == hashed(user_info[:password])
|
||
|
return user
|
||
|
end
|
||
|
end
|
||
|
|
||
|
private
|
||
|
before_save :update_password
|
||
|
|
||
|
def update_password
|
||
|
if not password.blank?
|
||
|
self.hashed_password = self.class.hashed(password)
|
||
|
end
|
||
13 years ago
|
end
|
||
13 years ago
|
end
|