Last change
on this file since 202 was 59, checked in by KOBAYASHI, Shinji, 16 years ago |
login window created
|
File size:
957 bytes
|
Rev | Line | |
---|
[57] | 1 | require 'digest/sha1'
|
---|
| 2 |
|
---|
| 3 | class User < ActiveRecord::Base
|
---|
| 4 |
|
---|
| 5 | validates_presence_of :name
|
---|
| 6 | validates_uniqueness_of :name
|
---|
| 7 |
|
---|
| 8 | attr_accessor :password_confirmation
|
---|
| 9 | validates_confirmation_of :password
|
---|
| 10 |
|
---|
| 11 | attr_reader :password
|
---|
| 12 |
|
---|
| 13 | def password=(pwd)
|
---|
| 14 | @password = pwd
|
---|
| 15 | create_new_salt
|
---|
| 16 | self.hashed_passwd = User.encrypted_password(self.password, self.salt)
|
---|
| 17 | end
|
---|
| 18 |
|
---|
| 19 | def validate
|
---|
| 20 | erros.add_to_base("¥Ñ¥¹¥ï¡¼¥É¤òÆþÎϤ·¤Æ¤¯¤À¤µ¤¤") if hashed_passwd.blank?
|
---|
| 21 | end
|
---|
| 22 |
|
---|
| 23 | def self.authenticate(name, password)
|
---|
| 24 | user = self.find_by_name(name)
|
---|
| 25 | if user
|
---|
[59] | 26 | expected_password = encrypted_password(password, user.salt)
|
---|
| 27 | if user.hashed_passwd != expected_password
|
---|
[57] | 28 | user = nil
|
---|
| 29 | end
|
---|
| 30 | end
|
---|
| 31 | user
|
---|
| 32 | end
|
---|
| 33 | private
|
---|
| 34 |
|
---|
| 35 | def self.encrypted_password(password, salt)
|
---|
| 36 | string_to_hash = password + "cancerehr" + salt
|
---|
| 37 | Digest::SHA1.hexdigest(string_to_hash)
|
---|
| 38 | end
|
---|
| 39 |
|
---|
| 40 | def create_new_salt
|
---|
| 41 | self.salt = self.object_id.to_s + rand.to_s
|
---|
| 42 | end
|
---|
| 43 | end
|
---|
Note:
See
TracBrowser
for help on using the repository browser.