-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.rb
52 lines (42 loc) · 1.45 KB
/
user.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-
require 'digest/sha1'
class User < ActiveRecord::Base
validates_presence_of :name
validates_uniqueness_of :name
attr_accessor :password_confirmation
validates_confirmation_of :password
validate :password_non_blank
def password
@password
end
def password= (pwd)
@password = pwd
return if pwd.blank?
create_new_salt
self.hashed_password = User.encrypted_password(self.password, self.salt)
#self.encrypted_password でも良いはず...?
#@password, self.salt でも良いはず...?
end
def self.authenticate (name, password)
user = self.find_by_name(name)
if user
encrypted_password = encrypted_password(password, user.salt)
#self.encrypted_password() と呼ぶほうがいいんじゃないかなぁ...
if user.hashed_password != encrypted_password
user = nil
end
end
user
end
private
def password_non_blank
errors.add(:password, "パスワードを入れてください") if hashed_password.blank?
end
def self.encrypted_password (password, salt)
string_to_hash = password + "wibble" + salt
Digest::SHA1.hexdigest(string_to_hash)
end
def create_new_salt
self.salt = self.object_id.to_s + rand.to_s
end
end